Passing variable to tasks

66 views
Skip to first unread message

Shivani Shinde

unread,
Apr 8, 2020, 7:43:07 AM4/8/20
to go-cd
Hi everyone,
I am having the hardest time dealing with variables for a task.

What I want to achieve:

1. I want to pass a variable(pipeline's path) to the task which generates builds. This is build command:
make build path="/pipeline_path"

I tried setting variable pipelinepath, in the environment variable path, and using it in the task as specified in the document like:
make build path=$pipelinepath

or

make build path="$pipelinepath"

But it does not work!

2. I want to export certain variable assigning it a path of the directory I git cloned. I have mentioned the git URL in Materials of pipeline and mentioned destination directory as 'myProject'.
I want to do the following:
export myVar="/path/to/myProject"


I want to achieve this at task level.

How can I work on this? Any help will be appreciated!

Thank you.

Ashwanth Kumar

unread,
Apr 8, 2020, 8:01:16 AM4/8/20
to go...@googlegroups.com
One thing I've learned working with GoCD for a while is, you don't want to use absolute paths anywhere. Especially when agents are run across different hosts or as ephemeral containers somewhere. 

To get the current pipeline path one can use something like $(pwd) or dirname etc.  and use relative paths from that location. 

Another point is Tasks within a job don't share any session on a shell of any kind among them. So if you export a variable and try to use it in the next task or later within the job it wouldn't work. They will have to be specified at the environment, pipeline, stage or at a job level. The specificity goes in the reverse order. Also as a general convention I've achieved best results when these scripts are NOT in GoCD tasks but instead in a separate shell script file and added as part of the source code or via a separate ops repo material(s). 

HTH. 

CONFIDENTIALITY. This email and any attachments are confidential to Alef Edge Inc., and may also be privileged, except where the email states it can be disclosed. If this email is received in error, please do not disclose the contents to anyone, notify the sender by return email, and delete this email (and any attachments) from your system.

--
You received this message because you are subscribed to the Google Groups "go-cd" group.
To unsubscribe from this group and stop receiving emails from it, send an email to go-cd+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/go-cd/083817de-2527-4e3e-a53f-36d4a1b407f4%40googlegroups.com.

Shivani Shinde

unread,
Apr 8, 2020, 9:46:12 AM4/8/20
to go-cd
Hi,
Thank you for responding.
I was initially using scripts only, but due to some reasons I need to mention git URL in materials.

I also tried using $(pwd) as suggested by you, but it gives me output as:
[go] Task: echo $(pwd)took: 0.2s
$
(pwd)


I understand using scripts is better way to deal with variables when trying to export them, but I was trying to find if any other way exists.

Thank you.

On Wednesday, April 8, 2020 at 5:31:16 PM UTC+5:30, Ashwanth Kumar wrote:
One thing I've learned working with GoCD for a while is, you don't want to use absolute paths anywhere. Especially when agents are run across different hosts or as ephemeral containers somewhere. 

To get the current pipeline path one can use something like $(pwd) or dirname etc.  and use relative paths from that location. 

Another point is Tasks within a job don't share any session on a shell of any kind among them. So if you export a variable and try to use it in the next task or later within the job it wouldn't work. They will have to be specified at the environment, pipeline, stage or at a job level. The specificity goes in the reverse order. Also as a general convention I've achieved best results when these scripts are NOT in GoCD tasks but instead in a separate shell script file and added as part of the source code or via a separate ops repo material(s). 

HTH. 

On Wed, 8 Apr, 2020, 17:13 Shivani Shinde, <shivan...@alefedge.com> wrote:
Hi everyone,
I am having the hardest time dealing with variables for a task.

What I want to achieve:

1. I want to pass a variable(pipeline's path) to the task which generates builds. This is build command:
make build path="/pipeline_path"

I tried setting variable pipelinepath, in the environment variable path, and using it in the task as specified in the document like:
make build path=$pipelinepath

or

make build path="$pipelinepath"

But it does not work!

2. I want to export certain variable assigning it a path of the directory I git cloned. I have mentioned the git URL in Materials of pipeline and mentioned destination directory as 'myProject'.
I want to do the following:
export myVar="/path/to/myProject"


I want to achieve this at task level.

How can I work on this? Any help will be appreciated!

Thank you.


CONFIDENTIALITY. This email and any attachments are confidential to Alef Edge Inc., and may also be privileged, except where the email states it can be disclosed. If this email is received in error, please do not disclose the contents to anyone, notify the sender by return email, and delete this email (and any attachments) from your system.

--
You received this message because you are subscribed to the Google Groups "go-cd" group.
To unsubscribe from this group and stop receiving emails from it, send an email to go...@googlegroups.com.

Ashwanth Kumar

unread,
Apr 8, 2020, 9:55:15 AM4/8/20
to go...@googlegroups.com
Try using it like this 

bash -c "echo $(pwd)" 

To unsubscribe from this group and stop receiving emails from it, send an email to go-cd+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/go-cd/ba9282ab-cf09-4be5-808a-4711cefe6d90%40googlegroups.com.

Shivani Shinde

unread,
Apr 8, 2020, 10:05:42 AM4/8/20
to go...@googlegroups.com
But I need to pass the path to my command which will be something like this:
make build path=$(pwd)

Where I mention make in custom command field. How do you suggest to use bash -c  with this command?

Jason Smyth

unread,
Apr 8, 2020, 11:14:33 AM4/8/20
to go-cd
Hi Shivani,

If you want to wrap a call to anything in an explicit invocation of bash, the usual syntax is: bash -c "commandToRun with arguments".

So to run the make command you specified, it would be: bash -c 'make build path="$(pwd)"'. Note the quotes around "$(pwd)" to deal with paths that contain spaces.

Regards,
Jason


On Wednesday, 8 April 2020 10:05:42 UTC-4, Shivani Shinde wrote:
But I need to pass the path to my command which will be something like this:
make build path=$(pwd)

Where I mention make in custom command field. How do you suggest to use bash -c  with this command?

On Wed, Apr 8, 2020, 7:25 PM 'Ashwanth Kumar' via go-cd <go...@googlegroups.com> wrote:
Try using it like this 

bash -c "echo $(pwd)" 

--
You received this message because you are subscribed to the Google Groups "go-cd" group.
To unsubscribe from this group and stop receiving emails from it, send an email to go...@googlegroups.com.

Shivani Shinde

unread,
Apr 9, 2020, 12:04:33 AM4/9/20
to go...@googlegroups.com
Hi Jason,
Thank you for responding.

I tried to run the command as you suggested, but i get this error:
[go] Task: bash -c 'make build path="$(pwd)"'took: 0.2sexited: 127
bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
bash: make clean build_ap pkg path="$(pwd)": command not found
[go] Task status: failed, took: 0.2s, exited: 127
[go] Current job status: failed

I specified the following in task:

Screenshot from 2020-04-09 09-33-01.png

Will you let me know if this is correct way mentioning arguments as you suggested?

Thank you.

To unsubscribe from this group and stop receiving emails from it, send an email to go-cd+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/go-cd/72225a93-0f73-4f0c-9534-e5e606de96a0%40googlegroups.com.


--
Shivani Shinde
AlefEdge

Ashwanth Kumar

unread,
Apr 9, 2020, 12:32:34 AM4/9/20
to go...@googlegroups.com
We don't need to quote the commands after -c when we pass it via GoCD. Like this

bash
-c
make build path="$(pwd)



--

Ashwanth Kumar / ashwanthkumar.in

Shivani Shinde

unread,
Apr 9, 2020, 12:50:38 AM4/9/20
to go...@googlegroups.com
Hi Ashwanth,
I tried by your way, but it still gives me this error:

[go] Task: bash -c "make clean build_ap pkg path=$(pwd)"took: 0.3sexited: 127
bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
bash: make: command not found
[go] Task status: failed, took: 0.3s, exited: 127
I tried mentioning task in following 2 ways:
1)
bash
-c
make build path=$(pwd)

2) 
bash
-c
make build path="$(pwd)"


I don't know what I am doing wrong.



Shivani Shinde

unread,
Apr 9, 2020, 7:57:57 AM4/9/20
to go-cd
Hi Jason,
I did try that. But I keep getting same error.
Can you let me know commands need to mentioned from UI?

Jason Smyth

unread,
Apr 9, 2020, 9:27:47 AM4/9/20
to go-cd
Hi Shivani,

The error message is saying that the "make" command is not found.

I recommend trying the following as a troubleshooting step:
bash
-c
echo make build path="$(pwd)"

The output from this should be: make build path="/path/to/pipeline/workingDirectory"

If that works as intended then the next thing to troubleshoot is why make is not available on the path of your agent's shell.

Regards,
Jason

Shivani Shinde

unread,
Apr 9, 2020, 10:08:52 AM4/9/20
to go-cd
HI Jason,

Yes that did work and gave me correct path.

But I have a weird observation:
When I do not mention any environment variables, the command works perfectly fine!

And now when I add the environment variables, it give me the following error:
make: pwd: Command not found
make: uname: Command not found
/bin/sh: 1: basename: not found
/bin/sh: 1: date: not found
make: git: Command not found

Any idea why shall this be happening?

Jason Smyth

unread,
Apr 9, 2020, 11:21:39 AM4/9/20
to go-cd
Hi Shivani,

I'm not sure I understand. Can you post specific examples of what is working and what isn't?

Another thing to try is:
bash
-c
make

The output of this should tell you whether or not the agent is able to find make on the path.

Regards,
Jason

Shivani Shinde

unread,
Apr 14, 2020, 4:18:29 AM4/14/20
to go-cd
I was able to get it working. I mentioned my environment variables at Stage level. And mentioned task as:
bash
-c
make build pkg path
=$pipelinepath


Thank you Jason and Ashwanth for your help : )
Reply all
Reply to author
Forward
0 new messages