[英]Good practice to write the version (= tag) in a file when tagging a Git repo with Github Actions
I have a repository with some source code and some Antora docs.我有一个包含一些源代码和一些 Antora 文档的存储库。 Now I'm trying to write a github action that updates the version in my antora.yml and puts the branch name.
现在我正在尝试编写一个 github 操作来更新我的 antora.yml 中的版本并放置分支名称。 When I don't change the version and then build my antora docs, I would habe
version: main
in my main branch and in my feature branch.当我不更改版本然后构建我的 antora 文档时,我会在我的主分支和我的功能分支中使用
version: main
。 Antora then crashes with Error: Duplicate nav in main
.然后 Antora 崩溃并出现
Error: Duplicate nav in main
。
I wrote a github action to sort this out.我写了一个 github 动作来解决这个问题。 On push my action checks if I'm in main and then commits
version: main
in my antora.yml and pushes the the remote repo.在推送时,我的操作会检查我是否在 main 中,然后在我的 antora.yml 中提交
version: main
并推送远程仓库。 When I'm in a feature branch, it puts version: <branchname>
and pushes.当我在功能分支中时,它会放置
version: <branchname>
并推送。 This works.这行得通。
But I get the same problem, when I create a tag from my main branch.但是当我从主分支创建标签时,我遇到了同样的问题。 Because the tag does not change any source code, the
version: main
is tagged and is present in my main branch.因为该标记不会更改任何源代码,所以
version: main
被标记并出现在我的主分支中。 This creates a new duplicate which breaks my Antora build.这会创建一个新的副本,它会破坏我的 Antora 构建。
My question is: how should I handle this?我的问题是:我应该如何处理这个? I would like to have eg
version: v0.1.0
in the tag but still version: main
in my main branch.我想在标签中有例如
version: v0.1.0
但仍然version: main
在我的主分支中。 Are there some good practices I can follow?我可以遵循一些好的做法吗? I guess other software projects face a similar issue.
我猜其他软件项目也面临着类似的问题。
My steps for Github Actions:我对 Github 操作的步骤:
# working for a feature branch
- name: Adjust version from 'main' to '<branchname>' if not already correct ... run on feature branch
if: contains(github.ref, 'refs/heads/feat/')
run: |
BRANCH="${{github.ref}}"
old="refs/heads/"
BRANCH="${BRANCH//$old/}"
old="/"
new="__"
BRANCH="${BRANCH/$old/$new}"
old='version: main'
new="version: $BRANCH"
sed -i "s|$old|$new|g" docs/antora.yml
cat docs/antora.yml
shell: bash
# working for main branch
- name: Adjust version from '<branchname>' to 'main' if not already correct ... run on main branch
if: github.ref == 'refs/heads/main'
run: |
oldPattern='version: '
new='version: main'
sed -i "/$oldPattern/s/.*/$new/" docs/antora.yml
cat docs/antora.yml
shell: bash
# not working for a tag
- name: Adjust version from '<branchname>' or 'main' to version 'v*' from tag
if: startsWith(github.ref, 'refs/tags/v')
run: |
TAG="${{github.ref}}"
old="refs/tags/"
TAG="${TAG//$old/}"
oldPattern='version: '
new="version: $TAG"
sed -i "/$oldPattern/s/.*/$new/" docs/antora.yml
cat docs/antora.yml
shell: bash
# https://github.com/marketplace/actions/add-commit
- name: Commit and push
uses: EndBug/add-and-commit@v9
with:
author_name: sebastian
author_email: sebastian@sommerfeld.io
message: "[Actions Bot] refactor: set antora version to branchname"
add: docs/antora.yml
When tagging, of course the workflow crashes with this message:标记时,工作流当然会因以下消息而崩溃:
Error: Error: fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
state now, use
git push origin HEAD:<name-of-remote-branch>
As said: how should I tackle this?如前所述:我应该如何解决这个问题?
I guess this would work, but I'm sure there must be a better way.我想这会起作用,但我相信一定有更好的方法。 Anyone got an idea?
有人有想法吗?
I invested some research and took a look into trunk based development and wrote a guideline for myself.我进行了一些研究并研究了基于主干的开发并为自己编写了指南。
What do you think of this?你觉得这怎么样? Does this fit a common pattern or am I on my own with this idea?
这符合一个共同的模式还是我自己有这个想法?
This should at least help with putting the branch-name into my antora-yml because now I have a branch for a version before tagging.这至少应该有助于将分支名称放入我的 antora-yml 中,因为现在我在标记之前有一个版本的分支。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.