|
| 1 | +param( |
| 2 | +[string] |
| 3 | +$ModulePath, |
| 4 | + |
| 5 | +# The user email associated with a git commit. |
| 6 | +[string] |
| 7 | +$UserEmail, |
| 8 | + |
| 9 | +# The user name associated with a git commit. |
| 10 | +[string] |
| 11 | +$UserName, |
| 12 | + |
| 13 | +# The tag version format (default value: 'v$(imported.Version)') |
| 14 | +# This can expand variables. $imported will contain the imported module. |
| 15 | +[string] |
| 16 | +$TagVersionFormat = 'v$($imported.Version)' |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +$gitHubEvent = if ($env:GITHUB_EVENT_PATH) { |
| 21 | + [IO.File]::ReadAllText($env:GITHUB_EVENT_PATH) | ConvertFrom-Json |
| 22 | +} else { $null } |
| 23 | + |
| 24 | + |
| 25 | +@" |
| 26 | +::group::GitHubEvent |
| 27 | +$($gitHubEvent | ConvertTo-Json -Depth 100) |
| 28 | +::endgroup:: |
| 29 | +"@ | Out-Host |
| 30 | + |
| 31 | +if (-not ($gitHubEvent.head_commit.message -match "Merge Pull Request #(?<PRNumber>\d+)") -and |
| 32 | + (-not $gitHubEvent.psobject.properties['inputs'])) { |
| 33 | + "::warning::Pull Request has not merged, skipping GitHub release" | Out-Host |
| 34 | + return |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +$imported = |
| 40 | +if (-not $ModulePath) { |
| 41 | + $orgName, $moduleName = $env:GITHUB_REPOSITORY -split "/" |
| 42 | + Import-Module ".\$moduleName.psd1" -Force -PassThru -Global |
| 43 | +} else { |
| 44 | + Import-Module $modulePath -Force -PassThru -Global |
| 45 | +} |
| 46 | + |
| 47 | +if (-not $imported) { return } |
| 48 | + |
| 49 | +$targetVersion =$ExecutionContext.InvokeCommand.ExpandString($TagVersionFormat) |
| 50 | +$targetReleaseName = $targetVersion |
| 51 | +$releasesURL = 'https://api.github.com/repos/${{github.repository}}/releases' |
| 52 | +"Release URL: $releasesURL" | Out-Host |
| 53 | +$listOfReleases = Invoke-RestMethod -Uri $releasesURL -Method Get -Headers @{ |
| 54 | + "Accept" = "application/vnd.github.v3+json" |
| 55 | + "Authorization" = 'Bearer ${{ secrets.GITHUB_TOKEN }}' |
| 56 | +} |
| 57 | + |
| 58 | +$releaseExists = $listOfReleases | Where-Object tag_name -eq $targetVersion |
| 59 | + |
| 60 | +if ($releaseExists) { |
| 61 | + "::warning::Release '$($releaseExists.Name )' Already Exists" | Out-Host |
| 62 | + return |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +Invoke-RestMethod -Uri $releasesURL -Method Post -Body ( |
| 67 | + [Ordered]@{ |
| 68 | + owner = '${{github.owner}}' |
| 69 | + repo = '${{github.repository}}' |
| 70 | + tag_name = $targetVersion |
| 71 | + name = "$($imported.Name) $targetVersion" |
| 72 | + body = |
| 73 | + if ($env:RELEASENOTES) { |
| 74 | + $env:RELEASENOTES |
| 75 | + } elseif ($imported.PrivateData.PSData.ReleaseNotes) { |
| 76 | + $imported.PrivateData.PSData.ReleaseNotes |
| 77 | + } else { |
| 78 | + "$($imported.Name) $targetVersion" |
| 79 | + } |
| 80 | + draft = if ($env:RELEASEISDRAFT) { [bool]::Parse($env:RELEASEISDRAFT) } else { $false } |
| 81 | + prerelease = if ($env:PRERELEASE) { [bool]::Parse($env:PRERELEASE) } else { $false } |
| 82 | + } | ConvertTo-Json |
| 83 | +) -Headers @{ |
| 84 | + "Accept" = "application/vnd.github.v3+json" |
| 85 | + "Content-type" = "application/json" |
| 86 | + "Authorization" = 'Bearer ${{ secrets.GITHUB_TOKEN }}' |
| 87 | +} |
0 commit comments