在GitHub动作中获取当前分支并提交哈希 [英] Getting current branch and commit hash in GitHub action

查看:116
本文介绍了在GitHub动作中获取当前分支并提交哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to build a docker image using a GitHub action, migrating from TeamCity.

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

In the build script, I want to tag the image with a combination of branch and commit, e.g. master.ad959de. Testing that locally, I get that information like this:

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

这是GitHub动作的相关部分:

This is the relevant part of the GitHub action:

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

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

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

Running that script in that GitHub action results in this error:

fatal: ref HEAD is not a symbolic ref

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

How can I generate a tag like that inside a GitHub action?

推荐答案

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

github provides two variables that are useful here, you'll need to process them a bit to get the values you desire:

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

GITHUB_SHA: The commit SHA that triggered the workflow. For example, ffac537e6cbbf934b08745a378932722df287a53.

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

GITHUB_REF: The branch or tag ref that triggered the workflow. For example, refs/heads/feature-branch-1. If neither a branch or tag is available for the event type, the variable will not exist.

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

The short values can be extracted like this:

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

这篇关于在GitHub动作中获取当前分支并提交哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆