Getting current branch and commit hash in GitHub action

前端 未结 5 2357
生来不讨喜
生来不讨喜 2021-02-19 05:26

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

In the build script, I want to tag the image with a combination of branch and commit, e.g.

5条回答
  •  孤独总比滥情好
    2021-02-19 05:44

    from Using environment variables

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

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

    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/}
    

提交回复
热议问题