Skip to content

deploy

deploy #112

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