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?

2 Answers 2

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.

1
  • ${BUILD_NUMBER} won't be available in other jobs and this is what the question was about
    – dgpro
    Commented Dec 22, 2022 at 10:53
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 >>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.