Repository navigation
170 lines (154 loc) · 7.13 KB
/
Copy pathrelease.yml
File metadata and controls
170 lines (154 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Publishes IntuneScriptLab to the PowerShell Gallery.
#
# Triggered by pushing a version tag:
#
# git tag v1.0.0 && git push origin v1.0.0
#
# The whole quality-gates workflow runs first and the publish is gated on it. A tag can be
# pushed at any commit, including one that never went through a pull request, so the gates
# are re-run here rather than assumed.
#
# Everything published to the Gallery is permanent - a version can be unlisted but never
# deleted, and its .nupkg stays downloadable at the direct URL afterwards. So the two
# checks that cannot be undone afterwards happen before the upload: the tag must match
# ModuleVersion, and Build/Publish-Module.ps1 stages an allowlist rather than packaging the
# working tree (which would otherwise ship the entire .git directory, the tests and the
# validation kit with its tenant-derived findings).
#
# The API key is a repository secret named PSGALLERY_API_KEY:
# Settings -> Secrets and variables -> Actions -> New repository secret
#
# It is passed as an environment variable, never as a command-line argument, so it cannot
# surface in a process listing or in the echoed command in the run log.
name: 'Release'
on:
push:
tags: ['v*']
workflow_dispatch:
inputs:
dryRun:
description: 'Stage and verify without publishing'
type: boolean
default: true
permissions:
contents: read
jobs:
# The same gates a pull request runs: analysis, the suites on Windows PowerShell 7 and 5.1
# and on ARM64, and the command help gate on a case-sensitive filesystem.
gates:
name: 'Quality gates'
uses: ./.github/workflows/quality-gates.yml
publish:
name: 'Publish to PowerShell Gallery'
needs: gates
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v7
# Pinned to match the pin in quality-gates.yml. The publish script rebuilds the MAML,
# and a different PlatyPS version would emit different XML than the committed help
# that was just verified.
- name: Install PlatyPS
shell: pwsh
run: Install-PSResource -Name Microsoft.PowerShell.PlatyPS -Version '1.0.3' -Scope CurrentUser -TrustRepository
# A tag that disagrees with the manifest publishes a version nobody can correlate to
# a commit, and the Gallery version cannot be corrected afterwards. Checked before
# anything is built.
- name: Verify the tag matches ModuleVersion
if: startsWith(github.ref, 'refs/tags/')
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
$tag = '${{ github.ref_name }}' -replace '^v', ''
$manifest = (Test-ModuleManifest ./IntuneScriptLab.psd1).Version.ToString()
if ($tag -ne $manifest) {
throw "Tag '${{ github.ref_name }}' resolves to $tag but the manifest declares $manifest. Bump ModuleVersion and retag."
}
Write-Host "Tag and manifest agree on $manifest."
- name: Stage and verify (no publish)
if: inputs.dryRun
shell: pwsh
run: ./Build/Publish-Module.ps1 -WhatIf
- name: Publish
if: ${{ !inputs.dryRun }}
shell: pwsh
env:
# Consumed by Build/Publish-Module.ps1, which prefers this over its local secret
# vault. Passing it here rather than as -ApiKey keeps it out of the run log and
# out of the process command line.
PSGALLERY_API_KEY: ${{ secrets.PSGALLERY_API_KEY }}
run: |
$ErrorActionPreference = 'Stop'
if (-not $env:PSGALLERY_API_KEY) {
throw 'The PSGALLERY_API_KEY repository secret is not set. Add it under Settings -> Secrets and variables -> Actions.'
}
./Build/Publish-Module.ps1 -Confirm:$false
github-release:
name: 'GitHub release'
needs: publish
# Without this job a tag published to the Gallery but showed no GitHub release, so the
# repository page kept naming an older hand-made release as Latest. The notes are the
# version's CHANGELOG section. They are built in every run, a dry run included, so a missing
# section fails before a tag is pushed rather than after the upload.
runs-on: ubuntu-latest
# Creating the release is the only write this workflow makes to the repository
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v7
- name: Build the release notes from CHANGELOG.md
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
$manifest = @(Get-ChildItem -Path . -Filter *.psd1 -File)
if ($manifest.Count -ne 1) {
throw "Expected one module manifest at the repository root, found $($manifest.Count)."
}
$module = $manifest[0].BaseName
$version = (Import-PowerShellDataFile -Path $manifest[0].FullName).ModuleVersion
# The section runs from the version's heading to the next '## [' heading
$lines = @(Get-Content -Path ./CHANGELOG.md)
$heading = '^' + [regex]::Escape("## [$version]")
$start = -1
for ($i = 0; $i -lt $lines.Count; $i++) {
if ($lines[$i] -match $heading) { $start = $i; break }
}
if ($start -lt 0) {
throw "CHANGELOG.md has no '## [$version]' section to use as the release notes."
}
$end = $lines.Count
for ($i = $start + 1; $i -lt $lines.Count; $i++) {
if ($lines[$i] -match '^## \[') { $end = $i; break }
}
$section = if ($end -gt $start + 1) { ($lines[($start + 1)..($end - 1)] -join "`n").Trim() }
if (-not $section) {
throw "The '## [$version]' section of CHANGELOG.md is empty."
}
$fence = '```'
$history = "$env:GITHUB_SERVER_URL/$env:GITHUB_REPOSITORY/blob/v$version/CHANGELOG.md"
$notes = @("${fence}powershell", "Update-PSResource $module", $fence, '', $section, '',
"Full history: [CHANGELOG.md]($history)") -join "`n"
Set-Content -Path ./release-notes.md -Value $notes -Encoding utf8
"RELEASE_TAG=v$version" | Add-Content -Path $env:GITHUB_ENV
"RELEASE_TITLE=$module $version" | Add-Content -Path $env:GITHUB_ENV
Write-Host "Release notes for $module $version ($($notes.Length) characters):"
Write-Host $notes
- name: Create the GitHub release
if: ${{ startsWith(github.ref, 'refs/tags/') && !inputs.dryRun }}
shell: pwsh
env:
GH_TOKEN: ${{ github.token }}
run: |
$ErrorActionPreference = 'Stop'
# A release made by hand before this job ran is kept as written
gh release view $env:RELEASE_TAG *> $null
if ($LASTEXITCODE -eq 0) {
Write-Host "A release for $env:RELEASE_TAG already exists; leaving it unchanged."
exit 0
}
# --verify-tag refuses to create a tag; GitHub marks the release Latest by version
gh release create $env:RELEASE_TAG --title $env:RELEASE_TITLE --notes-file ./release-notes.md --verify-tag
if ($LASTEXITCODE -ne 0) {
throw "gh release create failed with exit code $LASTEXITCODE."
}