|
| 1 | +name: Tag, Release, & Publish |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + |
| 8 | +jobs: |
| 9 | + build: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - uses: actions/checkout@v4 |
| 13 | + |
| 14 | + - name: Setup Node.js |
| 15 | + uses: actions/setup-node@v4 |
| 16 | + with: |
| 17 | + node-version: 20 |
| 18 | + |
| 19 | + - name: Extract Changelog Entry |
| 20 | + id: extract_changelog |
| 21 | + shell: pwsh |
| 22 | + run: | |
| 23 | +
|
| 24 | + $version = (Get-Content package.json | ConvertFrom-Json).version |
| 25 | + Write-Host "Version: $version" |
| 26 | +
|
| 27 | + # Set the path to your changelog file |
| 28 | + $changelogFile = "CHANGELOG.md" |
| 29 | +
|
| 30 | + # Ensure the changelog file exists and read lines |
| 31 | + if (Test-Path $changelogFile) { |
| 32 | + $lines = Get-Content $changelogFile |
| 33 | + $changelogContent = "" |
| 34 | + $isCapturing = $false |
| 35 | +
|
| 36 | + # Capture changelog content for the specified version |
| 37 | + foreach ($line in $lines) { |
| 38 | + # Check if we match the version header |
| 39 | + if ($line -match "# \[$($version)\]") { |
| 40 | + $isCapturing = $true |
| 41 | + continue |
| 42 | + } |
| 43 | + if ($isCapturing) { |
| 44 | + $changelogContent += "$line`n" |
| 45 | + # Stop when another version header is found (match format like '# [1.0.0]') |
| 46 | + if ($line -match "# \[\d+\.\d+\.\d+\]") { |
| 47 | + break |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | +
|
| 52 | + # Clean up changelog content |
| 53 | + if ($changelogContent) { |
| 54 | + $changelogContent = $changelogContent.Trim() |
| 55 | + $changelogContent = $changelogContent -replace "# \[\d+\.\d+\.\d+\] - \d{4}-\d{2}-\d{2}\s*", "" |
| 56 | +
|
| 57 | + Write-Host "Changelog entry for version $version found!" |
| 58 | + Write-Host "Changelog content: $changelogContent" |
| 59 | +
|
| 60 | + # Store the changelog content in the GITHUB_ENV variable |
| 61 | + echo "changelog_content=$changelogContent" >> $env:GITHUB_OUTPUT |
| 62 | + } else { |
| 63 | + Write-Host "No changelog entry found for version $version." |
| 64 | + exit 1 |
| 65 | + } |
| 66 | + } else { |
| 67 | + Write-Host "Changelog file not found." |
| 68 | + exit 1 |
| 69 | + } |
| 70 | +
|
| 71 | + - name: Tag |
| 72 | + id: autotagger |
| 73 | + if: steps.check_changes.outputs.uncommitted == 'false' |
| 74 | + uses: Klemensas/action-autotag@stable |
| 75 | + with: |
| 76 | + tag_message: ${{ steps.extract_changelog.outputs.changelog_content }} |
| 77 | + env: |
| 78 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 79 | + |
| 80 | + - name: Release |
| 81 | + id: create_release |
| 82 | + if: steps.check_changes.outputs.uncommitted == 'false' && steps.autotagger.outputs.tagname != '' |
| 83 | + uses: actions/create-release@v1.0.0 |
| 84 | + env: |
| 85 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 86 | + with: |
| 87 | + tag_name: ${{ steps.autotagger.outputs.tagname }} |
| 88 | + release_name: ${{ steps.autotagger.outputs.tagname }} |
| 89 | + body: ${{ steps.extract_changelog.outputs.changelog_content }} |
| 90 | + draft: false |
| 91 | + |
| 92 | + - name: Publish to npm |
| 93 | + if: steps.check_changes.outputs.uncommitted == 'false' |
| 94 | + env: |
| 95 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 96 | + run: | |
| 97 | + npm publish --dry-run --access public |
0 commit comments