Getting current branch and commit hash in GitHub action

前端 未结 5 2356
生来不讨喜
生来不讨喜 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:59

    A handy way to get the currrent branch and commit sha on the workflow could be getting it and saving on a "variable".

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

    Maybe your docker-build.sh could receive the branch and she as parameter, os the full version as parameter.

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

    OR JUST

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

    On this action you can see many tests I done to see what works, what don't.

提交回复
热议问题