简体   繁体   English

GitHub 操作 GIT_BRANCH 环境变量

[英]GitHub Actions GIT_BRANCH environment variable

I have a GitHub action running my unit tests for a web app.我有一个 GitHub 操作正在运行我的 Web 应用程序单元测试。 I run CodeClimate test reporting from this action.我从此操作运行 CodeClimate 测试报告。

CodeClimate requires two environment variables to be set for the report to be correctly sent. CodeClimate 需要设置两个环境变量才能正确发送报告。 These are;这些是;

  • GIT_SHA GIT_SHA
  • GIT_BRANCH GIT_BRANCH

GitHub actions makes the git commit sha avaiable through the github context via github.sha so I can set an environment variable on the action like this; GitHub 操作通过github.sha通过 github 上下文使 git commit sha 可用,因此我可以像这样在操作上设置环境变量;

env:
    GIT_SHA: ${{ github.sha }}

However github actions does not make the branch name available.但是 github 操作不会使分支名称可用。

It does provide a default environment variable called GITHUB_REF this is the full ref but I understand that I can grab the short ref ie the branch name using this shorthand syntax $GITHUB_REF##*/它确实提供了一个名为GITHUB_REF的默认环境变量,这是完整的引用,但我知道我可以使用这种速记语法$GITHUB_REF##*/获取短引用,即分支名称

The problem I have is that I cannot set an env variable called GITHUB_BRANCH with this value $GITHUB_REF##*/我遇到的问题是我无法使用此值设置名为 GITHUB_BRANCH 的环境变量$GITHUB_REF##*/

Does anyone how I might get the branch name and set it to the environment variable GIT_BRANCH so the CodeClimate test script would be able to use it.有谁知道我如何获取分支名称并将其设置为环境变量GIT_BRANCH以便 CodeClimate 测试脚本能够使用它。

Ultimately I want my env config to look like this:最终我希望我的 env 配置看起来像这样:

env:
    GIT_SHA: <git commit sha>
    GIT_BRANCH: <current git branch>

Ultimately I want my env config to look like this:最终我希望我的 env 配置看起来像这样:

 env: GIT_SHA: <git commit sha> GIT_BRANCH: <current git branch>

You can achieve the same effect (setting the environment variables) not only in the workflow definition but by setting the variables dynamically in a dedicated workflow step.您不仅可以在工作流定义中实现相同的效果(设置环境变量),还可以通过在专用工作流步骤中动态设置变量来实现相同的效果。 You can do it by environment files and built-in GITHUB_SHA and GITHUB_BRANCH variables :您可以通过环境文件内置的GITHUB_SHAGITHUB_BRANCH变量来完成

jobs:
  set-env:
    runs-on: ubuntu-latest
    steps:
      - name: Set environment variables
        run: |
          echo "GIT_SHA=${GITHUB_SHA}" >> $GITHUB_ENV
          echo "GIT_BRANCH=${GITHUB_REF##*/}" >> $GITHUB_ENV
      - name: Use environment variables
        run: |
          echo "GIT_SHA=${GIT_SHA}"
          echo "GIT_BRANCH=${GIT_BRANCH}"

Executing the workflow should give you the output:执行工作流应该给你输出:

在此处输入图片说明

Indeed, rather than using GITHUB_REF, it could be useful to use GITHUB_HEAD_REF or GITHUB_BASE_REF to figure out the real branch names (above all, if workflow is associated to a PR):实际上,与其使用 GITHUB_REF,不如使用 GITHUB_HEAD_REF 或 GITHUB_BASE_REF 来找出真正的分支名称(最重要的是,如果工作流与 PR 相关联):

Examples of output:输出示例:

    GITHUB_REF="refs/pull/4/merge"
    GITHUB_BASE_REF="main"
    GITHUB_HEAD_REF="key_advertisement"

More information here: https://docs.github.com/en/actions/learn-github-actions/contexts#github-context更多信息: https : //docs.github.com/en/actions/learn-github-actions/contexts#github-context

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM