2

Suppose I have these branches:

  • main
  • feature1
  • feature2

In my github actions I need to know what is number of times I have pushed into a branch or I have manually triggered the actions on that branch

With ${{ github.run_attempt }}: each time I push I get the number 1. I only get a new number if I manually rerun the job

with ${{ github.run_number }}:

each time I push to this repository in any branch it is incremented.

so suppose I push to the main or feature1 or feature2 branches, it is added to this number

But if I manually trigger the pipeline, it is not added to it


I need a variable that if I push to any branch for the first gives me the number 1 and if I push again/or trigger manually it adds to it.

so suppose I push 2 times into the feature1 branch and run it manually once and then push again, it gives me the number 4 in the next push or manual trigger, independent of what has happened on the other branches or their pipelines.

I need this number because on my docker registry my path is like this:

docker-reg-host/project-name/branch-name:the-number-from-above

the-number-from-above is the number of times the pipeline is triggered manually or by pushing, in this specific branch not other branches

I want to add a new tag each time that is this number


I can make the requirement simpler and request a variable that returns run_number of a specific branch

then I am going to choose this strategy

docker-reg-host/project-name/branch-name:branch_run_number-run_attempt

But at least I need to know how many times I have pushed to this specific branch

Amin Ba
  • 1,603
  • 1
  • 13
  • 38
  • You can count the number of commits and then add `github.run_attempt` to it. See this thread for counting commits: https://stackoverflow.com/questions/11657295/count-the-number-of-commits-on-a-git-branch. – Azeem Mar 31 '23 at 17:27
  • I am not counting the commits - I am counting the pushes – Amin Ba Mar 31 '23 at 17:33
  • @azeem this is related to your comment https://stackoverflow.com/questions/63284060/git-rev-list-not-working-inside-github-action – Amin Ba Mar 31 '23 at 17:43
  • `I need this number because on my docker registry my path is like this: docker-reg-host/project-name/branch-name:the-number-from-above` why? Normally that suffix is a version tag – AD7six Mar 31 '23 at 18:20
  • no, it is not the case. I want each branch tag to be a meaningful number not a big number related to all branches – Amin Ba Mar 31 '23 at 18:57
  • That doesn’t answer the question of why, what does this mean `not a big number related to all branches`? Please zoom out and describe the problem you are trying to solve (in detail) – AD7six Apr 01 '23 at 06:03

1 Answers1

2

GitHub doesn't capture a run number per branch. GitHub also doesn't expose the number of pushes to a branch in an easy accessible way.

It's up to you to come up with a number.

  • One way would be for your workflow to tag the commit. That way you can count the tags on the branch. And thus get to your number.

  • Another way would be to query the docker registry for the versions that have been pushed there. With a bit of powershell or another scripting language you could then find the highest version on your branch and add one to it before pushing the new container.

  • Yet another way would be to use the current date&time as a version on the branch.

  • Or to simply do what everyone has suggested so far, to count the number of commits since the last tag/branch and optionally add the attempt number to it.

Here is an action that uses the "tag" strategy and automatically pushes a new tag to the repo. You can pass in a prefix, in your case the branch name (ref_name), to generate a new number per branch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Generate build number
      id: buildnumber
      uses: onyxmueller/build-tag-number@v1
      with:
        token: ${{ secrets.github_token }}
        prefix: ${{ github.github.ref_name }}

    - run: |
          write-output $env:build_number
      env:
        build_number: ${{ steps.buildnumber.outputs.build_number }}
      shell: pwsh
jessehouwing
  • 106,458
  • 22
  • 256
  • 341