|
| 1 | +name: Create Release from Changelog |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + dry_run: |
| 7 | + description: 'Dry run (do not create tag/release)' |
| 8 | + required: false |
| 9 | + type: boolean |
| 10 | + default: false |
| 11 | + |
| 12 | +jobs: |
| 13 | + create-release-from-changelog: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + permissions: |
| 16 | + contents: write |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: Checkout repository |
| 20 | + uses: actions/checkout@v4 |
| 21 | + with: |
| 22 | + fetch-depth: 0 |
| 23 | + |
| 24 | + - name: Extract version and changelog |
| 25 | + id: extract |
| 26 | + run: | |
| 27 | + # Extract the latest version from CHANGELOG.md |
| 28 | + VERSION=$(grep -m 1 -E "^## [0-9]+\.[0-9]+\.[0-9]+" CHANGELOG.md | sed -E 's/^## ([0-9]+\.[0-9]+\.[0-9]+).*/\1/') |
| 29 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 30 | + echo "tag=v$VERSION" >> $GITHUB_OUTPUT |
| 31 | +
|
| 32 | + # Extract changelog content for the latest version |
| 33 | + CHANGELOG_CONTENT=$(awk '/^## [0-9]+\.[0-9]+\.[0-9]+/{if(++count==1){flag=1;next}else{flag=0}}flag' CHANGELOG.md) |
| 34 | +
|
| 35 | + # Save changelog to a temporary file |
| 36 | + echo "$CHANGELOG_CONTENT" > /tmp/release_notes.md |
| 37 | +
|
| 38 | + echo "Extracted version: $VERSION" |
| 39 | + echo "Release notes:" |
| 40 | + cat /tmp/release_notes.md |
| 41 | +
|
| 42 | + - name: Check if tag exists |
| 43 | + id: check_tag |
| 44 | + run: | |
| 45 | + TAG="${{ steps.extract.outputs.tag }}" |
| 46 | + if git rev-parse "$TAG" >/dev/null 2>&1; then |
| 47 | + echo "exists=true" >> $GITHUB_OUTPUT |
| 48 | + echo "Tag $TAG already exists" |
| 49 | + else |
| 50 | + echo "exists=false" >> $GITHUB_OUTPUT |
| 51 | + echo "Tag $TAG does not exist" |
| 52 | + fi |
| 53 | +
|
| 54 | + - name: Create and push tag |
| 55 | + if: steps.check_tag.outputs.exists == 'false' && inputs.dry_run == false |
| 56 | + run: | |
| 57 | + TAG="${{ steps.extract.outputs.tag }}" |
| 58 | + git config user.name "github-actions[bot]" |
| 59 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 60 | + git tag -a "$TAG" -m "Release $TAG" |
| 61 | + git push origin "$TAG" |
| 62 | + echo "Created and pushed tag: $TAG" |
| 63 | +
|
| 64 | + - name: Create GitHub Release |
| 65 | + if: steps.check_tag.outputs.exists == 'false' && inputs.dry_run == false |
| 66 | + env: |
| 67 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 68 | + run: | |
| 69 | + TAG="${{ steps.extract.outputs.tag }}" |
| 70 | + VERSION="${{ steps.extract.outputs.version }}" |
| 71 | +
|
| 72 | + gh release create "$TAG" \ |
| 73 | + --title "Release $VERSION" \ |
| 74 | + --notes-file /tmp/release_notes.md |
| 75 | +
|
| 76 | + echo "Created GitHub release: $TAG" |
| 77 | +
|
| 78 | + - name: Dry run summary |
| 79 | + if: inputs.dry_run == true |
| 80 | + run: | |
| 81 | + echo "=== DRY RUN MODE ===" |
| 82 | + echo "Version: ${{ steps.extract.outputs.version }}" |
| 83 | + echo "Tag: ${{ steps.extract.outputs.tag }}" |
| 84 | + echo "Tag exists: ${{ steps.check_tag.outputs.exists }}" |
| 85 | + echo "" |
| 86 | + echo "Release notes:" |
| 87 | + cat /tmp/release_notes.md |
| 88 | +
|
| 89 | + - name: Skip (tag already exists) |
| 90 | + if: steps.check_tag.outputs.exists == 'true' |
| 91 | + run: | |
| 92 | + echo "Tag ${{ steps.extract.outputs.tag }} already exists. Skipping release creation." |
| 93 | + echo "If you want to recreate the release, please delete the existing tag first." |
0 commit comments