Publish To Environment #5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | |
actions: read | |
contents: read | |
id-token: write | |
defaults: | |
run: | |
shell: powershell | |
env: | |
ALGoOrgSettings: ${{ vars.ALGoOrgSettings }} | |
ALGoRepoSettings: ${{ vars.ALGoRepoSettings }} | |
jobs: | |
Initialization: | |
needs: [ ] | |
runs-on: [ windows-latest ] | |
outputs: | |
environmentsMatrixJson: ${{ steps.DetermineDeploymentEnvironments.outputs.EnvironmentsMatrixJson }} | |
environmentCount: ${{ steps.DetermineDeploymentEnvironments.outputs.EnvironmentCount }} | |
deploymentEnvironmentsJson: ${{ steps.DetermineDeploymentEnvironments.outputs.DeploymentEnvironmentsJson }} | |
deviceCode: ${{ steps.Authenticate.outputs.deviceCode }} | |
telemetryScopeJson: ${{ steps.init.outputs.telemetryScopeJson }} | |
steps: | |
- name: Dump Workflow Information | |
uses: microsoft/AL-Go/Actions/DumpWorkflowInfo@a46044fa1ffca4b1ff05d2d1844fd7d5922c6984 | |
with: | |
shell: powershell | |
- name: Checkout | |
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
- name: Initialize the workflow | |
id: init | |
uses: microsoft/AL-Go/Actions/WorkflowInitialize@a46044fa1ffca4b1ff05d2d1844fd7d5922c6984 | |
with: | |
shell: powershell | |
- name: Read settings | |
id: ReadSettings | |
uses: microsoft/AL-Go/Actions/ReadSettings@a46044fa1ffca4b1ff05d2d1844fd7d5922c6984 | |
with: | |
shell: powershell | |
- name: Determine Deployment Environments | |
id: DetermineDeploymentEnvironments | |
uses: microsoft/AL-Go/Actions/DetermineDeploymentEnvironments@a46044fa1ffca4b1ff05d2d1844fd7d5922c6984 | |
env: | |
GITHUB_TOKEN: ${{ github.token }} | |
with: | |
shell: powershell | |
getEnvironments: ${{ github.event.inputs.environmentName }} | |
type: 'Publish' | |
- name: EnvName | |
id: envName | |
if: steps.DetermineDeploymentEnvironments.outputs.UnknownEnvironment == 1 | |
run: | | |
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0 | |
$envName = '${{ fromJson(steps.DetermineDeploymentEnvironments.outputs.environmentsMatrixJson).matrix.include[0].environment }}'.split(' ')[0] | |
Add-Content -Encoding UTF8 -Path $env:GITHUB_OUTPUT -Value "envName=$envName" | |
- name: Read secrets | |
id: ReadSecrets | |
uses: microsoft/AL-Go/Actions/ReadSecrets@a46044fa1ffca4b1ff05d2d1844fd7d5922c6984 | |
if: steps.DetermineDeploymentEnvironments.outputs.UnknownEnvironment == 1 | |
with: | |
shell: powershell | |
gitHubSecrets: ${{ toJson(secrets) }} | |
getSecrets: '${{ steps.envName.outputs.envName }}-AuthContext,${{ steps.envName.outputs.envName }}_AuthContext,AuthContext' | |
- name: Authenticate | |
id: Authenticate | |
if: steps.DetermineDeploymentEnvironments.outputs.UnknownEnvironment == 1 | |
run: | | |
$envName = '${{ steps.envName.outputs.envName }}' | |
$secretName = '' | |
$secrets = '${{ steps.ReadSecrets.outputs.Secrets }}' | ConvertFrom-Json | |
$authContext = $null | |
"$($envName)-AuthContext", "$($envName)_AuthContext", "AuthContext" | ForEach-Object { | |
if (!($authContext)) { | |
if ($secrets."$_") { | |
Write-Host "Using $_ secret as AuthContext" | |
$authContext = $secrets."$_" | |
$secretName = $_ | |
} | |
} | |
} | |
if ($authContext) { | |
Write-Host "AuthContext provided in secret $secretName!" | |
Add-Content -Encoding UTF8 -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/microsoft/AL-Go/a46044fa1ffca4b1ff05d2d1844fd7d5922c6984/Actions/AL-Go-Helper.ps1', $ALGoHelperPath) | |
. $ALGoHelperPath | |
DownloadAndImportBcContainerHelper | |
$authContext = New-BcAuthContext -includeDeviceLogin -deviceLoginTimeout ([TimeSpan]::FromSeconds(0)) | |
Add-Content -Encoding UTF8 -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.environmentsMatrixJson) }} | |
runs-on: ${{ fromJson(matrix.os) }} | |
name: Deploy to ${{ matrix.environment }} | |
defaults: | |
run: | |
shell: ${{ matrix.shell }} | |
environment: | |
name: ${{ matrix.environment }} | |
url: ${{ steps.Deploy.outputs.environmentUrl }} | |
env: | |
deviceCode: ${{ needs.Initialization.outputs.deviceCode }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
- 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: microsoft/AL-Go/Actions/ReadSettings@a46044fa1ffca4b1ff05d2d1844fd7d5922c6984 | |
with: | |
shell: ${{ matrix.shell }} | |
get: type,powerPlatformSolutionFolder | |
- name: Read secrets | |
id: ReadSecrets | |
uses: microsoft/AL-Go/Actions/ReadSecrets@a46044fa1ffca4b1ff05d2d1844fd7d5922c6984 | |
with: | |
shell: ${{ matrix.shell }} | |
gitHubSecrets: ${{ toJson(secrets) }} | |
getSecrets: '${{ steps.envName.outputs.envName }}-AuthContext,${{ steps.envName.outputs.envName }}_AuthContext,AuthContext' | |
- name: Get Artifacts for deployment | |
uses: microsoft/AL-Go/Actions/GetArtifactsForDeployment@a46044fa1ffca4b1ff05d2d1844fd7d5922c6984 | |
with: | |
shell: ${{ matrix.shell }} | |
artifactsVersion: ${{ github.event.inputs.appVersion }} | |
artifactsFolder: '.artifacts' | |
- name: Deploy to Business Central | |
id: Deploy | |
uses: microsoft/AL-Go/Actions/Deploy@a46044fa1ffca4b1ff05d2d1844fd7d5922c6984 | |
env: | |
Secrets: '${{ steps.ReadSecrets.outputs.Secrets }}' | |
with: | |
shell: ${{ matrix.shell }} | |
environmentName: ${{ matrix.environment }} | |
artifactsFolder: '.artifacts' | |
type: 'Publish' | |
deploymentEnvironmentsJson: ${{ needs.Initialization.outputs.deploymentEnvironmentsJson }} | |
- name: Deploy to Power Platform | |
if: env.type == 'PTE' && env.powerPlatformSolutionFolder != '' | |
uses: microsoft/AL-Go/Actions/DeployPowerPlatform@a46044fa1ffca4b1ff05d2d1844fd7d5922c6984 | |
env: | |
Secrets: '${{ steps.ReadSecrets.outputs.Secrets }}' | |
with: | |
shell: ${{ matrix.shell }} | |
environmentName: ${{ matrix.environment }} | |
artifactsFolder: '.artifacts' | |
deploymentEnvironmentsJson: ${{ needs.Initialization.outputs.deploymentEnvironmentsJson }} | |
PostProcess: | |
needs: [ Initialization, Deploy ] | |
if: always() | |
runs-on: [ windows-latest ] | |
steps: | |
- name: Checkout | |
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
- name: Finalize the workflow | |
id: PostProcess | |
uses: microsoft/AL-Go/Actions/WorkflowPostProcess@a46044fa1ffca4b1ff05d2d1844fd7d5922c6984 | |
env: | |
GITHUB_TOKEN: ${{ github.token }} | |
with: | |
shell: powershell | |
telemetryScopeJson: ${{ needs.Initialization.outputs.telemetryScopeJson }} | |
currentJobContext: ${{ toJson(job) }} |