HiveBrain v1.2.0
Get Started
← Back to all entries
snippetdockerMinor

Anyone know how to use $CIRCLE_BUILD_NUM across different jobs in CircleCI?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
anyoneacrossknowcirclecidifferentjobshowusecircle_build_num

Problem

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?

Solution

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.

Code Snippets

- 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"

Context

StackExchange DevOps Q#9414, answer score: 2

Revisions (0)

No revisions yet.