我正在用Github动作构建Docker图像,并想用分支名称标记图像。
我找到了GITHUB_REF变量,但它导致了refs/heads/feature-branch-1,我只需要feature-branch-1。
我正在用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
其他回答
如果你使用的是V2的actions/checkout,那么你总是可以运行git branch——show-current来获取当前签出的分支的名称。
现在不建议使用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 }}
注意,如果你在pull request触发器上执行你的GitHub动作,那么GITHUB_REF变量将包含类似refs/pull/421/merge这样的东西,所以如果你尝试git push到那个名字,它很可能会失败。
你可以在YAML中使用GitHub上下文中的引用。比如:${{github。head_ref}}
https://help.github.com/en/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#github-context
获取当前分支很简单,下面的代码适用于我和
github.ref_name
是用于获取分支名称的预定义变量吗
jobs:
main-branch-build:
if: github.ref_name == 'main'
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3
feature-branch-build:
if: github.ref_name != 'main'
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3
我们目前有一个单独的作业,它在所有其他作业之前运行。该步骤根据分支名称决定在什么环境中运行。我们的分支与部署基础设施和代码库的特定环境相关联。每个环境都有自己的秘密(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 }}"