-
Notifications
You must be signed in to change notification settings - Fork 0
235 lines (231 loc) · 11.1 KB
/
release.yml
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
name: Release
on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+*"
permissions:
contents: write
jobs:
create-release:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
with:
fetch-depth: 0
- name: Check Version
shell: pwsh
if: startsWith(github.ref, 'refs/tags/v')
run: |
$tag = $env:GITHUB_REF.Replace('refs/tags/v', '')
$manifestName = (Get-ChildItem -Path $env:GITHUB_WORKSPACE -Filter *.psd1).Name
$moduleVersion = [regex]::Match((Get-Content -Path ./$manifestName -Raw), '(?<=ModuleVersion\s*=\s*'')[^'']+(?='')').Value
$version = $moduleVersion -split '\.' | Select-Object -First 3 | Join-String -Separator '.'
$release = ($tag -replace '^v') -split '\.' | Select-Object -First 3 | Join-String -Separator '.'
if ($version -ne $release) {
Write-Error "FAILED: Comparing module version '$version' with release tag 'v$tag'."
exit 1
} else {
Write-Output "SUCCESS: Comparing module version '$version' with release tag 'v$tag'."
}
- name: Check Changelog
shell: pwsh
run: |
$version = $env:GITHUB_REF.Replace('refs/tags/', '')
$changelog = Get-Content -Path CHANGELOG.md
$foundVersion = $false
foreach ($line in $changelog) {
if ($line -match "^## $version$") {
$foundVersion = $true
continue
}
if ($foundVersion -and $line -match "^## ") {
break
}
}
if ($foundVersion) {
Write-Output "SUCCESS: Locating release in the changelog for version '$version'."
} else {
Write-Error "FAILED: Locating release in the changelog for version '$version'."
exit 1
}
- name: Create Release Branch
shell: pwsh
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
$version = $env:GITHUB_REF.Replace('refs/tags/', '')
$releaseBranch = "release/$version"
$git = Get-Command git | Select-Object -ExpandProperty Definition
& $git config --global user.name "github-actions[bot]"
& $git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
& $git checkout -b $releaseBranch
& $git push origin $releaseBranch
if ($LASTEXITCODE -ne 0) {
Write-Error "FAILED: Creating release branch '$releaseBranch'."
} else {
Write-Output "SUCCESS: Creating release branch '$releaseBranch'."
}
- name: Rebase Main Branch
shell: pwsh
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
$version = $env:GITHUB_REF.Replace('refs/tags/', '')
$releaseBranch = "release/$version"
$git = Get-Command git | Select-Object -ExpandProperty Definition
& $git config --global user.name "github-actions[bot]"
& $git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
& $git checkout $releaseBranch
& $git pull origin $releaseBranch
& $git checkout main
& $git pull origin main
& $git rebase $releaseBranch
& $git push origin main
if ($LASTEXITCODE -ne 0) {
Write-Error "FAILED: Rebasing main branch from release branch '$releaseBranch'."
} else {
Write-Output "SUCCESS: Rebasing main branch from release branch '$releaseBranch'."
}
- name: Install GitHub CLI
run: |
type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y)
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
&& sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y
- name: Create Release
shell: pwsh
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
$version = $env:GITHUB_REF.Replace('refs/tags/', '')
$changelog = Get-Content -Path CHANGELOG.md
$releaseNotes = $null
$foundVersion = $false
foreach ($line in $changelog) {
if ($line -match "^## $version$") {
$foundVersion = $true
continue
}
if ($foundVersion -and $line -match "^## ") {
break
}
if ($foundVersion) {
$releaseNotes += $line + "`n"
}
}
$gh = Get-Command gh | Select-Object -ExpandProperty Definition
& $gh release create $version --title "$version" --notes "$releaseNotes" --target "release/$version"
if ($LASTEXITCODE -ne 0) {
Write-Error "FAILED: Creating GitHub release '$version'."
} else {
Write-Output "SUCCESS: Creating GitHub release '$version'."
}
publish-docs:
needs: create-release
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
with:
ref: ${{ github.event.release.tag_name }}
fetch-depth: 0
- name: Setup Python
uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
with:
python-version: 3.x
- name: Install Dependencies
run: |
pip install mkdocs-material
pip install --requirement docs/requirements.txt
- name: Publish Documentation
run: |
mkdocs gh-deploy --force
if: ${{ success() }}
publish-module:
needs: [create-release, publish-docs]
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
with:
ref: ${{ github.event.release.tag_name }}
fetch-depth: 0
- name: Publish Module to PowerShell Gallery
shell: pwsh
env:
PSGALLERY_API_KEY: ${{ secrets.PSGALLERY_API_KEY }}
run: |
Write-Output "INFO: Preparing Ubuntu-based GitHub runner for publishing module to the PowerShell Gallery."
Write-Output "INFO: Setting the PowerShell Gallery as a trusted repository."
Set-PSRepository psgallery -InstallationPolicy trusted
Write-Output "INFO: Locating module manifest in '$env:GITHUB_WORKSPACE'."
$moduleManifest = (Get-ChildItem -Path $env:GITHUB_WORKSPACE -Filter *.psd1).Name
if ($moduleManifest) {
Write-Output "SUCCESS: Manifest '$moduleManifest' found in '$env:GITHUB_WORKSPACE'."
} else {
Write-Output "FAILURE: Manifest not found in '$env:GITHUB_WORKSPACE'."
}
if ($moduleManifest -match '^(.*)\.psd1$') {
$moduleName = $Matches[1]
Write-Output "SUCCESS: Determining module name from manifest '$moduleManifest'."
} else {
Write-Error "FAILED: Determining module name from manifest '$moduleManifest'."
}
Write-Output "INFO: Reading module manifest '$moduleManifest'."
$moduleManifestData = Import-PowerShellDataFile -Path $moduleManifest
Write-Output "INFO: Determining module dependencies."
$requiredModules = $moduleManifestData.RequiredModules
if ($requiredModules) {
Write-Output "SUCCESS: Module dependencies were found."
Write-Output "INFO: Required modules are $($requiredModules.ModuleName -join ', ')."
Write-Output "INFO: Setting location to the PowerShell modules location on a Ubuntu-based GitHub runner."
Set-Location '/home/runner/.local/share/powershell/Modules/'
foreach ($module in $requiredModules) {
$requiredModuleName = $module.ModuleName
New-Item $requiredModuleName -ItemType Directory
Write-Output "INFO: Performing workaround for github.com/PowerShell/PowerShell/issues/7722."
Write-Output "INFO: Creating placeholder manifest for $requiredModuleName at $((Get-Location).Path)/$requiredModuleName/$requiredModuleName.psd1"
New-Item "./$requiredModuleName/$requiredModuleName.psd1" -ItemType File
}
} else {
Write-Output "INFO: No module dependencies were found."
}
Write-Output "INFO: Setting location to the GitHub workspace at '$env:GITHUB_WORKSPACE'."
Set-Location $env:GITHUB_WORKSPACE
Write-Output "INFO: Publishing module to the PowerShell Gallery."
$remove = @('.ci', '.dependencies', '.git', '.github', '.gitignore', '.vscode', 'docs', 'CODEOWNERS', 'CODE_OF_CONDUCT.md', 'CONTRIBUTING.md', 'Makefile', 'mkdocs.yml')
$random = Get-Random -Count 1
$destinationPath = Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath $random
$manifestName = (Get-ChildItem -Path $env:GITHUB_WORKSPACE -Filter *.psd1).Name
$moduleVersion = [regex]::Match((Get-Content -Path ./$manifestName -Raw), '(?<=ModuleVersion\s*=\s*'')[^'']+(?='')').Value
if ($manifestName -match '^(.*)\.psd1$') {
$moduleName = $Matches[1]
Write-Output "SUCCESS: Determining module name from manifest file name '$moduleName'."
} else {
Write-Error "FAILED: Determining module name from manifest file name '$moduleName'."
}
$modulePath = Join-Path -Path $destinationPath -ChildPath $moduleName
$createModulePath = New-Item -Path $modulePath -ItemType Directory -Force
if ($createModulePath) {
Write-Output "SUCCESS: Creating staging path '$modulePath'."
} else {
Write-Error "FAILED: Creating staging path '$modulePath'."
}
Get-ChildItem -Force | Where-Object { $_.Name -notin $remove -and $_.Name -ne $random } | Copy-Item -Destination $modulePath -Recurse
Get-ChildItem -Depth 5 -Path $modulePath | Format-Table -AutoSize
$moduleManifest = Join-Path -Path $modulePath -ChildPath "$moduleName.psd1"
if (Test-Path -Path $moduleManifest) {
Publish-Module -Path $modulePath -NuGetApiKey $env:PSGALLERY_API_KEY
Start-Sleep -Seconds 30
$module = Find-Module -Name $moduleName -RequiredVersion "$moduleVersion"
if ($module) {
Write-Output "SUCCESS: Publishing module '$moduleName' version '$moduleVersion' to PowerShell Gallery."
} else {
Write-Error "FAILED: Publishing module '$moduleName' version '$moduleVersion' to PowerShell Gallery."
}
} else {
Write-Error "FAILED: Module manifest file not found at path '$moduleManifest'."
}