Skip to content

Commit

Permalink
ci: Add pack action.
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Jun 29, 2024
1 parent 523e0cb commit ef413e2
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .github/scripts/pack.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/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 " ")"
49 changes: 49 additions & 0 deletions .github/workflows/pack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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)) {
Write-Error "Commit message does not contain a pull request number."
}

Remove-Item -Force -Recurse pack -ErrorAction SilentlyContinue

.github/scripts/pack.ps1 `
-OutputPath "pack" `
-PullRequestNumber $matches[1] `
-KeyPath "$pwd/private.snk"

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
}
shell: pwsh

0 comments on commit ef413e2

Please sign in to comment.