【问题标题】:Getting current branch and commit hash in GitHub action在 GitHub 操作中获取当前分支和提交哈希
【发布时间】:2020-03-12 03:41:42
【问题描述】:

我想使用 GitHub 操作构建一个 docker 映像,从 TeamCity 迁移。

在构建脚本中,我想用分支和提交的组合标记图像,例如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 操作中生成类似的标签?

【问题讨论】:

标签: git github github-actions


【解决方案1】:

来自Using environment variables

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

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

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

短值可以这样提取:

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

【讨论】:

  • 请注意,如果分支名称包含/ git_branch 值将不正确。例如ref/heads/actions/test 转换为test 其中actions/test 是实际的分支名称。而是使用 git_branch=${branch#refs/heads/} 。来源:stackoverflow.com/a/58035262/9720375
  • 自发布此答案以来,也许 github 添加了更多环境变量; git_branch 现在应该可以作为 GITHUB_REF_NAME 环境变量使用。
【解决方案2】:

在工作流中获取当前分支并提交 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 上,您可以看到我进行的许多测试,看看哪些有效,哪些无效。

【讨论】:

    【解决方案3】:

    另一种方法是使用github context

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

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

    【讨论】:

    • 这应该是正确的答案,因为${{ github.sha }} 是获得提交 SHA 的正确方法
    【解决方案4】:

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

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

    【讨论】:

    • alpha. 语法是什么?
    • @Rüdiger 它是构建脚本的参数,很可能是 Docker 映像的标记。它与动作语法无关,但表达式的评估将导致像“alpha.ec7ba90”这样的参数被传递到构建脚本中。
    • 不应该把6→7改成7位短SHA吗?
    【解决方案5】:

    你可以通过这种方式在你的 sh 文件中获取它 -

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

    【讨论】:

      【解决方案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 }}
      

      【讨论】:

        【解决方案7】:

        使用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.
        
        

        【讨论】:

          【解决方案8】:

          您应该能够使用 ${{ github.event.pull_request.head.sha }} 访问最新 Git 提交的 SHA,假设这是由拉取请求触发的操作

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-06-04
            • 1970-01-01
            • 2014-10-29
            • 2021-09-04
            • 1970-01-01
            • 2013-03-18
            • 2021-01-01
            • 2017-06-21
            相关资源
            最近更新 更多