diff --git a/.github/scripts/pack-on-pull-request.ps1 b/.github/scripts/pack-on-pull-request.ps1 index 2dd166a..097991a 100644 --- a/.github/scripts/pack-on-pull-request.ps1 +++ b/.github/scripts/pack-on-pull-request.ps1 @@ -1,11 +1,18 @@ #!/usr/local/bin/pwsh param ( + [Parameter(Mandatory = $true)] [string]$OutputPath, - [int]$PullRequestNumber, - [string]$KeyPath + [Parameter(Mandatory = $true)] + [int]$PullRequestNumber ) +Get-childItem Env: + +if (!$Env:PRIVATE_KEY_PATH) { + Write-Error "Environment variable 'PRIVATE_KEY_PATH' is not set." +} + .github/scripts/pack.ps1 ` -OutputPath $OutputPath ` -PullRequestNumber $PullRequestNumber ` - -KeyPath $KeyPath + -KeyPath $Env:PRIVATE_KEY_PATH diff --git a/.github/scripts/pack-on-push-main.ps1 b/.github/scripts/pack-on-push-main.ps1 index 5e72d20..81da070 100644 --- a/.github/scripts/pack-on-push-main.ps1 +++ b/.github/scripts/pack-on-push-main.ps1 @@ -1,29 +1,50 @@ #!/usr/local/bin/pwsh param ( + [Parameter(Mandatory = $true)] [string]$OutputPath, - [string]$KeyPath, - [string]$NugetApiKey + [Parameter(Mandatory = $true)] + [ValidateScript({ $_ })] + [string]$CommitSHA ) -$commitMessage = "$(git log -1 --pretty=%B)" +if (!$Env:PRIVATE_KEY_PATH) { + Write-Error "Environment variable 'PRIVATE_KEY_PATH' is not set." +} + +if (!$Env:NUGET_API_KEY) { + Write-Error "Environment variable 'NUGET_API_KEY' is not set." +} + +$commitMessage = "$(git log $CommitSHA -1 --pretty=%B)" $pattern = "(?<=Merge pull request #)(\d+)" if (!($commitMessage -match $pattern)) { Write-Error "Commit message does not contain a pull request number." } +$pullRequestNumber = $matches[1] -Remove-Item -Force -Recurse pack -ErrorAction SilentlyContinue +Remove-Item -Path $OutputPath -Force -Recurse -ErrorAction SilentlyContinue -$commitSHA = "$(git log -1 --pretty=%H)" .github/scripts/pack.ps1 ` -OutputPath $OutputPath ` - -PullRequestNumber $matches[1] ` - -KeyPath $KeyPath ` - -CommitSHA $commitSHA + -PullRequestNumber $pullRequestNumber ` + -KeyPath $Env:PRIVATE_KEY_PATH ` + -CommitSHA $CommitSHA + +$labelsValue = gh pr view $pullRequestNumber --json labels | ConvertFrom-Json -AsHashtable +$labels = $labelsValue | Select-Object -ExpandProperty labels | ForEach-Object { + $_.name +} +$skipNugetUpload = $labels -contains "skip nuget upload" +if ($skipNugetUpload) { + Write-Host "Skipping NuGet upload" +} -Get-ChildItem -Path $OutputPath -Filter "*.nupkg" | ForEach-Object { +$nupkgs = Get-ChildItem -Path $OutputPath -Filter "*.nupkg" | ForEach-Object { $_.FullName } +$nupkgs = $skipNugetUpload ? @() : $nupkgs +$nupkgs | ForEach-Object { dotnet nuget push ` $_ ` --skip-duplicate ` - --api-key $NugetApiKey ` + --api-key $Env:NUGET_API_KEY ` --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 index f63b1a8..512692e 100644 --- a/.github/scripts/pack-on-push-tag.ps1 +++ b/.github/scripts/pack-on-push-tag.ps1 @@ -1,22 +1,28 @@ #!/usr/local/bin/pwsh param ( [string]$OutputPath, - [string]$KeyPath, - [string]$NugetApiKey, [string]$tagName ) -Remove-Item -Force -Recurse pack -ErrorAction SilentlyContinue +if (!$Env:PRIVATE_KEY_PATH) { + Write-Error "Environment variable 'PRIVATE_KEY_PATH' is not set." +} + +if (!$Env:NUGET_API_KEY) { + Write-Error "Environment variable 'NUGET_API_KEY' is not set." +} + +Remove-Item -Path $OutputPath -Force -Recurse -ErrorAction SilentlyContinue .github/scripts/pack.ps1 ` -OutputPath $OutputPath ` - -KeyPath $KeyPath ` + -KeyPath $Env:PRIVATE_KEY_PATH ` -CommitSHA $tagName $files = Get-ChildItem -Path pack -Filter "*.nupkg" | ForEach-Object { dotnet nuget push ` $_ ` - --api-key $NugetApiKey ` + --api-key $Env:NUGET_API_KEY ` --source https://api.nuget.org/v3/index.json $_.FullName } diff --git a/.github/workflows/pack.yml b/.github/workflows/pack.yml index f10d307..6944d0a 100644 --- a/.github/workflows/pack.yml +++ b/.github/workflows/pack.yml @@ -16,13 +16,14 @@ jobs: - uses: actions/setup-dotnet@v4.0.0 with: dotnet-version: 8.0.100 - - run: echo "${{ secrets.SNK_FILE }}" | base64 --decode > private.snk + - run: | + echo "${{ secrets.SNK_FILE }}" | base64 --decode > private.snk + echo "PRIVATE_KEY_PATH=$pwd/private.snk" >> $GITHUB_ENV - if: ${{ github.event_name == 'pull_request' }} run: | .github/scripts/pack-on-pull-request.ps1 ` -OutputPath "pack" ` - -PullRequestNumber ${{ github.event.pull_request.number }} ` - -KeyPath "$pwd/private.snk" + -PullRequestNumber ${{ github.event.pull_request.number }} shell: pwsh - if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} run: | @@ -31,6 +32,8 @@ jobs: -KeyPath "$pwd/private.snk" ` -NugetApiKey ${{ secrets.NUGET_API_KEY }} shell: pwsh + env: + GH_TOKEN: ${{ github.token }} - if: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') }} run: | $tagName = "${{ github.ref }}" -replace "refs/tags/", "" @@ -40,3 +43,5 @@ jobs: -NugetApiKey ${{ secrets.NUGET_API_KEY }} -TagName $tagName shell: pwsh + env: + GH_TOKEN: ${{ github.token }} diff --git a/.vscode/launch.json b/.vscode/launch.json index 1eab89e..db5e0b9 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -25,16 +25,6 @@ "cwd": "${workspaceFolder}/src/JSSoft.Communication.Server/bin/Debug/net8.0", "console": "integratedTerminal", "stopAtEntry": false - }, - { - "name": "pwsh-build", - "type": "PowerShell", - "request": "launch", - "script": "${workspaceFolder}/build.ps1", - "args": [ - "-Pack" - ], - "cwd": "${cwd}" } ] } \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index cad5ced..ecbbe63 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,6 +4,11 @@ 100 ] }, + "[powershell]": { + "editor.rulers": [ + 100 + ] + }, "files.exclude": { ".vs": true, "**/bin": true,