|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - "release/**" |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + package-and-pr: |
| 15 | + runs-on: windows-latest |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout |
| 19 | + uses: actions/checkout@v4 |
| 20 | + with: |
| 21 | + fetch-depth: 0 |
| 22 | + |
| 23 | + - name: Setup .NET |
| 24 | + uses: actions/setup-dotnet@v4 |
| 25 | + with: |
| 26 | + dotnet-version: 7.0.x |
| 27 | + |
| 28 | + - name: Install GitVersion tool |
| 29 | + shell: pwsh |
| 30 | + run: dotnet tool install --global GitVersion.Tool --version 5.12.0 |
| 31 | + |
| 32 | + - name: Add dotnet tools to PATH |
| 33 | + shell: pwsh |
| 34 | + run: echo "$env:USERPROFILE\.dotnet\tools" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append |
| 35 | + |
| 36 | + - name: Build release packages |
| 37 | + shell: pwsh |
| 38 | + run: .\build.ps1 |
| 39 | + |
| 40 | + - name: Upload release artifacts |
| 41 | + uses: actions/upload-artifact@v4 |
| 42 | + with: |
| 43 | + name: release-packages-${{ github.ref_name }} |
| 44 | + path: publish/*.zip |
| 45 | + if-no-files-found: error |
| 46 | + |
| 47 | + - name: Prepare release metadata |
| 48 | + id: release_meta |
| 49 | + shell: pwsh |
| 50 | + run: | |
| 51 | + $gitv = dotnet-gitversion | ConvertFrom-Json -ErrorAction Stop |
| 52 | + $tag = "v$($gitv.NuGetVersionV2)" |
| 53 | + $title = "Release $tag" |
| 54 | + $notesPath = Join-Path $env:GITHUB_WORKSPACE 'release-notes.md' |
| 55 | + $previousTag = git describe --tags --abbrev=0 --match "v*" 2>$null |
| 56 | + $commitRange = if ($previousTag) { "$previousTag..HEAD" } else { "HEAD" } |
| 57 | + $releaseDate = (Get-Date).ToString('yyyy-MM-dd') |
| 58 | + $compareUrl = if ($previousTag) { |
| 59 | + "https://github.com/${{ github.repository }}/compare/$previousTag...$tag" |
| 60 | + } |
| 61 | + else { |
| 62 | + "https://github.com/${{ github.repository }}/commits/$tag" |
| 63 | + } |
| 64 | +
|
| 65 | + $commits = if ($previousTag) { |
| 66 | + git log $commitRange --no-merges --reverse --pretty=format:"%s%x09%h" |
| 67 | + } |
| 68 | + else { |
| 69 | + git log --no-merges --reverse --pretty=format:"%s%x09%h" |
| 70 | + } |
| 71 | +
|
| 72 | + $buckets = @{ |
| 73 | + Added = New-Object System.Collections.Generic.List[string] |
| 74 | + Changed = New-Object System.Collections.Generic.List[string] |
| 75 | + Fixed = New-Object System.Collections.Generic.List[string] |
| 76 | + Removed = New-Object System.Collections.Generic.List[string] |
| 77 | + Security = New-Object System.Collections.Generic.List[string] |
| 78 | + } |
| 79 | +
|
| 80 | + foreach ($line in $commits) { |
| 81 | + if ([string]::IsNullOrWhiteSpace($line)) { |
| 82 | + continue |
| 83 | + } |
| 84 | +
|
| 85 | + $parts = $line -split "`t", 2 |
| 86 | + $subject = $parts[0].Trim() |
| 87 | + $hash = if ($parts.Count -gt 1) { $parts[1].Trim() } else { '' } |
| 88 | + $entry = if ($hash) { "- $subject ($hash)" } else { "- $subject" } |
| 89 | +
|
| 90 | + switch -Regex ($subject) { |
| 91 | + '^(feat|add)(\([^)]+\))?:\s+' { $buckets.Added.Add($entry); continue } |
| 92 | + '^(fix)(\([^)]+\))?:\s+' { $buckets.Fixed.Add($entry); continue } |
| 93 | + '^(remove|delete|drop)(\([^)]+\))?:\s+' { $buckets.Removed.Add($entry); continue } |
| 94 | + '^(sec|security)(\([^)]+\))?:\s+' { $buckets.Security.Add($entry); continue } |
| 95 | + '^(refactor|perf|docs|test|ci|build|chore|style)(\([^)]+\))?:\s+' { $buckets.Changed.Add($entry); continue } |
| 96 | + default { $buckets.Changed.Add($entry); continue } |
| 97 | + } |
| 98 | + } |
| 99 | +
|
| 100 | + $sectionLines = New-Object System.Collections.Generic.List[string] |
| 101 | +
|
| 102 | + foreach ($sectionName in @('Added', 'Changed', 'Fixed', 'Removed', 'Security')) { |
| 103 | + if ($buckets[$sectionName].Count -gt 0) { |
| 104 | + $sectionLines.Add("### $sectionName") |
| 105 | + $sectionLines.Add('') |
| 106 | + $sectionLines.AddRange($buckets[$sectionName]) |
| 107 | + $sectionLines.Add('') |
| 108 | + } |
| 109 | + } |
| 110 | +
|
| 111 | + if ($sectionLines.Count -eq 0) { |
| 112 | + $sectionLines.Add('### Changed') |
| 113 | + $sectionLines.Add('') |
| 114 | + $sectionLines.Add('- No notable changes were detected for this release.') |
| 115 | + $sectionLines.Add('') |
| 116 | + } |
| 117 | +
|
| 118 | + $releaseNotes = @( |
| 119 | + '# Release Notes' |
| 120 | + '' |
| 121 | + "## [$tag] - $releaseDate" |
| 122 | + '' |
| 123 | + '### Summary' |
| 124 | + '' |
| 125 | + "- Release branch: `$env:GITHUB_REF_NAME`" |
| 126 | + "- Version: `$($gitv.FullSemVer)`" |
| 127 | + "- SemVer: `$($gitv.SemVer)`" |
| 128 | + "- NuGetVersion: `$($gitv.NuGetVersionV2)`" |
| 129 | + "- Commit: `$($gitv.Sha)`" |
| 130 | + "- Base tag: `$($previousTag ?? 'none')`" |
| 131 | + "- Commit range: `$commitRange`" |
| 132 | + "- Compare: [$compareUrl]($compareUrl)" |
| 133 | + '' |
| 134 | + ) + $sectionLines + @( |
| 135 | + '' |
| 136 | + '## Packages' |
| 137 | + '' |
| 138 | + "- `DataverseGen-CLI_includedotnet_$($gitv.NuGetVersionV2).zip`" |
| 139 | + "- `DataverseGen-CLI_nodotnet_$($gitv.NuGetVersionV2).zip`" |
| 140 | + ) |
| 141 | +
|
| 142 | + $releaseNotes | Set-Content -Path $notesPath -Encoding utf8 |
| 143 | +
|
| 144 | + "tag=$tag" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append |
| 145 | + "title=$title" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append |
| 146 | + "notes_path=$notesPath" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append |
| 147 | +
|
| 148 | + - name: Check for existing pull request |
| 149 | + id: pr_check |
| 150 | + shell: pwsh |
| 151 | + env: |
| 152 | + GH_TOKEN: ${{ github.token }} |
| 153 | + run: | |
| 154 | + $prs = gh pr list --head $env:GITHUB_REF_NAME --base main --state open --json number | ConvertFrom-Json |
| 155 | + $exists = ($prs | Measure-Object).Count -gt 0 |
| 156 | + "exists=$exists" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append |
| 157 | +
|
| 158 | + - name: Create pull request to main |
| 159 | + if: steps.pr_check.outputs.exists != 'true' |
| 160 | + shell: pwsh |
| 161 | + env: |
| 162 | + GH_TOKEN: ${{ github.token }} |
| 163 | + run: | |
| 164 | + gh pr create ` |
| 165 | + --base main ` |
| 166 | + --head $env:GITHUB_REF_NAME ` |
| 167 | + --title "${{ steps.release_meta.outputs.title }}" ` |
| 168 | + --body-file "${{ steps.release_meta.outputs.notes_path }}" |
| 169 | +
|
| 170 | + - name: Publish GitHub release |
| 171 | + shell: pwsh |
| 172 | + env: |
| 173 | + GH_TOKEN: ${{ github.token }} |
| 174 | + run: | |
| 175 | + $tag = "${{ steps.release_meta.outputs.tag }}" |
| 176 | + $title = "${{ steps.release_meta.outputs.title }}" |
| 177 | + $notesPath = "${{ steps.release_meta.outputs.notes_path }}" |
| 178 | + $releaseExists = $false |
| 179 | +
|
| 180 | + gh release view $tag --json tagName | Out-Null |
| 181 | + if ($LASTEXITCODE -eq 0) { |
| 182 | + $releaseExists = $true |
| 183 | + } |
| 184 | +
|
| 185 | + if ($releaseExists) { |
| 186 | + gh release edit $tag --title $title --notes-file $notesPath |
| 187 | + gh release upload $tag publish/*.zip --clobber |
| 188 | + } |
| 189 | + else { |
| 190 | + gh release create $tag publish/*.zip --title $title --notes-file $notesPath --target $env:GITHUB_SHA |
| 191 | + } |
0 commit comments