-
Notifications
You must be signed in to change notification settings - Fork 248
262 lines (230 loc) · 11.4 KB
/
CI.yaml
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
name: CICD
on:
workflow_dispatch:
push:
paths-ignore:
- '**.md'
- '.github/**'
branches: [ main ]
permissions:
contents: read
actions: read
pull-requests: write
checks: write
id-token: write
concurrency:
group: 'runTests-${{ github.ref }}'
cancel-in-progress: true
defaults:
run:
shell: powershell
jobs:
AnalyzeTests:
if: github.repository == 'Microsoft/NavContainerHelper'
runs-on: [ windows-latest ]
outputs:
tests: ${{ steps.Analyze.outputs.tests }}
linuxtests: ${{ steps.Analyze.outputs.linuxtests }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
lfs: true
- name: Analyze
id: Analyze
run: |
$errorActionPreference = "stop"
$tests = @(Get-ChildItem -Path (Join-Path $ENV:GITHUB_WORKSPACE 'Tests\*.Tests.ps1') | ForEach-Object { $_.BaseName }) | ConvertTo-Json -compress
Add-Content -Path $env:GITHUB_OUTPUT -Value "tests=$tests"
Write-Host "tests=$tests"
$linuxtests = @(Get-ChildItem -Path (Join-Path $ENV:GITHUB_WORKSPACE 'LinuxTests\*.Tests.ps1') | ForEach-Object { $_.BaseName }) | ConvertTo-Json -compress
Add-Content -Path $env:GITHUB_OUTPUT -Value "linuxtests=$linuxtests"
Write-Host "linuxtests=$linuxtests"
PS5:
runs-on: [ windows-latest ]
needs: [ AnalyzeTests ]
strategy:
matrix:
test: ${{fromJson(needs.AnalyzeTests.outputs.tests)}}
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v4
with:
lfs: true
- name: Run Tests
run: |
try {
$errorActionPreference = "stop"
Set-StrictMode -version 2.0
$licenseFile = "$([System.IO.Path]::GetTempFileName()).flf"
[System.IO.File]::WriteAllBytes($licenseFile, ([Convert]::FromBase64String('${{ secrets.licensefile }}')))
$buildLicenseFile = "$([System.IO.Path]::GetTempFileName()).flf"
[System.IO.File]::WriteAllBytes($buildLicenseFile, ([Convert]::FromBase64String('${{ secrets.buildLicensefile }}')))
$pesterContainer = New-PesterContainer -Path (Join-Path $ENV:GITHUB_WORKSPACE 'Tests\${{ matrix.test }}.ps1') -Data @{ "licenseFile" = $licenseFile; "buildLicenseFile" = $buildLicenseFile }
$result = Invoke-Pester -Container $pesterContainer -passthru
if ($result.FailedCount -gt 0) {
Write-Host "::Error::$($result.FailedCount) tests are failing"
$host.SetShouldExit(1)
}
}
catch {
Write-Host "::Error::Exception when running tests. The Error was $($_.Exception.Message)"
$host.SetShouldExit(1)
}
PS7:
runs-on: [ windows-latest ]
defaults:
run:
shell: pwsh
needs: [ AnalyzeTests ]
strategy:
matrix:
test: ${{fromJson(needs.AnalyzeTests.outputs.tests)}}
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v4
with:
lfs: true
- name: Run Tests
run: |
try {
$errorActionPreference = "stop"
Set-StrictMode -version 2.0
$licenseFile = "$([System.IO.Path]::GetTempFileName()).flf"
[System.IO.File]::WriteAllBytes($licenseFile, ([Convert]::FromBase64String('${{ secrets.licensefile }}')))
$buildLicenseFile = "$([System.IO.Path]::GetTempFileName()).flf"
[System.IO.File]::WriteAllBytes($buildLicenseFile, ([Convert]::FromBase64String('${{ secrets.buildLicensefile }}')))
$pesterContainer = New-PesterContainer -Path (Join-Path $ENV:GITHUB_WORKSPACE 'Tests\${{ matrix.test }}.ps1') -Data @{ "licenseFile" = $licenseFile; "buildLicenseFile" = $buildLicenseFile }
$result = Invoke-Pester -Container $pesterContainer -passthru
if ($result.FailedCount -gt 0) {
Write-Host "::Error::$($result.FailedCount) tests are failing"
$host.SetShouldExit(1)
}
}
catch {
Write-Host "::Error::Exception when running tests. The Error was $($_.Exception.Message)"
$host.SetShouldExit(1)
}
Linux:
runs-on: [ ubuntu-latest ]
defaults:
run:
shell: pwsh
needs: [ AnalyzeTests ]
strategy:
matrix:
test: ${{fromJson(needs.AnalyzeTests.outputs.linuxtests)}}
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v4
with:
lfs: true
- name: Run Tests
run: |
try {
$errorActionPreference = "stop"
Set-StrictMode -version 2.0
$licenseFile = "$([System.IO.Path]::GetTempFileName()).flf"
[System.IO.File]::WriteAllBytes($licenseFile, ([Convert]::FromBase64String('${{ secrets.licensefile }}')))
$buildLicenseFile = "$([System.IO.Path]::GetTempFileName()).flf"
[System.IO.File]::WriteAllBytes($buildLicenseFile, ([Convert]::FromBase64String('${{ secrets.buildLicensefile }}')))
$pesterContainer = New-PesterContainer -Path (Join-Path $ENV:GITHUB_WORKSPACE 'LinuxTests\${{ matrix.test }}.ps1') -Data @{ "licenseFile" = $licenseFile; "buildLicenseFile" = $buildLicenseFile }
$result = Invoke-Pester -Container $pesterContainer -passthru
if ($result.FailedCount -gt 0) {
Write-Host "::Error::$($result.FailedCount) tests are failing"
$host.SetShouldExit(1)
}
}
catch {
Write-Host "::Error::Exception when running tests. The Error was $($_.Exception.Message)"
$host.SetShouldExit(1)
}
Deploy:
runs-on: [ windows-latest ]
needs: [ PS5, PS7, Linux ]
if: github.repository_owner == 'microsoft' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch')
steps:
- name: 'Az CLI login'
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
enable-AzPSSession: true
- name: Install Azure Powershell Modules
run: |
if(-not (Get-Module 'Az.Storage' -ListAvailable)) {
Install-Module -Name 'Az.Storage' -Force -AllowClobber
}
- name: Checkout
uses: actions/checkout@v4
- name: Deploy
run: |
try {
$path = Join-Path ([System.IO.Path]::GetTempPath()) "BcContainerHelper"
New-Item -path $path -itemType Directory | Out-Null
Copy-Item -path (Join-Path $ENV:GITHUB_WORKSPACE "*") -Destination $path -Recurse -Force
Remove-Item -Path (Join-Path $path "Tests") -Force -Recurse
Remove-Item -Path (Join-Path $path ".github") -Force -Recurse
Remove-Item -Path (Join-Path $path ".git") -Force -Recurse
Remove-Item -Path (Join-Path $path ".gitignore") -Force
Remove-Item -Path (Join-Path $path "README.md") -Force
$version = (Get-Content -Path (Join-Path $path 'Version.txt')).split('-')[0]
$prerelease = "preview$(620+$ENV:GITHUB_RUN_NUMBER)"
Set-Content -Path (Join-Path $path 'Version.txt') -value "$version-$prerelease"
Write-Host "BcContainerHelper version $Version$prerelease"
$modulePath = Join-Path $ENV:GITHUB_WORKSPACE "BcContainerHelper.psm1"
Import-Module $modulePath -DisableNameChecking
$labels = Get-BcContainerImageLabels -imageName 'mcr.microsoft.com/businesscentral:ltsc2022'
Set-Content -Path (Join-Path $path 'LatestGenericTagVersion.txt') -value $labels.tag
$functionsToExport = (get-module -Name BcContainerHelper).ExportedFunctions.Keys | Sort-Object
$aliasesToExport = (get-module -Name BcContainerHelper).ExportedAliases.Keys | Sort-Object
$releaseNotes = Get-Content -Path (Join-Path $path "ReleaseNotes.txt")
$idx = $releaseNotes.IndexOf($version)
if ($idx -lt 0) {
throw 'No release notes identified'
}
$versionReleaseNotes = @()
while ($releaseNotes[$idx]) {
$versionReleaseNotes += $releaseNotes[$idx]
$idx++
}
Write-Host "Release Notes:"
Write-Host $VersionReleaseNotes
Write-Host "Update Module Manifest"
Update-ModuleManifest -Path (Join-Path $path "BcContainerHelper.psd1") `
-RootModule "BcContainerHelper.psm1" `
-ModuleVersion $version `
-Prerelease $prerelease `
-Author "Freddy Kristiansen" `
-FunctionsToExport $functionsToExport `
-AliasesToExport $aliasesToExport `
-CompanyName "Microsoft" `
-ReleaseNotes $versionReleaseNotes `
-LicenseUri 'https://github.com/microsoft/navcontainerhelper/blob/main/LICENSE' `
-ProjectUri 'https://github.com/microsoft/navcontainerhelper'
$certFileName = Join-Path ([System.IO.Path]::GetTempPath()) "$([GUID]::NewGuid().ToString()).pfx"
[System.IO.File]::WriteAllBytes($certFileName, ([Convert]::FromBase64String('${{ secrets.CodeSignCertificatePfx }}')))
Remove-Module BcContainerHelper
Write-Host $path
Write-Host "Signing scripts"
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($certFileName, '${{ secrets.CodeSignCertificatePassword }}')
$filesToSign = @((Get-ChildItem $path -Filter "*.ps1" -Recurse -Depth 99).FullName)+
@((Get-ChildItem $path -Filter "*.psm1" -Recurse -Depth 99).FullName)
Set-AuthenticodeSignature -Certificate $cert -HashAlgorithm SHA256 -TimestampServer "http://timestamp.digicert.com" -FilePath $filesToSign
Write-Host "Upload to storage (preview)"
$storageContext = New-AzStorageContext -StorageAccountName 'bccontainerhelper' -UseConnectedAccount
New-AzStorageContainer -Name 'public' -Context $storageContext -Permission 'Container' -ErrorAction Ignore | Out-Null
Compress-Archive -path $path -DestinationPath "$($path).zip"
Set-AzStorageBlobContent -File "$($path).zip" -Context $storageContext -Container 'public' -Blob "$version-$prerelease.zip" -Force | Out-Null
Set-AzStorageBlobContent -File "$($path).zip" -Context $storageContext -Container 'public' -Blob "preview.zip" -Force | Out-Null
Write-Host "Publishing Module"
Publish-Module -Path $path -NuGetApiKey '${{ secrets.NugetKey }}' -SkipAutomaticTags
}
catch {
Write-Host "::Error::Error publishing module. Error was $($_.Exception.Message)"
$host.SetShouldExit(1)
}