branch-names/action.yml

140 lines
5.8 KiB
YAML
Raw Permalink Normal View History

2021-08-05 03:17:49 +08:00
name: Branch Names
2023-08-24 00:49:11 +08:00
description: Retrieve github branch or tag information without the /ref/* prefix
2020-12-11 21:15:40 +08:00
author: tj-actions
inputs:
2021-04-25 08:07:39 +08:00
strip_tag_prefix:
2023-03-09 17:26:25 +08:00
description: 'The prefix that should be stripped from the tag e.g `v` -> with a tag `v0.0.1` -> returns `0.0.1`'
2021-04-25 08:07:39 +08:00
default: ''
required: false
2020-12-11 21:23:56 +08:00
2020-12-11 21:15:40 +08:00
outputs:
2021-02-13 05:30:56 +08:00
is_default:
2021-02-13 05:33:03 +08:00
value: ${{ steps.default.outputs.is_default }}
2022-03-23 05:28:06 +08:00
description: 'Returns `"true"` if the current branch is the default else `"false"`.'
2021-05-27 20:24:26 +08:00
is_tag:
value: ${{ steps.tag.outputs.is_tag }}
2022-03-23 05:28:06 +08:00
description: 'Returns `"true"` if the current branch is a tag else `"false"`.'
default_branch:
value: ${{ steps.default.outputs.default_branch }}
2022-03-24 23:01:05 +08:00
description: 'The default branch name e.g `main` OR `master`'
2021-02-08 08:30:56 +08:00
current_branch:
value: ${{ steps.current_branch.outputs.current_branch }}
2022-08-27 23:35:53 +08:00
description: 'The current branch name regardless of event_type e.g `main`, `feature/test`'
2020-12-11 21:15:40 +08:00
base_ref_branch:
2020-12-11 21:41:21 +08:00
value: ${{ steps.branch.outputs.base_ref_branch }}
2022-09-24 14:38:31 +08:00
description: 'The target branch of a pull request or tag e.g `main`'
2020-12-11 21:15:40 +08:00
head_ref_branch:
2020-12-11 21:41:21 +08:00
value: ${{ steps.branch.outputs.head_ref_branch }}
2022-03-23 05:28:06 +08:00
description: 'The source branch of a pull request e.g `feature/test`'
2020-12-11 21:15:40 +08:00
ref_branch:
2020-12-11 21:41:21 +08:00
value: ${{ steps.branch.outputs.ref_branch }}
2022-03-23 05:28:06 +08:00
description: 'The branch that triggered the workflow run. e.g `1/merge`, `main`'
2021-04-24 18:35:28 +08:00
tag:
2021-04-24 18:50:42 +08:00
value: ${{ steps.tag.outputs.tag }}
2022-03-23 05:28:06 +08:00
description: 'The tag that triggered the workflow run. e.g `v0.0.1`, `0.0.1`'
2020-12-11 21:15:40 +08:00
runs:
2020-12-11 21:41:21 +08:00
using: "composite"
steps:
- id: branch
run: |
2022-08-22 00:59:36 +08:00
# "Set branch names..."
if [[ "${{ github.ref }}" != "refs/tags/"* ]]; then
BASE_REF=$(printf "%q" "${{ github.event.pull_request.base.ref || github.base_ref }}")
HEAD_REF=$(printf "%q" "${{ github.event.pull_request.head.ref || github.head_ref }}")
2021-09-15 01:38:39 +08:00
REF=$(printf "%q" "${{ github.ref }}")
BASE_REF=${BASE_REF/refs\/heads\//}
HEAD_REF=${HEAD_REF/refs\/heads\//}
2021-04-24 18:35:28 +08:00
REF_BRANCH=${REF/refs\/pull\//}
2021-09-15 01:38:39 +08:00
REF_BRANCH=${REF_BRANCH/refs\/heads\//}
if [[ -z "$GITHUB_OUTPUT" ]]; then
echo "::set-output name=base_ref_branch::$(eval printf "%s" "$BASE_REF")"
echo "::set-output name=head_ref_branch::$(eval printf "%s" "$HEAD_REF")"
echo "::set-output name=ref_branch::$(eval printf "%s" "$REF_BRANCH")"
else
echo "base_ref_branch=$(eval printf "%s" "$BASE_REF")" >> "$GITHUB_OUTPUT"
echo "head_ref_branch=$(eval printf "%s" "$HEAD_REF")" >> "$GITHUB_OUTPUT"
echo "ref_branch=$(eval printf "%s" "$REF_BRANCH")" >> "$GITHUB_OUTPUT"
fi
else
BASE_REF=$(printf "%q" "${{ github.event.base_ref }}")
BASE_REF=${BASE_REF/refs\/heads\/${{ inputs.strip_tag_prefix }}/}
if [[ -z "$GITHUB_OUTPUT" ]]; then
echo "::set-output name=base_ref_branch::$(eval printf "%s" "$BASE_REF")"
else
echo "base_ref_branch=$(eval printf "%s" "$BASE_REF")" >> "$GITHUB_OUTPUT"
fi
2021-04-24 18:35:28 +08:00
fi
2020-12-11 21:41:21 +08:00
shell: bash
2021-02-08 08:30:56 +08:00
- id: current_branch
run: |
2022-08-22 00:59:36 +08:00
# "Set the current branch name..."
2021-05-26 00:26:52 +08:00
if [[ "${{ github.ref }}" != "refs/tags/"* ]]; then
if [[ ${{ github.event_name }} == *"pull_request"* ]]; then
if [[ -z "$GITHUB_OUTPUT" ]]; then
echo "::set-output name=current_branch::${{ steps.branch.outputs.head_ref_branch }}"
else
echo "current_branch=${{ steps.branch.outputs.head_ref_branch }}" >> "$GITHUB_OUTPUT"
fi
2021-05-26 00:26:52 +08:00
else
if [[ -z "$GITHUB_OUTPUT" ]]; then
echo "::set-output name=current_branch::${{ steps.branch.outputs.ref_branch }}"
else
echo "current_branch=${{ steps.branch.outputs.ref_branch }}" >> "$GITHUB_OUTPUT"
fi
2021-05-26 00:26:52 +08:00
fi
2021-02-08 08:30:56 +08:00
fi
2021-02-08 08:39:26 +08:00
shell: bash
2021-02-13 05:33:03 +08:00
- id: default
2021-02-13 05:30:56 +08:00
run: |
2022-08-22 00:59:36 +08:00
# "Set the default branch name..."
if [[ "${{ github.ref }}" != "refs/tags/"* ]]; then
2022-11-13 07:17:13 +08:00
if [[ "${{ steps.current_branch.outputs.current_branch }}" == "${{ github.event.repository.default_branch }}" && "${{ github.event.pull_request.head.repo.fork }}" != "true" ]]; then
if [[ -z "$GITHUB_OUTPUT" ]]; then
echo "::set-output name=is_default::true"
echo "::set-output name=default_branch::${{ github.event.repository.default_branch }}"
else
echo "is_default=true" >> "$GITHUB_OUTPUT"
echo "default_branch=${{ github.event.repository.default_branch }}" >> "$GITHUB_OUTPUT"
fi
2021-04-25 09:16:05 +08:00
else
if [[ -z "$GITHUB_OUTPUT" ]]; then
echo "::set-output name=is_default::false"
echo "::set-output name=default_branch::${{ github.event.repository.default_branch }}"
else
echo "is_default=false" >> "$GITHUB_OUTPUT"
echo "default_branch=${{ github.event.repository.default_branch }}" >> "$GITHUB_OUTPUT"
fi
2021-04-25 09:16:05 +08:00
fi
2021-02-13 05:30:56 +08:00
fi
2021-04-24 18:35:28 +08:00
shell: bash
- id: tag
run: |
2022-08-22 00:59:36 +08:00
# "Set the tag name..."
2021-04-25 09:16:05 +08:00
if [[ "${{ github.ref }}" == "refs/tags/"* ]]; then
2021-09-15 01:38:39 +08:00
REF=$(printf "%q" "${{ github.ref }}")
2021-04-25 10:54:22 +08:00
TAG=${REF/refs\/tags\/${{ inputs.strip_tag_prefix }}/}
2021-09-15 01:38:39 +08:00
if [[ -z "$GITHUB_OUTPUT" ]]; then
echo "::set-output name=tag::$(eval printf "%s" "$TAG")"
echo "::set-output name=is_tag::true"
else
echo "tag=$(eval printf "%s" "$TAG")" >> "$GITHUB_OUTPUT"
echo "is_tag=true" >> "$GITHUB_OUTPUT"
fi
2021-05-27 20:24:26 +08:00
else
if [[ -z "$GITHUB_OUTPUT" ]]; then
echo "::set-output name=is_tag::false"
else
echo "is_tag=false" >> "$GITHUB_OUTPUT"
fi
2021-04-24 18:35:28 +08:00
fi
shell: bash
2021-02-08 08:39:26 +08:00
2020-12-11 21:15:40 +08:00
branding:
icon: git-branch
color: white