generated from deadlydog/Template.NewGitRepo
-
-
Notifications
You must be signed in to change notification settings - Fork 16
275 lines (230 loc) · 13.3 KB
/
build-and-test-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
name: build
on:
push:
branches: [ "main" ]
paths: [ "src/**", "build/**", "deploy/**", ".github/workflows/**" ]
pull_request:
branches: [ "main" ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
inputs:
versionNumber:
description: 'The version number to use for the module. This should be in the format of "Major.Minor.Patch". e.g. "1.0.0". Future builds will increment from this version number. This input is optional. If not provided, the previous version numbers Patch will be incremented.'
required: false
type: string
default: ''
deploy:
description: 'Deploy the build artifacts. Only has effect when not building the main branch.'
required: false
type: boolean
default: false
env:
powerShellModuleName: 'tiPS' # Must match the name in the deployment workflow.
powerShellModuleDirectoryPath: './src/tiPS'
powerShellModuleManifestFileName: 'tiPS.psd1'
deployFilesDirectoryPath: './deploy'
prereleaseArtifactDirectoryPath: './artifacts/Prerelease'
stableArtifactDirectoryPath: './artifacts/Stable'
deployFilesArtifactDirectoryPath: './artifacts/deploy'
prereleaseArtifactName: 'PrereleaseModuleArtifact' # Must match the name in the deployment workflow.
stableArtifactName: 'StableModuleArtifact' # Must match the name in the deployment workflow.
deployFilesArtifactName: 'DeployFilesArtifact' # Must match the name in the deployment workflow.
ciVersionTagPrefix: 'ci-version-'
jobs:
build-and-test:
runs-on: windows-latest # Use Windows agent to ensure dotnet.exe is available to build C# assemblies.
steps:
- name: Checkout the repo source code
uses: actions/checkout@v3
- name: Run spellcheck
uses: streetsidesoftware/cspell-action@v3
- name: Display PowerShell version being used
shell: pwsh
run: $PSVersionTable
- name: Build the C# assemblies
shell: pwsh
run: ./build/Build-CSharpAssemblies.ps1 -Force
- name: Generate PowerShellTips.json file
shell: pwsh
run: ./build/Convert-PowerShellTipFilesToJsonFile.ps1
- name: Run PowerShell linter with PSScriptAnalyzer
shell: pwsh
run: Invoke-ScriptAnalyzer -Path . -Recurse -EnableExit
- name: Run Pester tests and generate code coverage report
shell: pwsh
run: |
Write-Output "Pester version being used:"
Import-Module -Name Pester
Get-Module -Name Pester
Write-Output "Running all Pester tests in the repo:"
$pesterConfig = New-PesterConfiguration @{
Output = @{ Verbosity = 'Detailed' }
Run = @{ Throw = $true }
TestResult = @{
Enabled = $true
OutputPath = 'test-results-nunit.xml'
}
CodeCoverage = @{
Enabled = $true
OutputPath = 'code-coverage-jacoco.xml'
Path = 'src/' # Only include code coverage for the module's source code, not build or deployment scripts.
}
}
Invoke-Pester -Configuration $pesterConfig
- name: Add code coverage report to PR
# Adding the code coverage report is not supported for manual workflow runs.
if: github.event_name != 'workflow_dispatch'
uses: madrapps/jacoco-report@v1.6.1
with:
paths: code-coverage-jacoco.xml
token: ${{ secrets.GITHUB_TOKEN }}
min-coverage-overall: 60
min-coverage-changed-files: 60
- name: Build the concatenated psm1 file
shell: pwsh
run: ./build/Build-Psm1File.ps1 -DeleteSourceFilesAfterImport
- name: Run Pester tests again to ensure the concatenated psm1 file works as expected
shell: pwsh
run: |
Write-Output "Pester version being used:"
Import-Module -Name Pester
Get-Module -Name Pester
Write-Output "Running all Pester tests in the repo:"
$pesterConfig = New-PesterConfiguration @{
Output = @{ Verbosity = 'Detailed' }
Run = @{ Throw = $true }
TestResult = @{ Enabled = $false }
CodeCoverage = @{ Enabled = $false }
}
Invoke-Pester -Configuration $pesterConfig
- name: Determine new version from Git tag and set environment variables
shell: pwsh
run: |
[string] $ciVersionTagPrefix = $Env:ciVersionTagPrefix
[string] $ciTagSearchPattern = $ciVersionTagPrefix + '*'
Write-Output "Fetching all git tags, in case they were not included with the git checkout."
& git fetch --tags origin
Write-Output "Searching for CI version git tag by using search pattern '$ciTagSearchPattern'."
[string] $ciVersionTag = (& git tag --list $ciTagSearchPattern)
Write-Output "CI version git tag is '$ciVersionTag'."
if ([string]::IsNullOrWhiteSpace($ciVersionTag))
{
Write-Output "No CI version git tag was found. Assuming this is the first CI build and setting the version number to 0.0.0."
$ciVersionTag = $ciVersionTagPrefix + '0.0.0'
}
[string] $previousVersionNumber = $ciVersionTag -replace $ciVersionTagPrefix, ''
Write-Output "Previous version number was '$previousVersionNumber'. Determining what the new version number should be."
[string] $newVersionNumber = '${{ inputs.versionNumber }}'
if ([string]::IsNullOrWhiteSpace($newVersionNumber))
{
Write-Output "No version number was provided. Incrementing the previous version number."
$currentVersion = [System.Version]::new($previousVersionNumber)
$newVersion = [System.Version]::new($currentVersion.Major, $currentVersion.Minor, $currentVersion.Build + 1)
$newVersionNumber = $newVersion.ToString()
}
else
{
Write-Output "Version number '$newVersionNumber' was provided. Using it as the new version number."
}
Write-Output "Setting new environment variables 'NewVersionNumber=$newVersionNumber' and 'PreviousCiVersionTag=$ciVersionTag'."
"NewVersionNumber=$newVersionNumber" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append
"PreviousCiVersionTag=$ciVersionTag" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append
- name: Create Stable and Prerelease module artifacts
shell: pwsh
run: |
function Replace-TextInFile(
[ValidateScript({Test-Path $_ -PathType Leaf})]
[string]$filePath,
[string]$textToReplace,
[string]$replacementText)
{
$fileContents = Get-Content -Path $filePath -Raw
$newFileContents = $fileContents.Replace($textToReplace, $replacementText)
Set-Content -Path $filePath -Value $newFileContents
}
Write-Output "Reading in environment variables."
[string] $moduleName = $Env:powerShellModuleName
[string] $moduleDirectoryPath = $Env:powerShellModuleDirectoryPath
[string] $moduleManifestFileName = $Env:powerShellModuleManifestFileName
[string] $prereleaseArtifactModuleDirectoryPath = Join-Path -Path $Env:prereleaseArtifactDirectoryPath -ChildPath $moduleName
[string] $stableArtifactModuleDirectoryPath = Join-Path -Path $Env:stableArtifactDirectoryPath -ChildPath $moduleName
Write-Output "Reading in dynamic environment variables."
[string] $newVersionNumber = $Env:NewVersionNumber
Write-Output "Determining what the module manifest file paths should be."
[string] $manifestFilePath = Join-Path -Path $moduleDirectoryPath -ChildPath $moduleManifestFileName
[string] $prereleaseManifestFilePath = Join-Path -Path $prereleaseArtifactModuleDirectoryPath -ChildPath $moduleManifestFileName
[string] $stableManifestFilePath = Join-Path -Path $stableArtifactModuleDirectoryPath -ChildPath $moduleManifestFileName
Write-Output "Retrieving the module manifest's current version number line from '$manifestFilePath' so it can be updated."
$manifestVersionNumberRegexPattern = "(?i)ModuleVersion = '(?<Version>.*?)'"
$manifestVersionNumberMatches =
Select-String -Path $manifestFilePath -Pattern $manifestVersionNumberRegexPattern |
Select-Object -First 1
if ($manifestVersionNumberMatches.Matches.Count -le 0 -or
!$manifestVersionNumberMatches.Matches[0].Success)
{
throw "Could not find the manifest's current version number."
}
$manifestVersionNumberMatch = $manifestVersionNumberMatches.Matches[0]
$currentManifestVersionNumber = $manifestVersionNumberMatch.Groups['Version'].Value
$currentManifestVersionNumberLine = $manifestVersionNumberMatch.Value
Write-Output "Copying the module files to the Prerelease artifact directory '$prereleaseArtifactModuleDirectoryPath'."
Copy-Item -Path $moduleDirectoryPath -Destination $prereleaseArtifactModuleDirectoryPath -Exclude '*.Tests.ps1' -Recurse -Force
Write-Output "Copying the module files to the Stable artifact directory '$stableArtifactModuleDirectoryPath'."
Copy-Item -Path $moduleDirectoryPath -Destination $stableArtifactModuleDirectoryPath -Exclude '*.Tests.ps1' -Recurse -Force
# Prerelease version numbers can only contain 'a-zA-Z0-9' characters.
[string] $dateTime = (Get-Date -Format 'yyyyMMddTHHmmss')
[string] $truncatedCommitSha = $Env:GITHUB_SHA.Substring(0, 7)
[string] $prereleaseVersionNumberPostfix = 'ci' + $dateTime + 'SHA' + $truncatedCommitSha
Write-Output "Updating the prerelease manifest's version number from '$currentManifestVersionNumber' to '$newVersionNumber$prereleaseVersionNumberPostfix'."
Replace-TextInFile -filePath $prereleaseManifestFilePath -textToReplace $currentManifestVersionNumberLine -replacementText "ModuleVersion = '$newVersionNumber'"
Replace-TextInFile -filePath $prereleaseManifestFilePath -textToReplace "# Prerelease = ''" -replacementText "Prerelease = '$prereleaseVersionNumberPostfix'"
Write-Output "Updating the stable manifest's version number from '$currentManifestVersionNumber' to '$newVersionNumber'."
Replace-TextInFile -filePath $stableManifestFilePath -textToReplace $currentManifestVersionNumberLine -replacementText "ModuleVersion = '$newVersionNumber'"
- name: Create deploy files artifact
shell: pwsh
run: |
[string] $deployFilesDirectoryPath = $Env:deployFilesDirectoryPath
[string] $deployFilesArtifactDirectoryPath = $Env:deployFilesArtifactDirectoryPath
Write-Output "Copying the deployment files '$deployFilesDirectoryPath' to the deployment artifact directory '$deployFilesArtifactDirectoryPath'."
Copy-Item -Path $deployFilesDirectoryPath -Destination $deployFilesArtifactDirectoryPath -Recurse -Force
- name: Update CI version tag and set new version tag
# Only run this step if we are doing a push (not a PR) to the default branch (e.g. main).
if: github.event_name != 'pull_request' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
shell: pwsh
run: |
[string] $previousCiVersionTag = $Env:PreviousCiVersionTag
[string] $newVersionNumber = $Env:NewVersionNumber
[string] $newCiVersionTag = $Env:ciVersionTagPrefix + $newVersionNumber
[string] $newVersionTag = "v$newVersionNumber"
# To avoid a 403 error on 'git push', ensure you have granted your GitHub Actions workflow read/write permission.
# In your GitHub repo: Settings > Actions > General > Workflow permissions > Read and write permissions
Write-Output "Removing the previous CI version tag '$previousCiVersionTag'."
& git tag -d $previousCiVersionTag
& git push origin --delete $previousCiVersionTag
Write-Output "Creating new CI version tag '$newCiVersionTag'."
& git tag $newCiVersionTag
& git push origin $newCiVersionTag
Write-Output "Tagging commit with new version tag '$newVersionTag'."
& git tag $newVersionTag
& git push origin $newVersionTag
- name: Upload prerelease module artifact
uses: actions/upload-artifact@v3
with:
name: ${{ env.prereleaseArtifactName }}
path: ${{ env.prereleaseArtifactDirectoryPath }}
- name: Upload stable module artifact
uses: actions/upload-artifact@v3
with:
name: ${{ env.stableArtifactName }}
path: ${{ env.stableArtifactDirectoryPath }}
- name: Upload deploy files artifact
uses: actions/upload-artifact@v3
with:
name: ${{ env.deployFilesArtifactName }}
path: ${{ env.deployFilesArtifactDirectoryPath }}
trigger-deployment:
needs: build-and-test
# Only trigger a deployment if this build is for a push (not a PR) and is for the default branch (main).
if: inputs.deploy || (github.event_name != 'pull_request' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch))
uses: ./.github/workflows/deploy-powershell-module.yml
secrets: inherit