|
| 1 | +name: Create Release Tag |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + version: |
| 7 | + description: "Release version (e.g. 1.0.0)." |
| 8 | + required: true |
| 9 | + default: "1.0.0" |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + create-tag: |
| 16 | + name: Create and push tag |
| 17 | + runs-on: ubuntu-latest |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Checkout repository |
| 21 | + uses: actions/checkout@v4 |
| 22 | + with: |
| 23 | + fetch-depth: 0 |
| 24 | + |
| 25 | + - name: Validate and normalize version input |
| 26 | + id: normalize |
| 27 | + run: | |
| 28 | + set -euo pipefail |
| 29 | + raw_version="${{ github.event.inputs.version }}" |
| 30 | + if [ -z "$raw_version" ]; then |
| 31 | + echo "No version provided. Use workflow_dispatch input 'version'." >&2 |
| 32 | + exit 1 |
| 33 | + fi |
| 34 | + # strip any leading 'v' or 'V' |
| 35 | + normalized=$(echo "$raw_version" | sed -E 's/^[vV]//') |
| 36 | + if [ -z "$normalized" ]; then |
| 37 | + echo "Provided version '$raw_version' is invalid after stripping leading 'v' — please supply a semantic version like 1.2.3." >&2 |
| 38 | + exit 1 |
| 39 | + fi |
| 40 | + # simple semver-like warning (not fully strict), continue even if it doesn't match |
| 41 | + if ! echo "$normalized" | grep -E -q '^[0-9]+(\.[0-9]+){1,2}([-.+][0-9A-Za-z.-]+)?$'; then |
| 42 | + echo "Warning: version '$normalized' doesn't look like semver (X.Y or X.Y.Z). Continuing anyway." >&2 |
| 43 | + fi |
| 44 | + tag="v${normalized}" |
| 45 | + echo "Normalized version: $normalized" |
| 46 | + echo "tag=$tag" >> "$GITHUB_OUTPUT" |
| 47 | +
|
| 48 | + - name: Confirm tag does not already exist |
| 49 | + run: | |
| 50 | + tag=${{ steps.normalize.outputs.tag }} |
| 51 | + echo "Checking if tag $tag exists on origin..." |
| 52 | + if git ls-remote --tags origin | grep -q "refs/tags/$tag"; then |
| 53 | + echo "Tag $tag already exists on origin — aborting to avoid overwriting." >&2 |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | +
|
| 57 | + - name: Create annotated tag and push to origin |
| 58 | + env: |
| 59 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 60 | + run: | |
| 61 | + tag=${{ steps.normalize.outputs.tag }} |
| 62 | + git config user.name "github-actions[bot]" |
| 63 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 64 | + echo "Creating annotated tag $tag" |
| 65 | + git tag -a "$tag" -m "Release $tag" |
| 66 | + # Set remote URL that uses the token to push the new tag |
| 67 | + remote_url="https://x-access-token:${GITHUB_TOKEN}@github.com/${{ github.repository }}" |
| 68 | + git remote set-url origin "$remote_url" |
| 69 | + git push origin "$tag" |
| 70 | +
|
| 71 | + - name: Create GitHub release for the tag |
| 72 | + uses: actions/create-release@v1 |
| 73 | + with: |
| 74 | + tag_name: ${{ steps.normalize.outputs.tag }} |
| 75 | + release_name: "Release ${{ steps.normalize.outputs.tag }}" |
| 76 | + draft: false |
| 77 | + prerelease: false |
| 78 | + env: |
| 79 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments