|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - 'v*' |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + release: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Checkout code |
| 16 | + uses: actions/checkout@v6 |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + |
| 20 | + - name: Extract tag name |
| 21 | + id: tag |
| 22 | + run: echo "TAG_NAME=${GITHUB_REF#refs/tags/}" >> "$GITHUB_OUTPUT" |
| 23 | + |
| 24 | + - name: Extract changelog for this version |
| 25 | + id: changelog |
| 26 | + run: | |
| 27 | + # Extract the current version's changelog |
| 28 | + TAG="${{ steps.tag.outputs.TAG_NAME }}" |
| 29 | + TAG_WITHOUT_V="${TAG#v}" |
| 30 | +
|
| 31 | + # Create a temporary file for the changelog content |
| 32 | + CHANGELOG_FILE="changelog_content.md" |
| 33 | +
|
| 34 | + # Extract content between the current version and the next version header |
| 35 | + awk -v ver="$TAG_WITHOUT_V" ' |
| 36 | + BEGIN { found = 0; } |
| 37 | + $0 ~ "^#{2,3} \\[?" ver "\\]?" { found = 1; next; } |
| 38 | + /^#{2,3} \[?[0-9]+\.[0-9]+\.[0-9]+\]?/ { if (found) exit; } |
| 39 | + found { print; } |
| 40 | + ' CHANGELOG.md > "$CHANGELOG_FILE" |
| 41 | +
|
| 42 | + # If no content found with version without 'v', try with 'v' |
| 43 | + if [ ! -s "$CHANGELOG_FILE" ]; then |
| 44 | + awk -v ver="$TAG" ' |
| 45 | + BEGIN { found = 0; } |
| 46 | + $0 ~ "^#{2,3} \\[?" ver "\\]?" { found = 1; next; } |
| 47 | + /^#{2,3} \[?v?[0-9]+\.[0-9]+\.[0-9]+\]?/ { if (found) exit; } |
| 48 | + found { print; } |
| 49 | + ' CHANGELOG.md > "$CHANGELOG_FILE" |
| 50 | + fi |
| 51 | +
|
| 52 | + # If still no content, use a default message |
| 53 | + if [ ! -s "$CHANGELOG_FILE" ]; then |
| 54 | + echo "No changelog entry found for version $TAG" > "$CHANGELOG_FILE" |
| 55 | + echo "" |
| 56 | + echo "Please check CHANGELOG.md for updates." >> "$CHANGELOG_FILE" |
| 57 | + fi |
| 58 | +
|
| 59 | + # Set the output |
| 60 | + { |
| 61 | + echo 'CHANGELOG_CONTENT<<EOF' |
| 62 | + cat "$CHANGELOG_FILE" |
| 63 | + echo 'EOF' |
| 64 | + } >> "$GITHUB_OUTPUT" |
| 65 | +
|
| 66 | + - name: Create Release |
| 67 | + uses: softprops/action-gh-release@v2 |
| 68 | + with: |
| 69 | + tag_name: ${{ steps.tag.outputs.TAG_NAME }} |
| 70 | + name: ${{ steps.tag.outputs.TAG_NAME }} |
| 71 | + body: ${{ steps.changelog.outputs.CHANGELOG_CONTENT }} |
| 72 | + draft: false |
| 73 | + prerelease: ${{ contains(steps.tag.outputs.TAG_NAME, '-alpha') || contains(steps.tag.outputs.TAG_NAME, '-beta') || contains(steps.tag.outputs.TAG_NAME, '-rc') }} |
| 74 | + generate_release_notes: false |
| 75 | + env: |
| 76 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments