[英]Good practice to write the version (= tag) in a file when tagging a Git repo with Github Actions
我有一个包含一些源代码和一些 Antora 文档的存储库。 现在我正在尝试编写一个 github 操作来更新我的 antora.yml 中的版本并放置分支名称。 当我不更改版本然后构建我的 antora 文档时,我会在我的主分支和我的功能分支中使用version: main
。 然后 Antora 崩溃并出现Error: Duplicate nav in main
。
我写了一个 github 动作来解决这个问题。 在推送时,我的操作会检查我是否在 main 中,然后在我的 antora.yml 中提交version: main
并推送远程仓库。 当我在功能分支中时,它会放置version: <branchname>
并推送。 这行得通。
但是当我从主分支创建标签时,我遇到了同样的问题。 因为该标记不会更改任何源代码,所以version: main
被标记并出现在我的主分支中。 这会创建一个新的副本,它会破坏我的 Antora 构建。
我的问题是:我应该如何处理这个? 我想在标签中有例如version: v0.1.0
但仍然version: main
在我的主分支中。 我可以遵循一些好的做法吗? 我猜其他软件项目也面临着类似的问题。
我对 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
标记时,工作流当然会因以下消息而崩溃:
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>
如前所述:我应该如何解决这个问题?
我想这会起作用,但我相信一定有更好的方法。 有人有想法吗?
我进行了一些研究并研究了基于主干的开发并为自己编写了指南。
你觉得这怎么样? 这符合一个共同的模式还是我自己有这个想法?
这至少应该有助于将分支名称放入我的 antora-yml 中,因为现在我在标记之前有一个版本的分支。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.