英文:
Github actions - How to get a build number that starts from 1 in each branch
问题
I can help with that. Here's the translation of the provided text:
假设我有以下分支:
- 主分支
- 特性分支1
- 特性分支2
在我的 GitHub Actions 中,我需要知道我已经推送到某个分支的次数,或者我是否手动触发了该分支上的操作。
使用 ${{ github.run_attempt }}
:
每次我推送时,我会得到数字 1
。只有在我手动重新运行作业时,才会得到一个新的数字。
使用 ${{ github.run_number }}
:
每次我在任何分支中推送到这个存储库时,这个数字会递增。
所以假设我推送到主分支、特性分支1或特性分支2,它都会加到这个数字中。
但如果我手动触发流水线,它不会增加这个数字。
我需要一个变量,如果我首次推送到任何分支,它会给我数字 1
,如果我再次推送或手动触发,它会累加。所以假设我将特性分支1推送两次,然后手动运行一次,然后再次推送,下一次推送或手动触发时,它会给我数字 4,与其他分支或其流水线发生的情况无关。
我需要这个数字,因为在我的 Docker 注册表中,我的路径如下:
docker-reg-host/project-name/branch-name:the-number-from-above
the-number-from-above 是手动触发或推送的流水线在特定分支中触发的次数,而不是其他分支。
我想在每次触发时添加一个新标签,这个标签就是这个数字。
我可以使要求更简单,请求一个返回特定分支的 run_number 的变量,然后我将选择这个策略:
docker-reg-host/project-name/branch-name:branch_run_number-run_attempt
但至少我需要知道我已经推送到这个特定分支的次数。
英文:
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
答案1
得分: 2
GitHub 不会为每个分支记录运行编号。GitHub 也不会以一种方便获取的方式公开分支的推送次数。
这个编号需要由你自己确定。
-
一种方法是让你的工作流为提交打上标签,这样你可以统计分支上的标签数量,从而得到编号。
-
另一种方法是查询 Docker 存储库中已推送的版本。使用一些 PowerShell 或其他脚本语言,你可以找到分支上的最高版本,并在推送新容器之前加一。
-
还有一种方法是使用当前日期和时间作为分支的版本号。
-
或者,简单地按照迄今为止每个人都建议的方法,统计自上次标签/分支以来的提交数量,然后可选择性地将尝试编号添加到其中。
这是一个使用“标签”策略并自动推送新标签到存储库的操作。你可以传递一个前缀,在你的情况下是分支名称 (ref_name
),以生成每个分支的新编号:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: 生成构建编号
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论