我正在用Github动作构建Docker图像,并想用分支名称标记图像。
我找到了GITHUB_REF变量,但它导致了refs/heads/feature-branch-1,我只需要feature-branch-1。
我正在用Github动作构建Docker图像,并想用分支名称标记图像。
我找到了GITHUB_REF变量,但它导致了refs/heads/feature-branch-1,我只需要feature-branch-1。
当前回答
if: github.ref == 'refs/heads/integration' && github.event_name == 'push'
您可以使用上面的命令替换您想要运行的任何分支或事件。
其他回答
现在不建议使用setenv。建议使用环境文件。基于@youjin的回答,同时仍然允许功能/分支(用-替换所有/的出现),我现在使用这个:
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_REF的环境变量,如果不存在,则默认为dev。
根据您的需求调整sed命令。
export GIT_BRANCH=$(echo ${GITHUB_REF:-dev} | sed s/.*\\///g)
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)的演示也可以在存储库的演示工作流文件!
你可以使用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
下面是一个适用于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