in Uncategorized

How Create a Version Number with Github Actions Run Number + Adding an Offset Number

With CI/CD build pipelines like Github Actions, a common approach to versioning is to make use of the build number that is automatically incremented for each run of a workflow. Using this number on its own, or in combination of some semantic versioning, is pretty common, and you’ve most likely seen it described elsewhere.

In Github Actions, this number is accessed in your workflow file using the context github.run_number. Per the docs, this number begins at 1 for the workflow’s first run, and increments with each new run. This number does not change if you re-run the workflow run.

Now, let’s say you need this version number to start at a higher number. There is a good chance that whatever you are building has already been released, and therefore already has version numbers. There is even a good chance you were already using a similar method of auto-incrementing your version numbers on another CI/CD workflow.

In these cases, the obvious need is to start your numbering somewhere higher than the current Github Action run number. Actions currently don’t support calculations, but after some digging around, I found that we can do this pretty easily using shell shell variables and the GA run command.

Let’s say we need an integer for Android versionCode. This can be pretty much any number, but has to be higher than the current production version the Play Store. If our current versionCode is 945, we can jump over that with the following:

Example: Start our version or run number at 1000:

    steps:
    - name: create a custom version using run number offset by 1000 (run_number + 1000)
      run: |
        echo "VERSION_NUMBER_WITH_OFFSET=$((1000+GITHUB_RUN_NUMBER))" >> $GITHUB_ENV

Now we can access that in later steps using:

${{ env.VERSION_NUMBER_WITH_OFFSET }}

In the above example, we are:

  1. running a shell command to create a new environment variable called VERSION_NUMBER_WITH_OFFSET
  2. assigning it the result of our bash arithmetic expansion $(( expression )).
  3. This is where we add our offset (1000) and our current Github action run number, referenced in the Github Action environment variable GITHUB_RUN_NUMBER.
  4. Lastly, we make the environment variable available to any subsequent steps in the workflow job by defining or updating the environment variable and writing this to the GITHUB_ENV environment file.