|
| 1 | +# Summary: |
| 2 | +# Automatically tag and release when changes land on the "main" branch. |
| 3 | +# It uses "semantic-version" to resolve the next version to use, and then we use GitHub CLI to create or update the releases. |
| 4 | +# |
| 5 | +# See https://github.com/PaulHatch/semantic-version https://github.com/PaulHatch/semantic-version/tree/v5.0.2 |
| 6 | +# See https://github.com/softprops/action-gh-release https://github.com/softprops/action-gh-release/tree/v1 |
| 7 | + |
| 8 | +name: 'Auto release' |
| 9 | +on: |
| 10 | + push: |
| 11 | + branches: |
| 12 | + - main |
| 13 | + |
| 14 | +jobs: |
| 15 | + tag-and-release: |
| 16 | + runs-on: ubuntu-22.04 |
| 17 | + steps: |
| 18 | + - name: Fetching all commits for the current branch |
| 19 | + uses: actions/checkout@v3 |
| 20 | + with: |
| 21 | + fetch-depth: 0 # Force fetch all commits - See https://github.com/PaulHatch/semantic-version#important-note-regarding-the-checkout-action |
| 22 | + |
| 23 | + - run: "echo \"GITHUB_SHA: ${{ github.sha }}\"" |
| 24 | + |
| 25 | + # Outputs documentation: https://github.com/PaulHatch/semantic-version/blob/master/src/main.ts#L22-L33 |
| 26 | + - name: Resolving next Release Candidate version using semantic-version |
| 27 | + uses: paulhatch/semantic-version@v5.0.2 |
| 28 | + id: next_semantic_version |
| 29 | + with: # See https://github.com/PaulHatch/semantic-version#usage |
| 30 | + tag_prefix: "v" # The prefix to use to identify tags |
| 31 | + major_pattern: "(MAJOR)" # A string which, if present in a git commit, indicates that a change represents a major (breaking) change |
| 32 | + minor_pattern: "(MINOR)" # Same as above except indicating a minor change |
| 33 | + version_format: "${major}.${minor}.${patch}-rc.${increment}" # A string to determine the format of the version output |
| 34 | + bump_each_commit: true # If this input is set to true, every commit will be treated as a new version, bumping the patch, minor, or major version based on the commit message. |
| 35 | + |
| 36 | + - name: Printing semantic-version outputs (for debugging) |
| 37 | + run: | |
| 38 | + echo "Most useful outputs:" |
| 39 | + echo "Next version: ${{steps.next_semantic_version.outputs.version}}" |
| 40 | + echo "Next version tag: ${{steps.next_semantic_version.outputs.version_tag}}" |
| 41 | + echo -e "\n All outputs:" |
| 42 | + echo "version: ${{steps.next_semantic_version.outputs.version}}" |
| 43 | + echo "major: ${{steps.next_semantic_version.outputs.major}}" |
| 44 | + echo "minor: ${{steps.next_semantic_version.outputs.minor}}" |
| 45 | + echo "patch: ${{steps.next_semantic_version.outputs.patch}}" |
| 46 | + echo "increment: ${{steps.next_semantic_version.outputs.increment}}" |
| 47 | + echo "version_type: ${{steps.next_semantic_version.outputs.version_type}}" |
| 48 | + echo "changed: ${{steps.next_semantic_version.outputs.changed}}" |
| 49 | + echo "authors: ${{steps.next_semantic_version.outputs.authors}}" |
| 50 | + echo "version_tag: ${{steps.next_semantic_version.outputs.version_tag}}" |
| 51 | + echo "previous_commit: ${{steps.next_semantic_version.outputs.previous_commit}}" |
| 52 | + echo "current_commit: ${{steps.next_semantic_version.outputs.current_commit}}" |
| 53 | +
|
| 54 | + - name: Creating Git release tag for the "${{steps.next_semantic_version.outputs.version_tag}}" version |
| 55 | + run: | |
| 56 | + gh release create ${{steps.next_semantic_version.outputs.version_tag}} \ |
| 57 | + --title "${{steps.next_semantic_version.outputs.version_tag}}" \ |
| 58 | + --latest \ |
| 59 | + --generate-notes \ |
| 60 | + --target "${{github.sha}}" |
| 61 | + env: |
| 62 | + GH_TOKEN: "${{ secrets.GITHUB_TOKEN }}" |
| 63 | + |
| 64 | + # Check if the major version already exists (if it doesn't, we'll create it - if it does, we'll update it) |
| 65 | + - name: Check if tag "v${{steps.next_semantic_version.outputs.major}}" exists |
| 66 | + uses: mukunku/tag-exists-action@v1.2.0 |
| 67 | + id: majorTagExists |
| 68 | + with: # See https://github.com/mukunku/tag-exists-action#inputs |
| 69 | + tag: "v${{steps.next_semantic_version.outputs.major}}" |
| 70 | + |
| 71 | + - run: "echo \"Check if majorTagExists: ${{ steps.majorTagExists.outputs.exists }}\"" |
| 72 | + |
| 73 | + # See https://cli.github.com/manual/gh_release_create |
| 74 | + - name: Creating new release for the major "v${{steps.next_semantic_version.outputs.major}}" version |
| 75 | + if: ${{ steps.majorTagExists.outputs.exists == 'false' }} |
| 76 | + run: | |
| 77 | + gh release create v${{steps.next_semantic_version.outputs.major}} \ |
| 78 | + --title "v${{steps.next_semantic_version.outputs.major}} MAJOR release (auto-updated)" \ |
| 79 | + --generate-notes \ |
| 80 | + --target "${{github.sha}}" |
| 81 | + env: |
| 82 | + GH_TOKEN: "${{ secrets.GITHUB_TOKEN }}" |
| 83 | + |
| 84 | + # See https://cli.github.com/manual/gh_release_edit |
| 85 | + - name: Recreating existing release for the major "v${{steps.next_semantic_version.outputs.major}}" version |
| 86 | + if: ${{ steps.majorTagExists.outputs.exists == 'true' }} |
| 87 | + run: | |
| 88 | + # Delete and create the release again |
| 89 | + gh release delete v${{steps.next_semantic_version.outputs.major}} --cleanup-tag --yes |
| 90 | + |
| 91 | + gh release create v${{steps.next_semantic_version.outputs.major}} \ |
| 92 | + --title "v${{steps.next_semantic_version.outputs.major}} MAJOR release (auto-updated)" \ |
| 93 | + --generate-notes \ |
| 94 | + --target "${{github.sha}}" |
| 95 | + env: |
| 96 | + GH_TOKEN: "${{ secrets.GITHUB_TOKEN }}" |
| 97 | + |
| 98 | + # Check if the minor version already exists (if it doesn't, we'll create it - if it does, we'll update it) |
| 99 | + - name: Check if tag "v${{steps.next_semantic_version.outputs.major}}.${{steps.next_semantic_version.outputs.minor}}" exists |
| 100 | + uses: mukunku/tag-exists-action@v1.2.0 |
| 101 | + id: minorTagExists |
| 102 | + with: # See https://github.com/mukunku/tag-exists-action#inputs |
| 103 | + tag: "v${{steps.next_semantic_version.outputs.major}}.${{steps.next_semantic_version.outputs.minor}}" |
| 104 | + |
| 105 | + - run: "echo \"Check if minorTagExists: ${{ steps.minorTagExists.outputs.exists }}\"" |
| 106 | + |
| 107 | + # See https://cli.github.com/manual/gh_release_create |
| 108 | + - name: Creating new release for the minor "v${{steps.next_semantic_version.outputs.major}}.${{steps.next_semantic_version.outputs.minor}}" version |
| 109 | + if: ${{ steps.minorTagExists.outputs.exists == 'false' }} |
| 110 | + run: | |
| 111 | + gh release create v${{steps.next_semantic_version.outputs.major}}.${{steps.next_semantic_version.outputs.minor}} \ |
| 112 | + --title "v${{steps.next_semantic_version.outputs.major}}.${{steps.next_semantic_version.outputs.minor}} MINOR release (auto-updated)" \ |
| 113 | + --generate-notes \ |
| 114 | + --target "${{github.sha}}" |
| 115 | + env: |
| 116 | + GH_TOKEN: "${{ secrets.GITHUB_TOKEN }}" |
| 117 | + |
| 118 | + # See https://cli.github.com/manual/gh_release_edit |
| 119 | + - name: Recreating existing release for the minor "v${{steps.next_semantic_version.outputs.major}}.${{steps.next_semantic_version.outputs.minor}}" version |
| 120 | + if: ${{ steps.minorTagExists.outputs.exists == 'true' }} |
| 121 | + run: | |
| 122 | + # Delete and create the release again |
| 123 | + gh release delete v${{steps.next_semantic_version.outputs.major}}.${{steps.next_semantic_version.outputs.minor}} --cleanup-tag --yes |
| 124 | + |
| 125 | + gh release create v${{steps.next_semantic_version.outputs.major}}.${{steps.next_semantic_version.outputs.minor}} \ |
| 126 | + --title "v${{steps.next_semantic_version.outputs.major}}.${{steps.next_semantic_version.outputs.minor}} MINOR release (auto-updated)" \ |
| 127 | + --generate-notes \ |
| 128 | + --target "${{github.sha}}" |
| 129 | + env: |
| 130 | + GH_TOKEN: "${{ secrets.GITHUB_TOKEN }}" |
0 commit comments