我正在使用 Github Actions 构建 Docker 映像,并希望使用分支名称标记映像。
我找到了GITHUB_REF
变量,但结果是refs/heads/feature-branch-1
我只需要feature-branch-1
.
我正在使用 Github Actions 构建 Docker 映像,并希望使用分支名称标记映像。
我找到了GITHUB_REF
变量,但结果是refs/heads/feature-branch-1
我只需要feature-branch-1
.
我添加了一个单独的步骤,用于从中提取分支名称$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 }}
我相信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#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
有关参数扩展语法的更多信息,请参阅此答案。
作为参考,GitHub Actions 的虚拟环境页面列出了执行环境中可用的所有环境变量。
请注意,如果您在拉取请求触发器上执行 GitHub 操作,那么GITHUB_REF
变量将包含类似refs/pull/421/merge
这样的内容,如果您尝试使用git push
该名称,它很可能会失败。
不过,您可以使用 YAML 中 GitHub 上下文的引用。就像是:${{ github.head_ref }}
你可以使用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
使用setenv
现在已弃用。建议使用环境文件。在@youjin 的回答的基础上,在仍然允许feature/
分支(替换所有出现的/
with -
)的同时,我现在正在使用这个:
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 }}
如何在 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/}"
致谢:@rmunn 评论
或者使用接受的答案中的方法,这里是更短的版本(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 }}
。
笔记:
GitHub Action 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"
存储库的演示工作流程文件中还提供了适用于所有操作系统(Linux、macOS 和 Windows)的演示!
要将其设置为环境变量,我使用以下语法:
- 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'
中替换/
为_
我刚刚使用 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/})
这是输出的屏幕截图:
这是一个适用于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 现在支持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_name }}
似乎至少适用于推送。
如果您使用的是 V2,actions/checkout
那么您始终可以运行git branch --show-current
以获取当前签出的分支的名称。
现在${{github.ref}}
是获取分支名称的正确方法。请记住${{github.ref}}
有refs/heads/..
前缀
要同时处理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/}})"
在 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}
对于使用 Windows 映像运行操作的人,需要了解的几个关键点:
- run: |
...
shell: cmd
所以,总而言之,你不需要浪费潜在的时间来试图弄清楚如何以破旧的 cmd 方式做事(就像我做的那样)。
为了获取当前分支的名称,您可以在将 shell 设置为“bash”时使用流行的解决方案,或者使用例如以下简单方法在默认 PowerShell shell 中设置变量:
$branchName = $Env:GITHUB_REF -replace "refs/heads/", ""
无论是否触发 pull_request,我都做了一个获取分支名称的操作。https://github.com/EthanSK/git-branch-name-action
处理pull_request
和push
事件的解决方案。实施解决方法以保存获得的分支名称以进行进一步的步骤,因为set-env
已弃用。不需要第三方操作。
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 }}"
只需使用:
env:
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
诀窍是github.head_ref
仅当工作流由 a 触发时才设置,pull_request
并且它包含 PR 的源分支的值。 github.ref_name
将不仅仅在工作流不是由 a 触发的情况下使用pull_request
,而且它也只包含分支名称。
if: github.ref == 'refs/heads/integration' && github.event_name == 'push'
您可以使用上述命令并替换您想要运行的任何分支或事件。
在 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_REF
默认为。dev
根据您的要求调整 sed 命令。
export GIT_BRANCH=$(echo ${GITHUB_REF:-dev} | sed s/.*\\///g)
通常,我总是有一个用nodejs
or编写的脚本python
,它从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;
}
这需要处理以下情况 -
有一个非常简单的 git 命令来获取当前分支:
git rev-parse --abbrev-ref 头
要在 env 文件变量中获取输出,只需输入:
- 名称:设置 CURRENT_BRANCH 运行: echo "CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_ENV
要从 env 变量中获取输出:
- 名称:获取 CURRENT_BRANCH 运行:回声 ${{ 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