Getting current branch and commit hash in GitHub action

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

提交回复
热议问题