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

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


当前回答

你可以使用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

其他回答

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

GitHub Action FranzDiebold/ GitHub -env- vals - 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"

所有操作系统(Linux, macOS和Windows)的演示也可以在存储库的演示工作流文件!

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

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

git rev-parse --abbrev-ref HEAD

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

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

获取env变量的输出:

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

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

更新

GitHub现在支持GITHUB_REF_NAME,它代表触发工作流运行的分支或标记名称。

GitHub文档在这个https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables上