From 17f8576aafcedd41b86314c40dc37802b5554ab8 Mon Sep 17 00:00:00 2001 From: s2quake Date: Sat, 29 Jun 2024 15:53:35 +0900 Subject: [PATCH] ci: Add pack action. --- .github/scripts/pack.ps1 | 42 ++++++++++++++++++++++++++++++++++ .github/workflows/pack.yml | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100755 .github/scripts/pack.ps1 create mode 100644 .github/workflows/pack.yml diff --git a/.github/scripts/pack.ps1 b/.github/scripts/pack.ps1 new file mode 100755 index 0000000..b934b46 --- /dev/null +++ b/.github/scripts/pack.ps1 @@ -0,0 +1,42 @@ +#!/usr/local/bin/pwsh +param ( + [ValidateScript({ ($_ -eq "") -or !(Test-Path $_ -PathType Leaf) })] + [string]$OutputPath = "", + [ValidateScript({ $_ -ge 0 })] + [int]$PullRequestNumber = 0, + [ValidateScript({ ($_ -eq "") -or (Test-Path $_) })] + [string]$KeyPath = "" +) + +$namespaces = @{ + ns = "http://schemas.microsoft.com/developer/msbuild/2003" +} +$propsPath = "Directory.Build.props" +$fileVersionPath = "/ns:Project/ns:PropertyGroup/ns:FileVersion" +$result = Select-Xml -Path $propsPath -Namespace $namespaces -XPath $fileVersionPath +if ($null -eq $result) { + Write-Host "File version not found" + exit 1 +} + +$fileVersion = $result.Node.InnerText +$KeyPath = $KeyPath ? $(Resolve-Path -Path $KeyPath) : "" +$OutputPath = $OutputPath ? [System.IO.Path]::GetFullPath($OutputPath) : "" +$keyPathExits = Test-Path -Path $KeyPath + +Get-ChildItem -Path $OutputPath -Filter "*.nupkg" | ForEach-Object { + Remove-Item -Path $_.FullName +} + +$options = @( + $OutputPath ? "-o '$OutputPath'" : "" + "-p:FileVersion='$fileVersion'" + $PullRequestNumber ? "--version-suffix pr.$PullRequestNumber" : "" + $keyPathExits ? "-p:TreatWarningsAsErrors=true" : "" + $keyPathExits ? "-p:AssemblyOriginatorKeyFile='$KeyPath'" : "" +) | Where-Object { $_ } + +Invoke-Expression -Command "dotnet pack $($options -join " ")" | Out-Host +Get-ChildItem -Path $OutputPath -Filter "*.nupkg" | ForEach-Object { + $_.FullName +} diff --git a/.github/workflows/pack.yml b/.github/workflows/pack.yml new file mode 100644 index 0000000..cdca629 --- /dev/null +++ b/.github/workflows/pack.yml @@ -0,0 +1,46 @@ +name: Pack + +on: + pull_request: + push: + branches: + - main + tags: + - "*" + +jobs: + pack: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4.1.7 + - uses: actions/setup-dotnet@v4.0.0 + with: + dotnet-version: 8.0.100 + - run: echo "${{ secrets.SNK_FILE }}" | base64 --decode > private.snk + - if: ${{ github.event_name == 'pull_request' }} + run: | + .github/scripts/pack.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) { + $nupkgs = .github/scripts/pack.ps1 ` + -OutputPath "pack" ` + -PullRequestNumber $matches[1] ` + } else { + Write-Error "Commit message does not contain a pull request number." + } + + $nupkgs | ForEach-Object { + dotnet nuget push ` + $_ ` + --skip-duplicate ` + --api-key ${{ secrets.NUGET_API_KEY }} ` + --source https://api.nuget.org/v3/index.json + } + shell: pwsh