[英]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.