【问题标题】:GitHub actions: default branch variableGithub 操作:默认分支变量
【发布时间】:2021-02-23 03:11:04
【问题描述】:

有什么聪明的方法可以确定 Github 动作中的默认分支吗?

现在我需要写一些类似的东西:

on:
  push:
    branches:
      - master

有没有办法写出类似下面的代码?

on:
  push:
    branches:
      - $default-branch

我尝试谷歌但一无所获

【问题讨论】:

    标签: github continuous-integration github-actions


    【解决方案1】:

    $default-branch 可以在工作流模板中使用,但不能在工作流中使用。分支将在初始化时在工作流中被硬编码,并且必须手动维护。 [1]

    博文:https://github.blog/changelog/2020-07-22-github-actions-better-support-for-alternative-default-branch-names/

    【讨论】:

      【解决方案2】:

      我偶然发现了一个非常好的方法来解决这个问题。评估为分支名称,例如主人。

      ${{ github.event.repository.default_branch }}
      

      另外,发现always() 有副作用的困难方式:如果always() 未被调用,即使其他子句为真,我的工作也会被跳过。

      只有在默认分支上运行时才能运行作业

        if: ${{ always() && format('refs/heads/{0}', ) == github.ref }}
      

      【讨论】:

      • 这应该是公认的答案。无需额外的步骤或其他疯狂的解决方法。
      • github.com/manubot/rootstock/pull/452 中更新到此方法。我只是希望 github.event 上下文包含所有潜在事件类型的此字段。
      • ${{ github.event.repository.default_branch }} 表达式可以在作业内部使用,但不能像本题要求的那样在on: 语句下定义事件触发器
      【解决方案3】:

      目前这是不可能的。请在github community查看此主题

      你可以简单地在这个级别达到变量

      工作流程无效。 .github/workflows/so-004-variables-in-trigger.yaml(第 7 行,第 9 列):无法识别的命名值:'env'。位于表达式中的位置 1:env.default-branch

      您可以考虑根据分支名称添加 filterint,例如 here,但目前您无法做您想做的事情。

      【讨论】:

        【解决方案4】:

        将此步骤添加到您的工作中:

            - name: Determine default branch
              run: |
                DEFAULT_BRANCH=$(git remote show origin | awk '/HEAD branch/ {print $NF}')
                echo "default_branch=$DEFAULT_BRANCH" >> $GITHUB_ENV
                echo "default_branch_ref=refs/heads/$DEFAULT_BRANCH" >> $GITHUB_ENV
        

        这会将default_branchdefault_branch_ref 变量添加到env 环境变量中。 然后,您可以在后续步骤中使用 ${{ env.default_branch }} 访问默认分支名称。 default_branch_ref 变量可用于直接与 github.ref 进行比较以确定您是否在默认分支上。

        此方法使用当前设置环境变量的方法以在后续步骤中使用[1] 和 JoeLinux 确定默认分支名称的方法[2]

        完整示例工作流程:

        name: ci
        
        on: [push, pull_request]
        
        jobs:
          ci:
            runs-on: ubuntu-latest
            steps:
            - uses: actions/checkout@v2
        
            - name: Determine default branch
              run: |
                DEFAULT_BRANCH=$(git remote show origin | awk '/HEAD branch/ {print $NF}')
                echo "default_branch=$DEFAULT_BRANCH" >> $GITHUB_ENV
                echo "default_branch_ref=refs/heads/$DEFAULT_BRANCH" >> $GITHUB_ENV
        
            - name: debug
              run: echo ${{ env.default_branch }}
        
            - name: Deploy
              if: github.ref == env.default_branch_ref
              run: echo "Run!"
        

        【讨论】:

          【解决方案5】:
          - 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"
          

          【讨论】:

          • 奇怪的是,我查看了示例文档以打印出上下文,看起来这是在 github.event.repository.master_branch 中找到的
          • @ChrisKnoll - 你有构建的链接吗?这听起来确实很奇怪。
          【解决方案6】:

          希望将来会有更好的方法来做到这一点。在此之前,您可以使用 GitHub API 并将结果保存在命名的步骤输出中。

          例如

              - name: Extract default branch name
                shell: bash
                run: |
                  owner="my-org"
                  repo="repo_x"
                  branch=$(curl -L -H 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
                      https://api.github.com/repos/${owner}/${repo} \
                      | jq .default_branch)
                  echo "##[set-output name=default_branch;]$(echo ${branch})"
                id: repo_x
          ...
          ${{ steps.repo_x.outputs.default_branch }}
          

          【讨论】:

            猜你喜欢
            • 2020-12-05
            • 2020-09-01
            • 1970-01-01
            • 2022-12-16
            • 2021-04-09
            • 1970-01-01
            • 2020-03-06
            • 2021-06-08
            • 2018-08-02
            相关资源
            最近更新 更多