我正在用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}}类似于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}}。
注:
上述方法仅适用于基于unix的映像(Linux和macOS)。 对于文档,请阅读:GitHub Actions的上下文和表达式语法。
其他回答
在Windows上运行? Windows default命令是一个PowerShell终端。
- name: SET CURRENT_BRANCH
run: |
$branchName = "${{github.ref}}".Split("/")["${{github.ref}}".Split("/").Length -1]
echo "::set-env name=CURRENT_BRANCH::$(echo $branchName)"
更新
GitHub现在支持GITHUB_REF_NAME,它代表触发工作流运行的分支或标记名称。
GitHub文档在这个https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables上
我相信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的虚拟环境页面列出了执行环境中可用的所有环境变量。
通常,我总是有一个用nodejs或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;
}
这涉及到以下场景-
我想从PR部署到我的开发环境的变化 我想通过手动触发器将任何分支的更改部署到我的dev env中 我想从master部署更改到我的prod环境
我做了一个获取分支名称的操作,而不管是否触发pull_request。https://github.com/EthanSK/git-branch-name-action