我正在用Github动作构建Docker图像,并想用分支名称标记图像。
我找到了GITHUB_REF变量,但它导致了refs/heads/feature-branch-1,我只需要feature-branch-1。
我正在用Github动作构建Docker图像,并想用分支名称标记图像。
我找到了GITHUB_REF变量,但它导致了refs/heads/feature-branch-1,我只需要feature-branch-1。
当前回答
更新
GitHub现在支持GITHUB_REF_NAME,它代表触发工作流运行的分支或标记名称。
GitHub文档在这个https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables上
其他回答
我们目前有一个单独的作业,它在所有其他作业之前运行。该步骤根据分支名称决定在什么环境中运行。我们的分支与部署基础设施和代码库的特定环境相关联。每个环境都有自己的秘密(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_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
示例输出-主分支
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一步: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一步:16
有关参数展开语法的更多信息,请参阅这个答案。
作为参考,GitHub Actions的虚拟环境页面列出了执行环境中可用的所有环境变量。
${{ github.ref_name }}
至少对推送来说还行。参见ref_name了解更多细节。它说:
触发工作流运行的分支或标记名称。
下面是一个适用于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
我做了一个获取分支名称的操作,而不管是否触发pull_request。https://github.com/EthanSK/git-branch-name-action