15

My use case is I want to have a unique version number for artifacts per each build/run. With current tools like CircleCI, Travis, etc. there is a build number available which is basically a counter that always goes up. So, I can create version strings like 0.1.0-27. This counter is increased each time even for the same commit.

How can I do something similar with GitHub Actions? Github actions only offer GITHUB_SHA and GITHUB_REF.

Bless
  • 4,071
  • 2
  • 35
  • 39
moorara
  • 3,099
  • 7
  • 42
  • 55
  • Does the $GITHUB_EVENT_PATH JSON file contain some sort of unique ID that you can use? – cspotcode Feb 06 '19 at 20:15
  • 2
    Wouldn't `GITHUB_SHA` be the idiomatic way of doing this though? A counter can get confusing quickly, if you have several branches, etc. – maxheld Jun 20 '19 at 11:03

4 Answers4

16

GitHub Actions now has a unique number and ID for a run/build in the github context.

github.run_id : A unique number for each run within a repository. This number does not change if you re-run the workflow run.

github.run_number : A unique number for each run of a particular workflow in a repository. 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.

ref: https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#github-context

You can reference them in workflows like this:

      - name: Output Run ID
        run: echo ${{ github.run_id }}
      - name: Output Run Number
        run: echo ${{ github.run_number }}
peterevans
  • 13,485
  • 3
  • 40
  • 55
  • 1
    Important note: these numbers do not change if you re-run the action. This makes for a pretty fragile build number. – Javelin Apr 27 '21 at 20:51
8

I had the same problem and have just created an action to generate sequential build numbers. Use it like

- uses: einaregilsson/build-number@v1
  with:
    token: ${{secrets.github_token}}

In steps after that you'll have a BUILD_NUMBER environment variable. See more info about using the same build number for different jobs and more at https://github.com/einaregilsson/build-number/

UPDATE: There is now a $GITHUB_RUN_NUMBER variable built into GitHub Actions, so this approach is not needed anymore.

Einar Egilsson
  • 3,556
  • 8
  • 32
  • 39
0

If you want a constant integer increment (1,2,3,4,5), I haven't found anything in the docs that you could use as such increment which is aware of how many times that particular action ran. There's two solutions I can think of:

  1. Maintaining state on the repo: for example with a count.build file that uses the workflow ID and you increment it on build. This is my least favourite solution of the two because it adds other complexities, like it will itself trigger a push event. You could store this file somewhere else like S3 or in a Gist.

  2. Using the Date: if you're not worried about sequence on the integer increment you could just use the current data and time, for example 0.1.0-201903031310 for Today at 13:10.

Regardless if you have Actions Beta Access, I would definitely feed this back to GitHub.

Hope it helps.

bitoiu
  • 5,698
  • 5
  • 33
  • 50
0

You can use GitVersion to generate incrementing versions from tags in Git. The PR at https://github.com/GitTools/GitVersion/pull/1787 has some details, but basically you can define this job:

- uses: actions/checkout@v1
    - name: Get Git Version
      uses: docker://gittools/gitversion:5.0.2-beta1-34-linux-debian-9-netcoreapp2.1
      with:
        args: /github/workspace /nofetch /exec /bin/sh /execargs "-c \"echo $GitVersion_MajorMinorPatch > /github/workspace/version.txt\""
Phyxx
  • 14,436
  • 12
  • 64
  • 92