|
| 1 | +name: Semantic Versioning |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + pull_request: |
| 8 | + branches: |
| 9 | + - main |
| 10 | + workflow_dispatch: |
| 11 | + inputs: |
| 12 | + bump_type: |
| 13 | + description: "Type of version bump (patch, minor, major)" |
| 14 | + required: true |
| 15 | + default: "patch" |
| 16 | + type: choice |
| 17 | + options: |
| 18 | + - patch |
| 19 | + - minor |
| 20 | + - major |
| 21 | + |
| 22 | +jobs: |
| 23 | + semver: |
| 24 | + runs-on: ubuntu-latest |
| 25 | + permissions: |
| 26 | + contents: write |
| 27 | + steps: |
| 28 | + - uses: actions/checkout@v3 |
| 29 | + with: |
| 30 | + fetch-depth: 0 |
| 31 | + |
| 32 | + - name: Get latest tag |
| 33 | + id: get_latest_tag |
| 34 | + run: | |
| 35 | + git fetch --tags |
| 36 | + latest_tag=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0") |
| 37 | + echo "LATEST_TAG=$latest_tag" >> $GITHUB_ENV |
| 38 | +
|
| 39 | + - name: Determine version bump |
| 40 | + id: version_bump |
| 41 | + run: | |
| 42 | + if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then |
| 43 | + # Use the manually specified bump type |
| 44 | + echo "BUMP_TYPE=${{ github.event.inputs.bump_type }}" >> $GITHUB_ENV |
| 45 | + else |
| 46 | + # Get commit messages since last tag |
| 47 | + commits=$(git log ${{ env.LATEST_TAG }}..HEAD --pretty=format:"%s") |
| 48 | +
|
| 49 | + # Initialize bump type |
| 50 | + bump_type="patch" # default to patch bump |
| 51 | +
|
| 52 | + # Check commit messages for keywords |
| 53 | + while IFS= read -r commit; do |
| 54 | + if [[ "$commit" == *"BREAKING CHANGE"* ]] || [[ "$commit" == *"!:"* ]] || [[ "$commit" == *"major:"* ]]; then |
| 55 | + bump_type="major" |
| 56 | + break |
| 57 | + elif [[ "$commit" == *"feat:"* ]] || [[ "$commit" == *"minor:"* ]]; then |
| 58 | + bump_type="minor" |
| 59 | + fi |
| 60 | + done <<< "$commits" |
| 61 | +
|
| 62 | + echo "BUMP_TYPE=$bump_type" >> $GITHUB_ENV |
| 63 | + fi |
| 64 | +
|
| 65 | + - name: Bump version |
| 66 | + id: bump_version |
| 67 | + run: | |
| 68 | + current_version=${LATEST_TAG#v} # Remove 'v' prefix |
| 69 | + IFS='.' read -r major minor patch <<< "$current_version" |
| 70 | +
|
| 71 | + case ${{ env.BUMP_TYPE }} in |
| 72 | + "major") |
| 73 | + new_version="$((major + 1)).0.0" |
| 74 | + ;; |
| 75 | + "minor") |
| 76 | + new_version="$major.$((minor + 1)).0" |
| 77 | + ;; |
| 78 | + *) |
| 79 | + new_version="$major.$minor.$((patch + 1))" |
| 80 | + ;; |
| 81 | + esac |
| 82 | +
|
| 83 | + echo "NEW_VERSION=v$new_version" >> $GITHUB_ENV |
| 84 | +
|
| 85 | + - name: Create and push tag |
| 86 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch' |
| 87 | + run: | |
| 88 | + git config --local user.email "action@github.com" |
| 89 | + git config --local user.name "GitHub Action" |
| 90 | + git tag -a ${{ env.NEW_VERSION }} -m "Release ${{ env.NEW_VERSION }}" |
| 91 | + git push origin ${{ env.NEW_VERSION }} |
| 92 | +
|
| 93 | + - name: Create release zip |
| 94 | + if: (github.event_name == 'push' && github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch') && (env.BUMP_TYPE == 'minor' || env.BUMP_TYPE == 'major') |
| 95 | + run: | |
| 96 | + mkdir -p release |
| 97 | + cp toolbox_script.py release/ |
| 98 | + cp README.md release/ |
| 99 | + cd release |
| 100 | + zip -r ../toolbox_script_${{ env.NEW_VERSION }}.zip . |
| 101 | + cd .. |
| 102 | +
|
| 103 | + - name: Create GitHub Release |
| 104 | + if: (github.event_name == 'push' && github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch') && (env.BUMP_TYPE == 'minor' || env.BUMP_TYPE == 'major') |
| 105 | + uses: softprops/action-gh-release@v1 |
| 106 | + with: |
| 107 | + files: toolbox_script_${{ env.NEW_VERSION }}.zip |
| 108 | + tag_name: ${{ env.NEW_VERSION }} |
| 109 | + name: "Release ${{ env.NEW_VERSION }}" |
| 110 | + draft: false |
| 111 | + prerelease: false |
| 112 | + token: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments