kha*_*iuk 12 continuous-integration github github-actions
有没有什么聪明的方法来确定 Github 操作中的默认分支?
现在我需要写一些类似的东西:
on:
push:
branches:
- master
Run Code Online (Sandbox Code Playgroud)
有没有办法编写类似下面的代码?
on:
push:
branches:
- $default-branch
Run Code Online (Sandbox Code Playgroud)
我试着用谷歌搜索但什么也没找到
Dav*_* L. 13
$default-branch可以在工作流模板中使用,但不能在工作流中使用。分支将在初始化时在工作流中硬编码,并且必须手动维护。[1]
博文:https : //github.blog/changelog/2020-07-22-github-actions-better-support-for-alternative-default-branch-names/
fri*_*mle 11
- if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
run: echo "On the default branch"
- if: github.ref != format('refs/heads/{0}', github.event.repository.default_branch)
run: echo "Not on the default branch"
Run Code Online (Sandbox Code Playgroud)
编辑2023
正如 Elijah 在评论中指出的那样,ref_name现在有了一个新的领域。简化:
- if: github.ref_name == github.event.repository.default_branch
run: echo "On the default branch"
- if: github.ref_name != github.event.repository.default_branch
run: echo "Not on the default branch"
Run Code Online (Sandbox Code Playgroud)
我偶然发现了一个非常好的方法来解决这个问题。评估分支名称,例如 master。
${{ github.event.repository.default_branch }}
Run Code Online (Sandbox Code Playgroud)
此外,还发现了产生always()副作用的困难方式:always()即使另一个子句为真,如果没有调用,我的工作就会被跳过。
这适用于仅在默认分支上运行时运行作业
if: ${{ always() && format('refs/heads/{0}', ) == github.ref }}
Run Code Online (Sandbox Code Playgroud)
它可以达到 GitHub 上下文,特别是
github.event.repository.default_branch
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1739 次 |
| 最近记录: |