Specify docker tag via environment variable

I would like to use an environment variable to specify a docker image tag:

      - image: example/example-docker:${BRANCH}

This environment variable can be created in a previous job and will work with the $CIRCLE_BRANCH env to determine the value to be assigned to $BRANCH

If I set $BRANCH in the UI it can be used as the tag name example/example-docker:${BRANCH} but I seem to be unable to programatically assign the value to an env in config in a job before and then use that value in a later job.

I have looked into assigning an export to $BASH_ENV

echo 'export BRANCH=if [ $CIRCLE_BRANCH = 'master' ]; then echo 'master'; else echo 'develop'; fi' >> $BASH_ENV 

but $BRANCH is not available when the docker image tag needs to be assigned

Is what I am trying to do possible? Any thoughts on how I can achieve setting a docker image tagName dependant on $CIRCLE_BRANCH ?

As you mentioned, setting BRANCH in the local shell is too late if you want to use it as the Docker tag. Two approaches that I can think of:

  1. Just change the actual Docker tag name in branches to use the develop tag. This avoids having to use the variable at all. The downside would be remembering to not merge that change to the master branch.
  2. Use CircleCI Workflows and create two jobs. One for the master branch and one for other branches. Each job will use its own specific Docker tag. For steps in the job that are redundant (which may be all of them), a YAML anchor can be used.

Many thanks, I went down the route of using anchors, merges and overrides

1 Like