generated from deadlydog/Template.NewGitRepo
-
-
Notifications
You must be signed in to change notification settings - Fork 16
278 lines (237 loc) · 11.9 KB
/
deploy-powershell-module.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
name: deploy
on:
workflow_run:
workflows: [build]
types: [completed]
branches: [main]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
inputs:
workflowRunId:
description: 'The build workflow run ID containing the artifacts to use. The run ID can be found in the URL of the build workflow run.'
type: number
required: true
env:
powerShellModuleName: 'tiPS' # Must match the name in the build workflow.
prereleaseModuleArtifactName: 'PrereleaseModuleArtifact' # Must match the name in the build workflow.
stableModuleArtifactName: 'StableModuleArtifact' # Must match the name in the build workflow.
deployFilesArtifactName: 'DeployFilesArtifact' # Must match the name in the build workflow.
artifactsDirectoryPath: './artifacts'
workflowRunId: ${{ github.event_name == 'workflow_dispatch' && inputs.workflowRunId || github.event.workflow_run.id }} # Ternary operator to use input value if manually triggered, otherwise use the workflow_run.id value.
jobs:
publish-prerelease-module:
# Only run the deployment if manually triggered, or the build workflow succeeded.
if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
outputs:
prereleaseVersionNumber: ${{ steps.output-version-number.outputs.prereleaseVersionNumber }}
steps:
- name: Download prerelease module artifact from triggered workflow
uses: dawidd6/action-download-artifact@v2
with:
run_id: ${{ env.workflowRunId }}
name: ${{ env.prereleaseModuleArtifactName}}
path: ${{ env.artifactsDirectoryPath }}
search_artifacts: true
- name: Publish prerelease PowerShell module
shell: pwsh
run: |
[string] $moduleDirectoryPath = "$Env:artifactsDirectoryPath/$Env:powerShellModuleName"
Publish-Module -Path $moduleDirectoryPath -NuGetApiKey '${{ secrets.POWERSHELL_GALLERY_API_KEY }}' -Verbose
- name: Make prerelease version number available to downstream jobs
id: output-version-number
shell: pwsh
run: |
[string] $moduleManifestPath = "$Env:artifactsDirectoryPath/$Env:powerShellModuleName/$Env:powerShellModuleName.psd1"
Write-Output "Reading module manifest from '$moduleManifestPath'."
[hashtable] $manifest = Get-Content -Path $moduleManifestPath -Raw | Invoke-Expression
[string] $versionNumber = $manifest.ModuleVersion
[string] $prereleasePostfix = $manifest.PrivateData.PSData.Prerelease
[string] $prereleaseVersionNumber = "$versionNumber-$prereleasePostfix"
Write-Output "Saving the prerelease version number '$prereleaseVersionNumber' to an output variable."
"prereleaseVersionNumber=$prereleaseVersionNumber" | Out-File -FilePath $Env:GITHUB_OUTPUT -Encoding utf8 -Append
- name: Wait a short while for the module to be available on the PowerShell Gallery before continuing
shell: pwsh
run: Start-Sleep -Seconds 30
test-prerelease-module-in-pwsh:
needs: publish-prerelease-module
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Display PowerShell version being used
shell: pwsh
run: $PSVersionTable
- name: Install prerelease module from PowerShell Gallery
shell: pwsh
run: |
[string] $moduleName = $Env:powerShellModuleName
[string] $prereleaseVersionNumber = '${{ needs.publish-prerelease-module.outputs.prereleaseVersionNumber }}'
Write-Output "Installing the module '$moduleName' prerelease version '$prereleaseVersionNumber' from the PowerShell Gallery."
Install-Module -Name $moduleName -AllowPrerelease -RequiredVersion $prereleaseVersionNumber -Force -Scope CurrentUser -Repository PSGallery -ErrorAction Stop -Verbose
- name: Download deploy files artifact from triggered workflow
uses: dawidd6/action-download-artifact@v2
with:
run_id: ${{ env.workflowRunId }}
name: ${{ env.deployFilesArtifactName}}
path: ${{ env.artifactsDirectoryPath }}
search_artifacts: true
- name: Run smoke tests
shell: pwsh
run: |
[string] $smokeTestsScriptPath = "$Env:artifactsDirectoryPath/Invoke-SmokeTests.ps1"
Write-Output "Running Pester smoke tests from file '$smokeTestsScriptPath'."
$pesterConfig = New-PesterConfiguration @{
Output = @{ Verbosity = 'Detailed' }
Run = @{
Path = $smokeTestsScriptPath
Throw = $true
}
}
Invoke-Pester -Configuration $pesterConfig
Write-Output "Displaying the installed module version that was used for the smoke tests."
Get-Module -Name $Env:powerShellModuleName
test-prerelease-module-in-windows-powershell:
needs: publish-prerelease-module
runs-on: windows-latest
steps:
- name: Display PowerShell version being used
shell: powershell
run: $PSVersionTable
- name: Install prerelease module from PowerShell Gallery
shell: powershell
run: |
[string] $moduleName = $Env:powerShellModuleName
[string] $prereleaseVersionNumber = '${{ needs.publish-prerelease-module.outputs.prereleaseVersionNumber }}'
Write-Output "Installing the module '$moduleName' prerelease version '$prereleaseVersionNumber' from the PowerShell Gallery."
Install-Module -Name $moduleName -AllowPrerelease -RequiredVersion $prereleaseVersionNumber -Force -Scope CurrentUser -Repository PSGallery -ErrorAction Stop -Verbose
- name: Download deploy files artifact from triggered workflow
uses: dawidd6/action-download-artifact@v2
with:
run_id: ${{ env.workflowRunId }}
name: ${{ env.deployFilesArtifactName}}
path: ${{ env.artifactsDirectoryPath }}
search_artifacts: true
- name: Run smoke tests
shell: powershell
run: |
[string] $smokeTestsScriptPath = "$Env:artifactsDirectoryPath/Invoke-SmokeTests.ps1"
Write-Output "Running Pester smoke tests from file '$smokeTestsScriptPath'."
$pesterConfig = New-PesterConfiguration @{
Output = @{ Verbosity = 'Detailed' }
Run = @{
Path = $smokeTestsScriptPath
Throw = $true
}
}
Invoke-Pester -Configuration $pesterConfig
Write-Output "Displaying the installed module version that was used for the smoke tests."
Get-Module -Name $Env:powerShellModuleName
publish-stable-module:
needs: [test-prerelease-module-in-pwsh, test-prerelease-module-in-windows-powershell]
runs-on: ubuntu-latest
environment: production # Used for deployment approvals.
outputs:
stableVersionNumber: ${{ steps.output-version-number.outputs.StableVersionNumber }}
steps:
- name: Download stable module artifact from triggered workflow
uses: dawidd6/action-download-artifact@v2
with:
run_id: ${{ env.workflowRunId }}
name: ${{ env.stableModuleArtifactName}}
path: ${{ env.artifactsDirectoryPath }}
search_artifacts: true
- name: Publish stable PowerShell module
shell: pwsh
run: |
[string] $moduleDirectoryPath = "$Env:artifactsDirectoryPath/$Env:powerShellModuleName"
Publish-Module -Path $moduleDirectoryPath -NuGetApiKey '${{ secrets.POWERSHELL_GALLERY_API_KEY }}' -Verbose
- name: Make stable version number available to downstream jobs
id: output-version-number
shell: pwsh
run: |
[string] $moduleManifestPath = "$Env:artifactsDirectoryPath/$Env:powerShellModuleName/$Env:powerShellModuleName.psd1"
Write-Output "Reading module manifest from '$moduleManifestPath'."
[hashtable] $manifest = Get-Content -Path $moduleManifestPath -Raw | Invoke-Expression
[string] $versionNumber = $manifest.ModuleVersion
Write-Output "Saving the stable version number '$versionNumber' to an output variable."
"stableVersionNumber=$versionNumber" | Out-File -FilePath $Env:GITHUB_OUTPUT -Encoding utf8 -Append
- name: Wait a short while for the module to be available on the PowerShell Gallery before continuing
shell: pwsh
run: Start-Sleep -Seconds 30
test-stable-module-in-pwsh:
needs: publish-stable-module
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Display PowerShell version being used
shell: pwsh
run: $PSVersionTable
- name: Install stable module from PowerShell Gallery
shell: pwsh
run: |
[string] $moduleName = $Env:powerShellModuleName
[string] $stableVersionNumber = '${{ needs.publish-stable-module.outputs.stableVersionNumber }}'
Write-Output "Installing the module '$moduleName' stable version '$stableVersionNumber' from the PowerShell Gallery."
Install-Module -Name $moduleName -RequiredVersion $stableVersionNumber -Force -Scope CurrentUser -Repository PSGallery -ErrorAction Stop -Verbose
- name: Download deploy files artifact from triggered workflow
uses: dawidd6/action-download-artifact@v2
with:
run_id: ${{ env.workflowRunId }}
name: ${{ env.deployFilesArtifactName}}
path: ${{ env.artifactsDirectoryPath }}
search_artifacts: true
- name: Run smoke tests
shell: pwsh
run: |
[string] $smokeTestsScriptPath = "$Env:artifactsDirectoryPath/Invoke-SmokeTests.ps1"
Write-Output "Running Pester smoke tests from file '$smokeTestsScriptPath'."
$pesterConfig = New-PesterConfiguration @{
Output = @{ Verbosity = 'Detailed' }
Run = @{
Path = $smokeTestsScriptPath
Throw = $true
}
}
Invoke-Pester -Configuration $pesterConfig
Write-Output "Displaying the installed module version that was used for the smoke tests."
Get-Module -Name $Env:powerShellModuleName
test-stable-module-in-windows-powershell:
needs: publish-stable-module
runs-on: windows-latest
steps:
- name: Display PowerShell version being used
shell: powershell
run: $PSVersionTable
- name: Install stable module from PowerShell Gallery
shell: powershell
run: |
[string] $moduleName = $Env:powerShellModuleName
[string] $stableVersionNumber = '${{ needs.publish-stable-module.outputs.stableVersionNumber }}'
Write-Output "Installing the module '$moduleName' stable version '$stableVersionNumber' from the PowerShell Gallery."
Install-Module -Name $moduleName -RequiredVersion $stableVersionNumber -Force -Scope CurrentUser -Repository PSGallery -ErrorAction Stop -Verbose
- name: Download deploy files artifact from triggered workflow
uses: dawidd6/action-download-artifact@v2
with:
run_id: ${{ env.workflowRunId }}
name: ${{ env.deployFilesArtifactName}}
path: ${{ env.artifactsDirectoryPath }}
search_artifacts: true
- name: Run smoke tests
shell: powershell
run: |
[string] $smokeTestsScriptPath = "$Env:artifactsDirectoryPath/Invoke-SmokeTests.ps1"
Write-Output "Running Pester smoke tests from file '$smokeTestsScriptPath'."
$pesterConfig = New-PesterConfiguration @{
Output = @{ Verbosity = 'Detailed' }
Run = @{
Path = $smokeTestsScriptPath
Throw = $true
}
}
Invoke-Pester -Configuration $pesterConfig
Write-Output "Displaying the installed module version that was used for the smoke tests."
Get-Module -Name $Env:powerShellModuleName