mirror of
https://github.com/tj-actions/branch-names.git
synced 2024-11-23 23:03:50 +08:00
Merge pull request #21 from tj-actions/add-support-for-tags
Added support for getting the current tag
This commit is contained in:
commit
fdb3a42221
22
.github/workflows/test.yml
vendored
22
.github/workflows/test.yml
vendored
@ -2,6 +2,8 @@ name: CI
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
|
tags:
|
||||||
|
- '*'
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
pull_request:
|
pull_request:
|
||||||
@ -19,6 +21,7 @@ jobs:
|
|||||||
id: branch-name
|
id: branch-name
|
||||||
uses: ./
|
uses: ./
|
||||||
- name: Show output
|
- name: Show output
|
||||||
|
if: "!startsWith(github.ref, 'refs/tags/')"
|
||||||
run: |
|
run: |
|
||||||
echo "Current Branch: ${{ steps.branch-name.outputs.current_branch }}"
|
echo "Current Branch: ${{ steps.branch-name.outputs.current_branch }}"
|
||||||
echo "Base Ref: ${{ steps.branch-name.outputs.base_ref_branch }}"
|
echo "Base Ref: ${{ steps.branch-name.outputs.base_ref_branch }}"
|
||||||
@ -26,38 +29,43 @@ jobs:
|
|||||||
echo "Ref: ${{ steps.branch-name.outputs.ref_branch }}"
|
echo "Ref: ${{ steps.branch-name.outputs.ref_branch }}"
|
||||||
echo "Is Default: ${{ steps.branch-name.outputs.is_default }}"
|
echo "Is Default: ${{ steps.branch-name.outputs.is_default }}"
|
||||||
- name: Check is_default for pull request
|
- name: Check is_default for pull request
|
||||||
if: github.event_name == 'pull_request' && steps.branch-name.outputs.is_default != 'false'
|
if: github.event_name == 'pull_request' && !startsWith(github.ref, 'refs/tags/') && steps.branch-name.outputs.is_default != 'false'
|
||||||
run: |
|
run: |
|
||||||
echo "Is default is invalid: ${{ steps.branch-name.outputs.is_default }}"
|
echo "Is default is invalid: ${{ steps.branch-name.outputs.is_default }}"
|
||||||
exit 1
|
exit 1
|
||||||
- name: Check is_default for non pull request
|
- name: Check is_default for non pull request
|
||||||
if: github.event_name != 'pull_request' && steps.branch-name.outputs.is_default != 'true'
|
if: github.event_name != 'pull_request' && !startsWith(github.ref, 'refs/tags/') && steps.branch-name.outputs.is_default != 'true'
|
||||||
run: |
|
run: |
|
||||||
echo "Is default is invalid: ${{ steps.branch-name.outputs.is_default }}"
|
echo "Is default is invalid: ${{ steps.branch-name.outputs.is_default }}"
|
||||||
exit 1
|
exit 1
|
||||||
- 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' && !startsWith(github.ref, 'refs/tags/') && !steps.branch-name.outputs.base_ref_branch
|
||||||
run: |
|
run: |
|
||||||
echo "Base ref unset: ${{ steps.branch-name.outputs.base_ref_branch }}"
|
echo "Base ref unset: ${{ steps.branch-name.outputs.base_ref_branch }}"
|
||||||
exit 1
|
exit 1
|
||||||
- name: Test head ref output
|
- name: Test head ref output
|
||||||
if: github.event_name == 'pull_request' && !steps.branch-name.outputs.head_ref_branch
|
if: github.event_name == 'pull_request' && !startsWith(github.ref, 'refs/tags/') && !steps.branch-name.outputs.head_ref_branch
|
||||||
run: |
|
run: |
|
||||||
echo "Head ref unset: ${{ steps.branch-name.outputs.head_ref_branch }}"
|
echo "Head ref unset: ${{ steps.branch-name.outputs.head_ref_branch }}"
|
||||||
exit 1
|
exit 1
|
||||||
- name: Test ref output
|
- name: Test ref output
|
||||||
if: "!steps.branch-name.outputs.ref_branch"
|
if: "!steps.branch-name.outputs.ref_branch && !startsWith(github.ref, 'refs/tags/') "
|
||||||
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.
|
- name: Test current branch output for pull_request event.
|
||||||
if: github.event_name == 'pull_request' && !steps.branch-name.outputs.current_branch
|
if: github.event_name == 'pull_request' && !startsWith(github.ref, 'refs/tags/') && !steps.branch-name.outputs.current_branch
|
||||||
run: |
|
run: |
|
||||||
echo "Current branch unset: ${{ steps.branch-name.outputs.current_branch }}"
|
echo "Current branch unset: ${{ steps.branch-name.outputs.current_branch }}"
|
||||||
exit 1
|
exit 1
|
||||||
- name: Test current branch output for push event.
|
- name: Test current branch output for push event.
|
||||||
if: github.event_name == 'push' && !steps.branch-name.outputs.current_branch
|
if: github.event_name == 'push' && !startsWith(github.ref, 'refs/tags/') && !steps.branch-name.outputs.current_branch
|
||||||
run: |
|
run: |
|
||||||
echo "Current branch unset: ${{ steps.branch-name.outputs.current_branch }}"
|
echo "Current branch unset: ${{ steps.branch-name.outputs.current_branch }}"
|
||||||
exit 1
|
exit 1
|
||||||
|
- name: Test current tag for push events.
|
||||||
|
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') && !steps.branch-name.outputs.tag
|
||||||
|
run: |
|
||||||
|
echo "Current tag unset: ${{ steps.branch-name.outputs.tag }}"
|
||||||
|
exit 1
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@ Get branch information without the `/ref/*` prefix
|
|||||||
| 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 |
|
||||||
|
| tag | `string` | `v0.0.1` *OR* `0.0.1` | The tag that triggered the workflow run |
|
||||||
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@ -78,6 +79,11 @@ Get branch information without the `/ref/*` prefix
|
|||||||
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.
|
||||||
|
|
||||||
|
- name: Get the current tag
|
||||||
|
if: startsWith(github.ref, 'refs/tags/')
|
||||||
|
run: |
|
||||||
|
echo "${{ steps.branch-name.outputs.tag }}"
|
||||||
|
# Outputs: "v0.0.1" OR "0.0.1"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
13
action.yml
13
action.yml
@ -31,12 +31,16 @@ outputs:
|
|||||||
ref_branch:
|
ref_branch:
|
||||||
value: ${{ steps.branch.outputs.ref_branch }}
|
value: ${{ steps.branch.outputs.ref_branch }}
|
||||||
description: 'The branch that triggered the workflow run.'
|
description: 'The branch that triggered the workflow run.'
|
||||||
|
tag:
|
||||||
|
value: ${{ steps.branch.outputs.tag }}
|
||||||
|
description: 'The tag that triggered the workflow run.'
|
||||||
|
|
||||||
runs:
|
runs:
|
||||||
using: "composite"
|
using: "composite"
|
||||||
steps:
|
steps:
|
||||||
- id: branch
|
- id: branch
|
||||||
run: |
|
run: |
|
||||||
|
if [[ !${{ startsWith(inputs.ref, 'refs/tags/') }} ]]; then
|
||||||
export BASE_REF=${{ inputs.base_ref }}
|
export BASE_REF=${{ inputs.base_ref }}
|
||||||
export HEAD_REF=${{ inputs.head_ref }}
|
export HEAD_REF=${{ inputs.head_ref }}
|
||||||
export REF=${{ inputs.ref }}
|
export REF=${{ inputs.ref }}
|
||||||
@ -44,6 +48,7 @@ runs:
|
|||||||
echo "::set-output name=head_ref_branch::${HEAD_REF/refs\/heads\//}"
|
echo "::set-output name=head_ref_branch::${HEAD_REF/refs\/heads\//}"
|
||||||
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\//}"
|
||||||
|
fi
|
||||||
shell: bash
|
shell: bash
|
||||||
- id: current_branch
|
- id: current_branch
|
||||||
run: |
|
run: |
|
||||||
@ -61,6 +66,14 @@ runs:
|
|||||||
echo "::set-output name=is_default::false"
|
echo "::set-output name=is_default::false"
|
||||||
fi
|
fi
|
||||||
shell: bash
|
shell: bash
|
||||||
|
- id: tag
|
||||||
|
run: |
|
||||||
|
if [[ ${{ startsWith(inputs.ref, 'refs/tags/') }} ]]; then
|
||||||
|
export REF=${{ inputs.ref }}
|
||||||
|
TAG=${REF/refs\/tags\//}
|
||||||
|
echo "::set-output name=tag::$TAG"
|
||||||
|
fi
|
||||||
|
shell: bash
|
||||||
|
|
||||||
branding:
|
branding:
|
||||||
icon: git-branch
|
icon: git-branch
|
||||||
|
Loading…
Reference in New Issue
Block a user