|
| 1 | +# Creates a GitHub release and tag from a version trigger file. |
| 2 | +# Triggered when .release-trigger is pushed to any branch. |
| 3 | +name: Create Release |
| 4 | + |
| 5 | +on: |
| 6 | + push: |
| 7 | + paths: |
| 8 | + - '.release-trigger' |
| 9 | + workflow_dispatch: |
| 10 | + inputs: |
| 11 | + version: |
| 12 | + description: 'Version tag to create (e.g. v3.0.0)' |
| 13 | + required: true |
| 14 | + default: 'v3.0.0' |
| 15 | + |
| 16 | +permissions: |
| 17 | + contents: write |
| 18 | + |
| 19 | +jobs: |
| 20 | + release: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + steps: |
| 23 | + - uses: actions/checkout@v4 |
| 24 | + with: |
| 25 | + fetch-depth: 0 |
| 26 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 27 | + |
| 28 | + - name: Determine version |
| 29 | + id: version |
| 30 | + run: | |
| 31 | + if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then |
| 32 | + echo "tag=${{ inputs.version }}" >> $GITHUB_OUTPUT |
| 33 | + else |
| 34 | + VER=$(node -e "process.stdout.write(require('./package.json').version)") |
| 35 | + echo "tag=v${VER}" >> $GITHUB_OUTPUT |
| 36 | + fi |
| 37 | +
|
| 38 | + - name: Read release notes |
| 39 | + id: notes |
| 40 | + run: | |
| 41 | + TAG="${{ steps.version.outputs.tag }}" |
| 42 | + VERSION="${TAG#v}" |
| 43 | + NOTES_FILE="RELEASE_v${VERSION}.md" |
| 44 | + if [ -f "$NOTES_FILE" ]; then |
| 45 | + BODY=$(cat "$NOTES_FILE") |
| 46 | + else |
| 47 | + BODY="Release $TAG" |
| 48 | + fi |
| 49 | + # Write to file to avoid quoting issues |
| 50 | + echo "$BODY" > /tmp/release_notes.txt |
| 51 | + echo "notes_file=/tmp/release_notes.txt" >> $GITHUB_OUTPUT |
| 52 | +
|
| 53 | + - name: Create tag and release |
| 54 | + env: |
| 55 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 56 | + run: | |
| 57 | + TAG="${{ steps.version.outputs.tag }}" |
| 58 | + SHA="${{ github.sha }}" |
| 59 | +
|
| 60 | + # Create annotated tag via API |
| 61 | + git config user.name "github-actions[bot]" |
| 62 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 63 | +
|
| 64 | + # Check if tag already exists |
| 65 | + if git ls-remote --tags origin "$TAG" | grep -q "$TAG"; then |
| 66 | + echo "Tag $TAG already exists, skipping tag creation" |
| 67 | + else |
| 68 | + git tag -a "$TAG" -m "Release $TAG" "$SHA" |
| 69 | + git push origin "$TAG" |
| 70 | + echo "Tag $TAG created and pushed" |
| 71 | + fi |
| 72 | +
|
| 73 | + # Create GitHub release |
| 74 | + gh release create "$TAG" \ |
| 75 | + --title "Release $TAG" \ |
| 76 | + --notes-file "${{ steps.notes.outputs.notes_file }}" \ |
| 77 | + --target "${{ github.ref_name }}" \ |
| 78 | + || echo "Release may already exist" |
0 commit comments