diff --git a/.github/scripts/pack-on-pull-request.ps1 b/.github/scripts/pack-on-pull-request.ps1 new file mode 100644 index 0000000..2dd166a --- /dev/null +++ b/.github/scripts/pack-on-pull-request.ps1 @@ -0,0 +1,11 @@ +#!/usr/local/bin/pwsh +param ( + [string]$OutputPath, + [int]$PullRequestNumber, + [string]$KeyPath +) + +.github/scripts/pack.ps1 ` + -OutputPath $OutputPath ` + -PullRequestNumber $PullRequestNumber ` + -KeyPath $KeyPath diff --git a/.github/scripts/pack-on-push-main.ps1 b/.github/scripts/pack-on-push-main.ps1 new file mode 100644 index 0000000..5e72d20 --- /dev/null +++ b/.github/scripts/pack-on-push-main.ps1 @@ -0,0 +1,29 @@ +#!/usr/local/bin/pwsh +param ( + [string]$OutputPath, + [string]$KeyPath, + [string]$NugetApiKey +) + +$commitMessage = "$(git log -1 --pretty=%B)" +$pattern = "(?<=Merge pull request #)(\d+)" +if (!($commitMessage -match $pattern)) { + Write-Error "Commit message does not contain a pull request number." +} + +Remove-Item -Force -Recurse pack -ErrorAction SilentlyContinue + +$commitSHA = "$(git log -1 --pretty=%H)" +.github/scripts/pack.ps1 ` + -OutputPath $OutputPath ` + -PullRequestNumber $matches[1] ` + -KeyPath $KeyPath ` + -CommitSHA $commitSHA + +Get-ChildItem -Path $OutputPath -Filter "*.nupkg" | ForEach-Object { + dotnet nuget push ` + $_ ` + --skip-duplicate ` + --api-key $NugetApiKey ` + --source https://api.nuget.org/v3/index.json +} diff --git a/.github/scripts/pack-on-push-tag.ps1 b/.github/scripts/pack-on-push-tag.ps1 new file mode 100644 index 0000000..f63b1a8 --- /dev/null +++ b/.github/scripts/pack-on-push-tag.ps1 @@ -0,0 +1,24 @@ +#!/usr/local/bin/pwsh +param ( + [string]$OutputPath, + [string]$KeyPath, + [string]$NugetApiKey, + [string]$tagName +) + +Remove-Item -Force -Recurse pack -ErrorAction SilentlyContinue + +.github/scripts/pack.ps1 ` + -OutputPath $OutputPath ` + -KeyPath $KeyPath ` + -CommitSHA $tagName + +$files = Get-ChildItem -Path pack -Filter "*.nupkg" | ForEach-Object { + dotnet nuget push ` + $_ ` + --api-key $NugetApiKey ` + --source https://api.nuget.org/v3/index.json + $_.FullName +} + +gh release create --generate-notes --latest --title "Release $tagName" $tagName $files diff --git a/.github/workflows/pack.yml b/.github/workflows/pack.yml index 662887a..f10d307 100644 --- a/.github/workflows/pack.yml +++ b/.github/workflows/pack.yml @@ -19,33 +19,24 @@ jobs: - run: echo "${{ secrets.SNK_FILE }}" | base64 --decode > private.snk - if: ${{ github.event_name == 'pull_request' }} run: | - .github/scripts/pack.ps1 ` + .github/scripts/pack-on-pull-request.ps1 ` -OutputPath "pack" ` -PullRequestNumber ${{ github.event.pull_request.number }} ` -KeyPath "$pwd/private.snk" shell: pwsh - if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} run: | - $commitMessage = "$(git log -1 --pretty=%B)" - $pattern = "(?<=Merge pull request #)(\d+)" - if (!($commitMessage -match $pattern)) { - Write-Error "Commit message does not contain a pull request number." - } - - Remove-Item -Force -Recurse pack -ErrorAction SilentlyContinue - - $commitSHA = "$(git log -1 --pretty=%H)" - .github/scripts/pack.ps1 ` + .github/scripts/pack-on-push-main.ps1 ` + -OutputPath "pack" ` + -KeyPath "$pwd/private.snk" ` + -NugetApiKey ${{ secrets.NUGET_API_KEY }} + shell: pwsh + - if: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') }} + run: | + $tagName = "${{ github.ref }}" -replace "refs/tags/", "" + .github/scripts/pack-on-push-tag.ps1 ` -OutputPath "pack" ` - -PullRequestNumber $matches[1] ` -KeyPath "$pwd/private.snk" ` - -CommitSHA $commitSHA - - Get-ChildItem -Path pack -Filter "*.nupkg" | ForEach-Object { - dotnet nuget push ` - $_ ` - --skip-duplicate ` - --api-key ${{ secrets.NUGET_API_KEY }} ` - --source https://api.nuget.org/v3/index.json - } + -NugetApiKey ${{ secrets.NUGET_API_KEY }} + -TagName $tagName shell: pwsh diff --git a/CHANGES.md b/CHANGES.md index c57421e..0fc0711 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,10 +9,12 @@ To be released. * Improve CI to make sure minimal checks are done before merge [[#26]] * Set actual sha to PackageProjectUrl property [[#27]] * Change json library to System.Text.Json [[#28]] +* Add github action on push tag [[#31]] [#26]: https://github.com/s2quake/communication/pull/26 [#27]: https://github.com/s2quake/communication/pull/27 [#28]: https://github.com/s2quake/communication/pull/28 +[#31]: https://github.com/s2quake/communication/pull/31 2.0.2