Merge pull request #7 from tj-actions/feature/update-to-always-return-the-current-branch-name

This commit is contained in:
Tonye Jack 2021-02-07 20:04:50 -05:00 committed by GitHub
commit b7e207141f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 3 deletions

View File

@ -20,9 +20,10 @@ jobs:
uses: ./ uses: ./
- name: Show output - name: Show output
run: | run: |
echo "${{ steps.branch-name.outputs.base_ref_branch }}" echo "Current Branch: ${{ steps.branch-name.outputs.current_branch }}"
echo "${{ steps.branch-name.outputs.head_ref_branch }}" echo "Base Ref: ${{ steps.branch-name.outputs.base_ref_branch }}"
echo "${{ steps.branch-name.outputs.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 - name: Test base ref output
if: github.event_name == 'pull_request' && !steps.branch-name.outputs.base_ref_branch if: github.event_name == 'pull_request' && !steps.branch-name.outputs.base_ref_branch
run: | run: |
@ -38,3 +39,14 @@ jobs:
run: | run: |
echo "Ref unset: ${{ steps.branch-name.outputs.ref_branch }}" echo "Ref unset: ${{ steps.branch-name.outputs.ref_branch }}"
exit 1 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

View File

@ -12,6 +12,19 @@ Get branch information without the `/ref/*` prefix
- name: Get branch names - name: Get branch names
uses: tj-actions/branch-names@v2 uses: tj-actions/branch-names@v2
id: branch-names 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 - name: Get Ref brach name
run: | run: |
echo "${{ steps.branch-name.outputs.ref_branch }}" echo "${{ steps.branch-name.outputs.ref_branch }}"
@ -28,6 +41,7 @@ Get branch information without the `/ref/*` prefix
run: | run: |
echo "${{ steps.branch-name.outputs.base_ref_branch }}" echo "${{ steps.branch-name.outputs.base_ref_branch }}"
# Outputs: "main" for main <- PR branch. # Outputs: "main" for main <- PR branch.
``` ```
@ -65,6 +79,7 @@ on:
| Output | type | Example | Description | | 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 | | 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 | | 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 | | ref_branch | `string` | `1/merge` OR `main` | The branch that triggered the workflow run |

View File

@ -20,6 +20,9 @@ inputs:
required: false required: false
outputs: 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: base_ref_branch:
value: ${{ steps.branch.outputs.base_ref_branch }} value: ${{ steps.branch.outputs.base_ref_branch }}
description: 'The target branch of a pull request' description: 'The target branch of a pull request'
@ -43,6 +46,15 @@ runs:
REF_BRANCH=${REF/refs\/pull\//} REF_BRANCH=${REF/refs\/pull\//}
echo "::set-output name=ref_branch::${REF_BRANCH/refs\/heads\//}" echo "::set-output name=ref_branch::${REF_BRANCH/refs\/heads\//}"
shell: bash 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: branding:
icon: git-branch icon: git-branch
color: white color: white