Getting current branch and commit hash in GitHub action

前端 未结 5 2352
生来不讨喜
生来不讨喜 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/}
    
    0 讨论(0)
  • 2021-02-19 05:52

    Using https://github.com/tj-actions/branch-names provides an output which also works for push and pull_request events

    
    ...
        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.
    
    
    0 讨论(0)
  • 2021-02-19 05:57

    You can get it this way in your sh file -

    BRANCH_NAME=$(echo $GITHUB_REF | cut -d'/' -f 3)
    GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c1-8)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-19 06:05

    Another approach is to use github context.

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

    The benefit of this approach is that you don't have to add a step to set the values. Note that it uses the full version of the sha (not the short version).

    0 讨论(0)
提交回复
热议问题