3

I use $CIRCLE_BUILD_NUM in different jobs in a workflow (but it seems like that get incremented in between jobs of the same workflow)?

I did some research and it looks like I can switch to CIRCLE_WORKFLOW_ID which is unique for the workflow but then the ID is some really long string, not like the build number which is a small integer, nice that I can use to append to build numbers and docker tags and such.

Any ideas?

chicks
  • 1,911
  • 1
  • 13
  • 29
erotsppa
  • 131
  • 2

2 Answers2

2

This is the approach I came up with. It's kinda ugly, but it does work:

- run:
    name: Create unique build identifier
    command: |
        GIT_SHA=$( git rev-parse --short HEAD )
        echo "export GIT_SHA=${GIT_SHA}" >> "${BASH_ENV}"

        # the always-increasing counter, based on CIRCLE_BUILD_NUM
        BUILD_COUNTER="${CIRCLE_BUILD_NUM}"
        echo "export BUILD_COUNTER=${BUILD_COUNTER}" >> "${BASH_ENV}"

        # the build identifier, which includes the short git sha
        BUILD_NUMBER="CIRC${BUILD_COUNTER}-${GIT_SHA}"
        echo "export BUILD_NUMBER=${BUILD_NUMBER}" >> "${BASH_ENV}"

        # output build id and counter
        echo -e "\nbuild counter: ${BUILD_COUNTER}; build id: ${BUILD_NUMBER}\n"

Then ${BUILD_NUMBER} is available as an environment variable in other steps.

blalor
  • 121
  • 1
0

In v2.1 config you can use pipeline values, specifically pipeline.number. This number is the same across all jobs in a workflow. (Indeed, across all workflows in the pipeline.) You can use it like this:

    steps:
      - run: echo Hello from pipeline number << pipeline.number >>