Created
February 1, 2024 00:58
-
-
Save rc1021/f2ec4db4e1a4cd8015b05e8da5255905 to your computer and use it in GitHub Desktop.
PR 合併後自動新增版本號
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Create Version Tag on PR Merge | |
on: | |
pull_request: | |
types: | |
- closed | |
jobs: | |
create_tag: | |
runs-on: ubuntu-latest | |
if: github.event.pull_request.merged == true && (startsWith(github.event.pull_request.head.ref, 'hotfix') || startsWith(github.event.pull_request.head.ref, 'feature')) | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Set up Git | |
run: git config user.email "[email protected]" && git config user.name "GitHub Actions" | |
- name: Fetch latest tags | |
run: git fetch --tags | |
- name: Determine New Version | |
id: version | |
run: | | |
branch_name="${{ github.event.pull_request.head.ref }}" | |
latest_tag=$(git describe --tags `git rev-list --tags --max-count=1`) | |
IFS='.' read -r -a version_parts <<< "$latest_tag" | |
major_version="${version_parts[0]}" | |
minor_version="${version_parts[1]}" | |
patch_version="${version_parts[2]}" | |
new_patch_version="${patch_version}" | |
new_minor_version="${minor_version}" | |
if [[ $branch_name == "hotfix"* ]]; then | |
new_patch_version=$((patch_version + 1)) | |
elif [[ $branch_name == "feature"* ]]; then | |
new_minor_version=$((minor_version + 1)) | |
new_patch_version="0" | |
fi | |
new_version="$major_version.$new_minor_version.$new_patch_version" | |
echo "new_version=$new_version" >> $GITHUB_OUTPUT | |
shell: bash | |
- name: Create and Push New Version Tag | |
run: | | |
TAG="${{ steps.version.outputs.new_version }}" | |
git tag $TAG | |
git push origin $TAG | |
shell: bash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment