Search code examples
mavencontinuous-integrationgo-cd

Put output of a command in a variable GoCD


I try to create a variable with the version of a project in a GoCD job.

My project is built with maven. The version of the project is set on the pom.xml file (present on the git repository).

Maven allow to retrieve the version of the project with command :

mvn help:evaluate -Dexpression=project.version

So I tried to put the output of this command in a linux variable :

my_version=${mvn help:evaluate -Dexpression=project.version}

And then use it to push the project on a docker repository :

docker push my_project:$my_version

The problem is that GoCD seems to have his own interpreter (see https://docs.gocd.org/current/faq/dev_use_current_revision_in_build.html ).To use a variable, I must do something like this :

sh -c docker push my_project:$my_version

But I can't find a way to set a linux variable.

Is it something that is ok to do with GoCD ? What is the right way to do this ?


Solution

  • GoCD doesn't maintain a single shell session for the entire job. So even if you use sh export MY_VERSION=... and try to use that in the next task within the same job it will not work as expected.

    To solve this problem the best way is to wrap the entire set of commands you want to perform within a shell script and execute that shell script as a task. Given the entire shell script would be executed within a single shell instance anything variable you define can be used.