我正在用Github动作构建Docker图像,并想用分支名称标记图像。

我找到了GITHUB_REF变量,但它导致了refs/heads/feature-branch-1,我只需要feature-branch-1。


当前回答

我们目前有一个单独的作业,它在所有其他作业之前运行。该步骤根据分支名称决定在什么环境中运行。我们的分支与部署基础设施和代码库的特定环境相关联。每个环境都有自己的秘密(github企业特性)。

输出环境变量可用于所有其他连续作业。您可以在步骤中使用该变量,例如将其设置为NODE_ENV,或者用它标记docker图像。您还可以在该特定变量上设置并发性,以确保同一时间只运行使用相同值的单个作业或工作流。这使得它非常强大。

下面是我上面描述的一个例子:

name: Build pipeline
on:
  push:
    branches:
      - feature/*
      - develop
      - release/*
      - main

jobs:
  environments:
    name: Set environment
    runs-on: ubuntu-latest
    steps:
      - run: echo "Setting $ENVIRONMENT.."
    outputs:
      # Defaults to 'dev' in case of a feature branch
      # You could also use the contains expression if needed
      environment: ${{ github.ref == 'refs/heads/main' && 'prd' || (startsWith(github.ref, 'refs/heads/release/') && 'acc' || github.ref == 'refs/heads/develop' && 'tst' || 'dev') }}

  build:
    name: Docker build
    runs-on: ubuntu-latest
    needs: [environments]
    environment: ${{ needs.environments.outputs.environment }} # Enterprise only
    concurrency: ${{ needs.environments.outputs.environment }}
    env:
      ENVIRONMENT: ${{ needs.environments.outputs.environment }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Build
        run: |
          docker build . \
            -t hello/world:${{ needs.environments.outputs.environment }}

注意:因为提取环境名称不是在bash或powershell中完成的,你被绑定到github动作提供的有限表达式。如果你需要更花哨的东西,你可以用不同的方式来做,这不是唯一的真理。然而,我总是喜欢让事情简单易懂。

替代(快速)选项

如果你不介意使用其他人的github动作,你可以使用市场上的许多动作之一,做一个找到一个替换。这里可以找到一个例子,它看起来像:

on: [push]

jobs:
  replace-job:
    runs-on: ubuntu-latest
    name: 'Find and replace'
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Remove refs/heads/ from string
        uses: mad9000/actions-find-and-replace-string@1
        id: findandreplace
        with:
          source: ${{ github.ref }}
          find: 'refs/heads/'
          replace: ''
      - name: Branch name
        run: echo "The branch name is ${{ steps.findandreplace.outputs.value }}"

其他回答

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

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

博士TL;

这适用于你可以在on下指定的每个触发器(例如push或pull_request):

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

解释

诀窍在于github。head_ref仅在工作流由pull_request触发时设置,并且它包含PR. github的源分支的值。Ref_name只在工作流不是由pull_request触发的情况下使用,而且它也只包含分支名称。

GitHub的文档

来自GitHub官方文档的详细解释:

github。ref_name string触发工作流运行的分支或标记的短引用名称。该值与GitHub上显示的分支或标记名称匹配。例如,feature-branch-1。

github。工作流运行中pull请求的head_ref或源分支。此属性仅在触发工作流运行的事件为pull_request或pull_request_target时可用。

下面是一个适用于push和pull_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动作上使用分支名称

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

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}