forked from freddydk/spchars
-
Notifications
You must be signed in to change notification settings - Fork 0
242 lines (224 loc) · 10.8 KB
/
PublishToEnvironment.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
name: ' Publish To Environment'
on:
workflow_dispatch:
inputs:
appVersion:
description: App version to deploy to environment(s) (current, prerelease, draft, latest or version number)
required: false
default: 'current'
environmentName:
description: Environment mask to receive the new version (* for all, PROD* for all environments starting with PROD)
required: true
permissions:
contents: read
actions: read
defaults:
run:
shell: powershell
env:
ALGoOrgSettings: ${{ vars.ALGoOrgSettings }}
ALGoRepoSettings: ${{ vars.ALGoRepoSettings }}
jobs:
Initialization:
runs-on: [ windows-latest ]
outputs:
telemetryScopeJson: ${{ steps.init.outputs.telemetryScopeJson }}
settings: ${{ steps.ReadSettings.outputs.SettingsJson }}
environments: ${{ steps.ReadSettings.outputs.EnvironmentsJson }}
environmentCount: ${{ steps.ReadSettings.outputs.EnvironmentCount }}
unknownEnvironment: ${{ steps.ReadSettings.outputs.UnknownEnvironment }}
deviceCode: ${{ steps.Authenticate.outputs.deviceCode }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Initialize the workflow
id: init
uses: mazhelez/AL-Go/Actions/WorkflowInitialize@output-encoding
with:
shell: powershell
eventId: "DO0097"
- name: Read settings
id: ReadSettings
uses: mazhelez/AL-Go/Actions/ReadSettings@output-encoding
with:
shell: powershell
parentTelemetryScopeJson: ${{ steps.init.outputs.telemetryScopeJson }}
getEnvironments: ${{ github.event.inputs.environmentName }}
includeProduction: 'Y'
- name: EnvName
id: envName
if: steps.ReadSettings.outputs.UnknownEnvironment == 1
run: |
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0
$envName = '${{ fromJson(steps.ReadSettings.outputs.environmentsJson).matrix.include[0].environment }}'.split(' ')[0]
Add-Content -Encoding UTF8 -Path $env:GITHUB_OUTPUT -Value "envName=$envName"
- name: Read secrets
uses: mazhelez/AL-Go/Actions/ReadSecrets@output-encoding
if: steps.ReadSettings.outputs.UnknownEnvironment == 1
env:
secrets: ${{ toJson(secrets) }}
with:
shell: powershell
parentTelemetryScopeJson: ${{ steps.init.outputs.telemetryScopeJson }}
settingsJson: ${{ env.Settings }}
secrets: '${{ steps.envName.outputs.envName }}-AuthContext,${{ steps.envName.outputs.envName }}_AuthContext,AuthContext'
- name: Authenticate
id: Authenticate
if: steps.ReadSettings.outputs.UnknownEnvironment == 1
run: |
$envName = '${{ steps.envName.outputs.envName }}'
$secretName = ''
$authContext = $null
"$($envName)-AuthContext", "$($envName)_AuthContext", "AuthContext" | ForEach-Object {
if (!($authContext)) {
$authContext = [System.Environment]::GetEnvironmentVariable($_)
if ($authContext) {
Write-Host "Using $_ secret as AuthContext"
$secretName = $_
}
}
}
if ($authContext) {
Write-Host "AuthContext provided in secret $secretName!"
Set-Content -Path $ENV:GITHUB_STEP_SUMMARY -value "AuthContext was provided in a secret called $secretName. Using this information for authentication."
}
else {
Write-Host "No AuthContext provided for $envName, initiating Device Code flow"
$ALGoHelperPath = "$([System.IO.Path]::GetTempFileName()).ps1"
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile('https://raw.githubusercontent.com/mazhelez/AL-Go/output-encoding/Actions/AL-Go-Helper.ps1', $ALGoHelperPath)
. $ALGoHelperPath
$BcContainerHelperPath = DownloadAndImportBcContainerHelper -baseFolder $ENV:GITHUB_WORKSPACE
$authContext = New-BcAuthContext -includeDeviceLogin -deviceLoginTimeout ([TimeSpan]::FromSeconds(0))
CleanupAfterBcContainerHelper -bcContainerHelperPath $bcContainerHelperPath
Set-Content -Path $ENV:GITHUB_STEP_SUMMARY -value "AL-Go needs access to the Business Central Environment $('${{ steps.envName.outputs.envName }}'.Split(' ')[0]) and could not locate a secret called ${{ steps.envName.outputs.envName }}_AuthContext`n`n$($authContext.message)"
Add-Content -Encoding UTF8 -Path $env:GITHUB_OUTPUT -Value "deviceCode=$($authContext.deviceCode)"
}
Deploy:
needs: [ Initialization ]
if: needs.Initialization.outputs.environmentCount > 0
strategy: ${{ fromJson(needs.Initialization.outputs.environments) }}
runs-on: ${{ fromJson(matrix.os) }}
name: Deploy to ${{ matrix.environment }}
environment:
name: ${{ matrix.environment }}
env:
deviceCode: ${{ needs.Initialization.outputs.deviceCode }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: EnvName
id: envName
run: |
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0
$envName = '${{ matrix.environment }}'.split(' ')[0]
Add-Content -Encoding UTF8 -Path $env:GITHUB_OUTPUT -Value "envName=$envName"
- name: Read settings
uses: mazhelez/AL-Go/Actions/ReadSettings@output-encoding
with:
shell: powershell
- name: Read secrets
uses: mazhelez/AL-Go/Actions/ReadSecrets@output-encoding
env:
secrets: ${{ toJson(secrets) }}
with:
shell: powershell
settingsJson: ${{ env.Settings }}
secrets: '${{ steps.envName.outputs.envName }}-AuthContext,${{ steps.envName.outputs.envName }}_AuthContext,AuthContext,${{ steps.envName.outputs.envName }}-EnvironmentName,${{ steps.envName.outputs.envName }}_EnvironmentName,EnvironmentName,projects'
- name: AuthContext
id: authContext
run: |
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0
$envName = '${{ steps.envName.outputs.envName }}'
$deployToSettingStr = [System.Environment]::GetEnvironmentVariable("DeployTo$envName")
if ($deployToSettingStr) {
$deployToSetting = $deployToSettingStr | ConvertFrom-Json
}
else {
$deployToSetting = [PSCustomObject]@{}
}
$authContext = $null
if ($env:deviceCode) {
$authContext = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("{""deviceCode"":""$($env:deviceCode)""}"))
}
"$($envName)-AuthContext", "$($envName)_AuthContext", "AuthContext" | ForEach-Object {
if (!($authContext)) {
$authContext = [System.Environment]::GetEnvironmentVariable($_)
if ($authContext) {
Write-Host "Using $_ secret as AuthContext"
}
}
}
if (!($authContext)) {
Write-Host "::Error::No AuthContext provided"
exit 1
}
if (("$deployToSetting" -ne "") -and $deployToSetting.PSObject.Properties.name -eq "EnvironmentName") {
$environmentName = $deployToSetting.EnvironmentName
}
else {
$environmentName = $null
"$($envName)-EnvironmentName", "$($envName)_EnvironmentName", "EnvironmentName" | ForEach-Object {
if (!($EnvironmentName)) {
$EnvironmentName = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String([System.Environment]::GetEnvironmentVariable($_)))
if ($EnvironmentName) {
Write-Host "Using $_ secret as EnvironmentName"
Write-Host "Please consider using the DeployTo$_ setting instead, where you can specify EnvironmentName, projects and branches"
}
}
}
}
if (!($environmentName)) {
$environmentName = '${{ steps.envName.outputs.envName }}'
}
$environmentName = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(($environmentName + '${{ matrix.environment }}'.SubString($envName.Length)).ToUpperInvariant()))
if (("$deployToSetting" -ne "") -and $deployToSetting.PSObject.Properties.name -eq "projects") {
$projects = $deployToSetting.projects
}
else {
$projects = [System.Environment]::GetEnvironmentVariable("$($envName)-projects")
if (-not $projects) {
$projects = [System.Environment]::GetEnvironmentVariable("$($envName)_Projects")
if (-not $projects) {
$projects = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String([System.Environment]::GetEnvironmentVariable('projects')))
}
}
}
if ($projects -eq '') {
$projects = '*'
}
elseif ($projects -ne '*') {
$buildProjects = '${{ needs.Initialization.outputs.projects }}' | ConvertFrom-Json
$projects = ($projects.Split(',') | Where-Object { $buildProjects -contains $_ }) -join ','
}
Add-Content -Encoding UTF8 -Path $env:GITHUB_OUTPUT -Value "authContext=$authContext"
Add-Content -Encoding UTF8 -Path $env:GITHUB_OUTPUT -Value "environmentName=$environmentName"
Write-Host "environmentName=$([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($environmentName)))"
Write-Host "environmentName (as Base64)=$environmentName"
Add-Content -Encoding UTF8 -Path $env:GITHUB_OUTPUT -Value "projects=$projects"
Write-Host "projects=$projects"
- name: Deploy
uses: mazhelez/AL-Go/Actions/Deploy@output-encoding
env:
AuthContext: ${{ steps.authContext.outputs.authContext }}
with:
shell: powershell
parentTelemetryScopeJson: ${{ needs.Initialization.outputs.telemetryScopeJson }}
type: 'Publish'
projects: ${{ steps.authContext.outputs.projects }}
environmentName: ${{ steps.authContext.outputs.environmentName }}
artifacts: ${{ github.event.inputs.appVersion }}
PostProcess:
if: always()
runs-on: [ windows-latest ]
needs: [ Initialization, Deploy ]
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Finalize the workflow
id: PostProcess
uses: mazhelez/AL-Go/Actions/WorkflowPostProcess@output-encoding
with:
shell: powershell
eventId: "DO0097"
telemetryScopeJson: ${{ needs.Initialization.outputs.telemetryScopeJson }}