From 7d622afbd0abc79359a72c2d353524cb96d8ec5e Mon Sep 17 00:00:00 2001
From: s2quake <han0210@netsgo.com>
Date: Sun, 30 Jun 2024 12:54:29 +0900
Subject: [PATCH] ci: Add action on push tag.

---
 .github/scripts/pack-on-pull-request.ps1 | 11 ++++++++
 .github/scripts/pack-on-push-main.ps1    | 29 +++++++++++++++++++++
 .github/scripts/pack-on-push-tag.ps1     | 24 +++++++++++++++++
 .github/workflows/pack.yml               | 33 +++++++++---------------
 4 files changed, 76 insertions(+), 21 deletions(-)
 create mode 100644 .github/scripts/pack-on-pull-request.ps1
 create mode 100644 .github/scripts/pack-on-push-main.ps1
 create mode 100644 .github/scripts/pack-on-push-tag.ps1

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..e25ead8
--- /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