diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 55fca25..4cb5c10 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,9 +20,10 @@ jobs: uses: ./ - name: Show output run: | - echo "${{ steps.branch-name.outputs.base_ref_branch }}" - echo "${{ steps.branch-name.outputs.head_ref_branch }}" - echo "${{ steps.branch-name.outputs.ref_branch }}" + echo "Current Branch: ${{ steps.branch-name.outputs.current_branch }}" + echo "Base Ref: ${{ steps.branch-name.outputs.base_ref_branch }}" + echo "Head Ref: ${{ steps.branch-name.outputs.head_ref_branch }}" + echo "Ref: ${{ steps.branch-name.outputs.ref_branch }}" - name: Test base ref output if: github.event_name == 'pull_request' && !steps.branch-name.outputs.base_ref_branch run: | @@ -38,3 +39,14 @@ jobs: run: | echo "Ref unset: ${{ steps.branch-name.outputs.ref_branch }}" exit 1 + - name: Test current branch output for pull_request event. + if: github.event_name == 'pull_request' && !steps.branch-name.outputs.current_branch + run: | + echo "Current branch unset: ${{ steps.branch-name.outputs.current_branch }}" + exit 1 + - name: Test current branch output for push event. + if: github.event_name == 'push' && !steps.branch-name.outputs.current_branch + run: | + echo "Current branch unset: ${{ steps.branch-name.outputs.current_branch }}" + exit 1 + diff --git a/README.md b/README.md index 8782583..fee62d8 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,19 @@ Get branch information without the `/ref/*` prefix - name: Get branch names uses: tj-actions/branch-names@v2 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 current branch that triggered the action. + - name: Get Ref brach name run: | echo "${{ steps.branch-name.outputs.ref_branch }}" @@ -28,6 +41,7 @@ Get branch information without the `/ref/*` prefix run: | echo "${{ steps.branch-name.outputs.base_ref_branch }}" # Outputs: "main" for main <- PR branch. + ``` @@ -65,6 +79,7 @@ on: | Output | type | Example | Description | |:--------------------:|:-----------:|:---------------------:|:------------------------------------------------:| +| current_branch | `string` | `main` | Always returns the branch that triggered the workflow run. | | base_ref_branch | `string` | `main` | The target branch of a pull request | | head_ref_branch | `string` | `feature/test` | The source branch of a pull request | | ref_branch | `string` | `1/merge` OR `main` | The branch that triggered the workflow run | diff --git a/action.yml b/action.yml index e86c37d..ac4a44d 100644 --- a/action.yml +++ b/action.yml @@ -20,6 +20,9 @@ inputs: required: false outputs: + current_branch: + value: ${{ steps.current_branch.outputs.current_branch }} + description: 'Returns the value of the current branch which is consistent regardless of event_type: i.e (push, pull_requests).' base_ref_branch: value: ${{ steps.branch.outputs.base_ref_branch }} description: 'The target branch of a pull request' @@ -43,6 +46,15 @@ runs: REF_BRANCH=${REF/refs\/pull\//} echo "::set-output name=ref_branch::${REF_BRANCH/refs\/heads\//}" shell: bash + - id: current_branch + run: | + if [[ ${{ github.event_name }} == 'pull_request' ]]; then + echo "::set-output name=current_branch::${{ steps.branch.outputs.head_ref_branch }}" + else + echo "::set-output name=current_branch::${{ steps.branch.outputs.ref_branch }}" + fi + shell: bash + branding: icon: git-branch color: white