在GitHub操作中获取当前分支并提交哈希

我想使用从TeamCity迁移的GitHub动作构建docker映像。

在构建脚本中,我想使用分支和提交的组合来标记图像,例如master.ad959de。在本地进行测试,我得到的信息是这样的:

git_branch=`git symbolic-ref --short HEAD`
git_hash=`git rev-parse --short HEAD`
docker_version=${git_branch}.${git_hash}

这是GitHub动作的相关部分:

name: CI
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1

    - name: Create docker image
      run: ./docker-build.sh  

在该GitHub动作中运行该脚本会导致以下错误:

fatal: ref HEAD is not a symbolic ref

如何在GitHub动作中生成类似的标记?

zpengibm 回答:在GitHub操作中获取当前分支并提交哈希

获取当前分支并在工作流程中提交sha 的一种简便方法是获取并保存在“变量”中。

  - name: Declare some variables
    id: vars
    shell: bash
    run: |
      echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
      echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"

  - name: Another step
    run: |
      echo "Branch: ${{ steps.vars.outputs.branch }}"
      echo "Sha: ${{ steps.vars.outputs.sha_short }}"

也许您的docker-build.sh可以收到分支,而她作为参数,而完整版则作为参数。

  - name: Create docker image
     run: ./docker-build.sh "${{ steps.vars.outputs.branch }}.${{ steps.vars.outputs.sha_short }}"

或者只是

  - name: Create docker image
     run: ./docker-build.sh "${GITHUB_REF#refs/heads/}.${GITHUB_SHA}"

this action上,您可以看到我所做的许多测试,以查看哪些有效,哪些无效。

,

另一种方法是使用 github context

- name: Create docker image
  run: ./docker-build.sh ${{ github.head_ref }}.${{ github.sha }}

这种方法的好处是您不必添加设置值的步骤。请注意,它使用了完整版的 sha(不是短版)。

,

来自Using environment variables

github提供了两个在这里有用的变量,您需要对其进行一些处理以获得所需的值:

  

GITHUB_SHA:触发工作流程的提交SHA。例如,ffac537e6cbbf934b08745a378932722df287a53

     

GITHUB_REF:触发工作流的分支或标记ref。例如,refs/heads/feature-branch-1。如果事件类型没有分支或标记,则该变量将不存在。

可以按以下方式提取短值:

git_hash=$(git rev-parse --short "$GITHUB_SHA")
git_branch=${GITHUB_REF##*/}
,

使用 Environment variables 获得缩短的 SHA 的最简单方法:

- name: Build Docker Image
    run: ./docker-build.sh alpha.${GITHUB_SHA::6}
,

也许像这样

name: CI 
on: push 
jobs:   
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1

    - name: Get Branch
      id: branch
      run: echo "git_branch=${GITHUB_REF#refs/heads/}" >> $GITHUB_BRANCH

    - name: Check Branch
      run: echo "${{ env.branch }}"

    - name: Get Hash
      id: hash
      run: echo "git_hash=$(git rev-parse --short "$GITHUB_SHA")" >> $GITHUB_HASH

    - name: Check Hash
      run: echo "${{ env.hash }}"

    - name: Create docker image
      run: ./docker-build.sh ${{ env.branch }} ${{ env.hash }}
,

您可以在sh文件中以这种方式获取它-

BRANCH_NAME=$(echo $GITHUB_REF | cut -d'/' -f 3)
GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c1-8)
,

使用 https://github.com/tj-actions/branch-names 提供的输出也适用于 pushpull_request 事件


...
    steps:
      - uses: actions/checkout@v2
      - name: Get branch names
        uses: tj-actions/branch-names@v2.1
        id: branch-names
      
      - name: Current branch name
        if: github.event_name == 'pull_request'
        run: |
          echo "${{ steps.branch-name.outputs.current_branch }}"
        # Outputs: "feature/test" current PR branch.
      
      - name: Current branch name
        if: github.event_name == 'push'
        run: |
          echo "${{ steps.branch-name.outputs.current_branch }}"
        # Outputs: "main" the default branch that triggered the push event.
      
      - name: Get Ref brach name
        run: |
          echo "${{ steps.branch-name.outputs.ref_branch }}"
        #  Outputs: "main" for non PR branches | "1/merge" for a PR branch

      - name: Get Head Ref branch name
        if: github.event_name == 'pull_request'
        run: |
          echo "${{ steps.branch-name.outputs.head_ref_branch }}"
        # Outputs: "feature/test" current PR branch.

      - name: Get Base Ref branch name
        if: github.event_name == 'pull_request'
        run: |
          echo "${{ steps.branch-name.outputs.base_ref_branch }}"
        # Outputs: "main" for main <- PR branch.

本文链接:https://www.f2er.com/3091962.html

大家都在问