Make an env variable from another env variable in a pipeline definition

14 views
Skip to first unread message

Alexey Savchkov

unread,
Dec 21, 2022, 10:22:45 PM12/21/22
to go-cd
Is it possible to create an environment variable referring to another existing environment variable in a pipeline definition? I use the Groovy configuration plugin and would like to do  something like this:
stage('Build') {
  environmentVariables = [
    IMAGE_TAG: "$GO_REVISION-$GO_PIPELINE_COUNTER"
  ]
  ...
The variables must be resolved at runtime, not ath the time of creating the configuration. Is this possible at all? In all my attempts I only get errors like
"No such property: GO_REVISION for class: cd.go.contrib.plugins.configrepo.groovy.dsl.Stage"
The only workaround I see is to set environment variables directly in the bash string before executing the actual bash commands but then this has to be done for every bash task and I don't quite like it.

Chad Wilson

unread,
Dec 21, 2022, 11:05:02 PM12/21/22
to go...@googlegroups.com
I don't believe there is any interpolation support for environment variables.

Normally I'd suggest that this type of logic is better to sit in a source controlled script than in ones pipeline configuration for ease of maintenance and to minimise coupling with the logic of your build tasks. However, for the specific example below, you might be able to use pipeline labels and the GO_PIPELINE_LABEL env var to meet a similar need, if you are OK with the names of the entire pipeline (not just the stage) in GoCD itself changing from "$GO_PIPELINE_COUNTER" to "$GO_REVISION-$GO_PIPELINE_COUNTER"

In this case you should be able to set the label_template to "${git}-${COUNT}" (or a variation with a truncated revision) and then use $GO_PIPELINE_LABEL for your image tag. See https://docs.gocd.org/current/configuration/pipeline_labeling.html and Groovy DSL docs at https://github.com/gocd-contrib/gocd-groovy-dsl-config-plugin/blob/4b19c96dc805ee7bbe292a9bc8adb65a05ba0612/dsl/src/main/java/cd/go/contrib/plugins/configrepo/groovy/dsl/Pipeline.java#L67-L98.

-Chad

--
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/d25de2ad-935e-4f93-a88c-a929c94fe468n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages