如何在 Github Actions 中获取当前分支?

How to get the current branch within Github Actions?

我正在使用 Github 操作构建 Docker 图像,并希望使用分支名称标记图像。

我找到了 GITHUB_REF 变量,但结果是 refs/heads/feature-branch-1 而我只需要 feature-branch-1.

我相信 GITHUB_REF 是唯一包含分支名称的环境变量。

您可以像这样从该字符串的其余部分中仅提取分支名称:

${GITHUB_REF##*/}

示例:

$ GITHUB_REF=refs/heads/feature-branch-1
$ echo ${GITHUB_REF##*/}
feature-branch-1

更新:添加了完整的工作流程示例。

工作流程

name: CI
on: push
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Git checkout
        uses: actions/checkout@v1
      - name: Branch name
        run: echo running on branch ${GITHUB_REF##*/}
      - name: Build
        run: docker build -t tedmiston/tag-example:${GITHUB_REF##*/} .

来源:https://github.com/tedmiston/x/blob/master/.github/workflows/workflow.yml

示例输出 - master 分支

Run docker build -t tedmiston/tag-example:${GITHUB_REF##*/} .
  docker build -t tedmiston/tag-example:${GITHUB_REF##*/} .
  shell: /bin/bash -e {0}
Sending build context to Docker daemon  146.9kB

Step 1/1 : FROM alpine
latest: Pulling from library/alpine
9d48c3bd43c5: Pulling fs layer
9d48c3bd43c5: Verifying Checksum
9d48c3bd43c5: Download complete
9d48c3bd43c5: Pull complete
Digest: sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb
Status: Downloaded newer image for alpine:latest
 ---> 961769676411
Successfully built 961769676411
Successfully tagged tedmiston/tag-example:master

日志:https://github.com/tedmiston/x/commit/cdcc58a908e41d3d90c39ab3bf6fef1ad2c4238a/checks#step:4:16

示例输出 - 非主分支

Run docker build -t tedmiston/tag-example:${GITHUB_REF##*/} .
  docker build -t tedmiston/tag-example:${GITHUB_REF##*/} .
  shell: /bin/bash -e {0}
Sending build context to Docker daemon  144.9kB

Step 1/1 : FROM alpine
latest: Pulling from library/alpine
9d48c3bd43c5: Pulling fs layer
9d48c3bd43c5: Verifying Checksum
9d48c3bd43c5: Download complete
9d48c3bd43c5: Pull complete
Digest: sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb
Status: Downloaded newer image for alpine:latest
 ---> 961769676411
Successfully built 961769676411
Successfully tagged tedmiston/tag-example:branch-name-test

日志:https://github.com/tedmiston/x/commit/4e8d31259f861aaa2c30375756fc081c3659bddf/checks#step:4:16


有关参数扩展语法的更多信息,请参阅 this answer

作为参考,页面 Virtual environments for GitHub Actions 列出了执行环境中可用的所有环境变量。

我添加了一个单独的步骤来从 $GITHUB_REF 中提取分支名称并将其设置为步骤输出

- name: Extract branch name
  shell: bash
  run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
  id: extract_branch

之后,我可以在接下来的步骤中使用它

- name: Push to ECR
  id: ecr
  uses: jwalton/gh-ecr-push@master
  with:
    access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
    secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
    region: us-west-2
    image: eng:${{ steps.extract_branch.outputs.branch }}

你可以使用 https://github.com/rlespinasse/github-slug-action

# Just add this  => 
- name: Inject slug/short variables
  uses: rlespinasse/github-slug-action@v3.x



# And you get this  => 
- name: Print slug/short variables
  run: |
    echo "Slug variables"
    echo " - ${{ env.GITHUB_REF_SLUG }}"    
    echo " - ${{ env.GITHUB_HEAD_REF_SLUG }}"
    echo " - ${{ env.GITHUB_BASE_REF_SLUG }}"
    echo " - ${{ env.GITHUB_REPOSITORY_SLUG }}"
    # output e.g. : master feat-new-feature v1.0.0 product-1.0.0-rc.2 new-awesome-product
    echo "Slug URL variables"
    echo " - ${{ env.GITHUB_REF_SLUG_URL }}"
    echo " - ${{ env.GITHUB_HEAD_REF_SLUG_URL }}"
    echo " - ${{ env.GITHUB_BASE_REF_SLUG_URL }}"
    echo " - ${{ env.GITHUB_REPOSITORY_SLUG_URL }}"
    # output e.g. : master feat-new-feature v1-0-0 product-1-0-0-rc-2 new-awesome-product
    echo "Short SHA variables"
    echo " - ${{ env.GITHUB_SHA_SHORT }}"
    # output e.g. : ffac537e

我刚刚使用 bash 脚本在 GitHub Actions 中做了一个简单的测试:

#!/bin/bash

echo Reserved for REPO_NAME=${GITHUB_REPOSITORY##*/}
echo GITHUB_REF=${GITHUB_REF}
echo EXTRACT_GITHUB_REF=${GITHUB_REF##*/}
echo EXTRACT_GITHUB_REF_HEADS=$(echo ${GITHUB_REF#refs/heads/})

cd $REPO_NAME
git checkout ${GITHUB_REF##*/}
git checkout $(echo ${GITHUB_REF#refs/heads/})

这是输出的屏幕截图:

所以 ${GITHUB_REF##*/}$(echo ${GITHUB_REF#refs/heads/}) 都是正确的

要将其设置为环境变量,我使用以下语法:

- name: Extract branch name
  shell: bash
  run: echo "::set-env name=BRANCH_NAME::$(echo ${GITHUB_REF#refs/heads/} | sed 's/\//_/g')"
- name: Test
  run: echo "${BRANCH_NAME}"

我在这里找到了这个语法:Github actions - starter worflows#How to define env variable? #68

rmq: sed 's/\//_/g'是把分支名

中的/替换成_

请注意,如果您在拉取请求触发器上执行 GitHub 操作,那么 GITHUB_REF 变量将包含类似 refs/pull/421/merge 的内容,因此如果您尝试 git push 这个名字很可能会失败。

您可以使用的是 YAML 中 GitHub 上下文的引用。类似于:${{ github.head_ref }}

https://help.github.com/en/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#github-context

if: github.ref == 'refs/heads/integration' && github.event_name == 'push' 

您可以使用上述命令并替换您想要 运行 的任何分支或事件。

How to get the current branch within Github Actions?

假设${{ github.ref }}类似于refs/heads/mybranch,您可以使用以下方法提取分支名称:

steps:
  - name: Prints the current branch name
    run: echo "${GITHUB_BRANCH##*/}"
    env:
      GITHUB_BRANCH: ${{ github.ref }}

如果您的分支包含斜杠(例如 feature/foo),请使用以下语法:

steps:
  - name: Prints the current branch name
    run: echo "${GITHUB_REF#refs/heads/}"

致谢:

或者使用接受的答案中的方法,这里是更短的版本(lint 友好):

steps:
  - name: Get the current branch name
    shell: bash
    run: echo "::set-output name=branch::${GITHUB_REF#refs/heads/}"
    id: myref

然后在其他步骤中引用为${{ steps.myref.outputs.branch }}

备注:

运行 在 Windows?。 Windows 默认命令是 PowerShell 终端。

  - name: SET CURRENT_BRANCH
    run: |
      $branchName = "${{github.ref}}".Split("/")["${{github.ref}}".Split("/").Length -1]
      echo "::set-env name=CURRENT_BRANCH::$(echo $branchName)"

在 GitHub 操作上使用分支名称

使用当前分支名称的便捷操作。 用法

name: build
on: push

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - run: npm ci
    - uses: nelonoel/branch-name@v1
    # Use branch name for whatever purpose
    - run: echo ${BRANCH_NAME}

这是适用于 pushpull_request 事件的完整工作流程

name: whichBranch
on: [pull_request, push]

jobs:
  which_branch:
    runs-on: ubuntu-latest
    steps:
      - name: Extract branch name on push
        if: github.event_name != 'pull_request'
        shell: bash
        run: echo "::set-env name=BRANCH_NAME::$(echo ${GITHUB_REF#refs/heads/})"
        id: extract_branch

      - name: Extract branch name on pull request
        if: github.event_name == 'pull_request'
        run: echo "::set-env name=BRANCH_NAME::$(echo ${GITHUB_HEAD_REF})"

      - name: Print branch name
        run: echo 'The branch name is' $BRANCH_NAME

GitHub 操作 FranzDiebold/github-env-vars-action 公开了几个有用的环境变量,例如当前分支名称及其 slug 值。我为这个用例做了这个动作。

用法

steps:
  - uses: FranzDiebold/github-env-vars-action@v1.2.0
  - name: Print environment variables
    run: |
      echo "GITHUB_REPOSITORY_SLUG=$GITHUB_REPOSITORY_SLUG"
      echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER"
      echo "GITHUB_REPOSITORY_OWNER_SLUG=$GITHUB_REPOSITORY_OWNER_SLUG"
      echo "GITHUB_REPOSITORY_NAME=$GITHUB_REPOSITORY_NAME"
      echo "GITHUB_REPOSITORY_NAME_SLUG=$GITHUB_REPOSITORY_NAME_SLUG"
      echo "GITHUB_REF_SLUG=$GITHUB_REF_SLUG"
      echo "GITHUB_REF_NAME=$GITHUB_REF_NAME"
      echo "GITHUB_REF_NAME_SLUG=$GITHUB_REF_NAME_SLUG"
      echo "GITHUB_SHA_SHORT=$GITHUB_SHA_SHORT"

存储库的 demo workflows file 中还提供了适用于所有操作系统(Linux、macOS 和 Windows)的演示!

使用 setenvnow deprecated. It is advised to use environment files. Building on @youjin's ,同时仍然允许 feature/ 分支(用 - 替换所有出现的 /),我现在正在使用这个:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Get branch name (merge)
        if: github.event_name != 'pull_request'
        shell: bash
        run: echo "BRANCH_NAME=$(echo ${GITHUB_REF#refs/heads/} | tr / -)" >> $GITHUB_ENV

      - name: Get branch name (pull request)
        if: github.event_name == 'pull_request'
        shell: bash
        run: echo "BRANCH_NAME=$(echo ${GITHUB_HEAD_REF} | tr / -)" >> $GITHUB_ENV

      - name: Debug
        run: echo ${{ env.BRANCH_NAME }}

对于使用 Windows 图片进行 运行 操作的人,需要了解的几个要点:

  1. 假设 GitHub 操作使用 CMD shell 是不正确的。默认情况下 use PowerShell
  2. 您可以指定shell使用以下方式:
- run: |
    ...
  shell: cmd
  1. 您可以使用值 'bash' 在 bash shell.
  2. 的上下文中执行命令

所以,总而言之,您不需要浪费可能的时间来弄清楚如何以破旧的 cmd 方式做事(就像我所做的那样)。

并且为了获取当前分支名称的简单目的,您可以在将 shell 设置为 'bash' 时使用流行的解决方案,或者使用例如以下简单方法来在默认 PowerShell 中设置一个变量 shell:

$branchName = $Env:GITHUB_REF -replace "refs/heads/", ""

无论 pull_request 是否触发,我都做了一个获取分支名称的操作。 https://github.com/EthanSK/git-branch-name-action

如果您使用的是 actions/checkout 的 V2,那么您始终可以 运行 git branch --show-current 获取当前签出的分支名称。

这是一个基于 $GITHUB_REF 设置环境变量的片段,如果不存在则默认为 dev

根据您的要求调整 sed 命令。

export GIT_BRANCH=$(echo ${GITHUB_REF:-dev} | sed s/.*\///g)

要同时处理 pull_request 事件(在这种情况下 $GITHUB_REF 包含一些无用的东西,例如 refs/pull/519/merge),您可以使用这个衬垫:

   - name: Set branch name
     run: echo "::set-output name=branch_name::$(echo ${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}})"

通常,我总是有一个用 nodejspython 编写的脚本,它从 workflow.yaml 调用。该脚本通常负责获取正确的分支引用等工作。

我在 prepare-deployment.js 脚本中有一个如下所示的函数-

const VALID_REF_PREFIX = 'refs/heads/';
...

function getBranchRef(isProd = false) {
  let branchRef = 'origin/master';

  if (isProd) {
    return branchRef;
  }
  /**
   * When the workflow is invoked from manual flow, the branch name
   * is in GITHUB_REF, otherwise, we have to look into GITHUB_BASE_REF
   */
  if (GITHUB_REF.startsWith(VALID_REF_PREFIX)) {
    // coming from a manual workflow trigger
    branchName = `origin/${GITHUB_REF.replace(VALID_REF_PREFIX, '')}`;
  } else {
    // coming from a PR
    branchRef = `origin/${GITHUB_HEAD_REF}`;
  }

  return branchRef;
}

这会处理以下情况-

  1. 我想将更改从 PR 部署到我的开发环境
  2. 我想通过手动触发器将我想要的任何分支的更改部署到我的开发环境中
  3. 我想将更改从 master 部署到我的产品环境中
${{ github.ref_name }}

似乎至少对于推送来说工作正常。

更新

GitHub 现在支持 GITHUB_REF_NAME,代表:The branch or tag name that triggered the workflow run.

GitHub 关于此 https://docs.github.com/en/actions/learn-github-actions/environment-variables

的文档

现在 ${{github.ref}} 是获取分支名称的正确方法。 请记住 ${{github.ref}}refs/heads/.. 前缀

有一个非常简单的git命令来获取当前分支:

git rev-parse --abbrev-ref HEAD

要在 env 文件变量中获取输出,只需输入:

      - name: Set CURRENT_BRANCH
        run: echo "CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_ENV

从环境变量获取输出:

      - name: Get CURRENT_BRANCH
        run: echo ${{ env.CURRENT_BRANCH}}

来源:https://www.techiedelight.com/determine-current-branch-name-git/

只是在这里重复一下,以便更好地了解其他人在之前的回复中作为简单评论写的内容:

https://docs.github.com/en/actions/learn-github-actions/environment-variables

分支名称仅用于拉取请求 在此环境变量中公开:

GITHUB_HEAD_REF Only set for pull request events. The name of the head branch.

在GitHub个动作中,对应的上下文键为:

github.head_ref

同时处理 pull_requestpush 事件的解决方案。实施解决方法以保存获得的分支名称以供进一步步骤使用,因为 set-env 已弃用。 不需要 third-party 操作。

name: CI
on: [ pull_request, push ]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: "Get branch name and save to env"
        env:
          IS_PR: ${{ github.EVENT_NAME == 'pull_request' }}
        run: |
          if ${IS_PR}; then
            BRANCH_NAME="${GITHUB_HEAD_REF}"
          else
            BRANCH_NAME="${GITHUB_REF##*/}"
          fi
          echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV

      - name: "Another step uses branch name"
        run: echo "Branch name is ${{ env.BRANCH_NAME }}"

Runtime Variables in GitHub Actions

只需使用:

env:
 BRANCH_NAME: ${{ github.head_ref || github.ref_name }} 

诀窍是 github.head_ref 仅在工作流被 pull_request 触发时设置,并且它包含 PR 的源分支的值。 github.ref_name 将仅在工作流不是由 pull_request 触发时使用,并且它也仅包含分支名称。

对于刚刚找到此主题的人,您现在可以使用 GITHUB_REF_NAME 例如${{ github.ref_name }}https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables

因此,如果您的触发操作工作流分支是 main,则此变量将设置为 main。例如,如果您有多个带有 releasemain 分支的回购协议,这将很有用。