Suppose I have these branches:
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With