Skip to content

Cut 4324 policy group template #491

Cut 4324 policy group template

Cut 4324 policy group template #491

name: PowerShell Module CI
on:
pull_request:
# Sequence of patterns matched against refs/heads
branches:
- "master"
paths:
- "PowerShell/Deploy/**"
- "PowerShell/JumpCloud Module/**"
- "PowerShell/ModuleChangelog.md"
types: [opened, synchronize, reopened, labeled, unlabeled]
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
Filter-Branch:
runs-on: ubuntu-latest
if: contains(github.event.pull_request.labels.*.name, 'PowerShell Module')
steps:
- run: echo "Building JumpCloud Module Event 'JumpCloudModule_'"
Check-PR-Labels:
needs: ["Filter-Branch"]
runs-on: ubuntu-latest
outputs:
RELEASE_TYPE: ${{ steps.validate.outputs.RELEASE_TYPE }}
steps:
- name: Validate-PR-Version-Labels
id: validate
shell: pwsh
run: |
$PR_LABEL_LIST=$(curl -s "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels" | jq -r '.[].name')
if ("PowerShell Module" -in $PR_LABEL_LIST) {
write-host "Starting Build for PowerShell Module Release"
}
# validate type from label list:
$types = @('major', 'minor', 'patch', 'manual')
$typeCount = 0
foreach ($item in $PR_LABEL_LIST) {
if ($item -in $types) {
write-host "$item"
$typeCount += 1
$RELEASE_TYPE = $item
}
}
if ($typeCount -eq 1) {
echo "RELEASE_TYPE=$RELEASE_TYPE" >> $env:GITHUB_OUTPUT
} else {
throw "Multiple or invalid release types were found on PR"
exit 1
}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Validate-Env-Variables:
needs: ["Filter-Branch", "Check-PR-Labels"]
runs-on: ubuntu-latest
steps:
- env:
RELEASE_TYPE: ${{ needs.Check-PR-Labels.outputs.RELEASE_TYPE }}
shell: pwsh
run: |
# validate release type variables
$env:RELEASE_TYPE | Should -BeIn @('major','minor','patch','manual')
Setup-Build-Dependancies:
needs: ["Filter-Branch", "Check-PR-Labels", "Validate-Env-Variables"]
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- name: Setup PowerShell Module Cache
id: cacher
uses: actions/cache@v3
with:
path: "/home/runner/.local/share/powershell/Modules/"
key: PS-Dependancies
- name: Install dependencies
if: steps.cacher.outputs.cache-hit != 'true'
shell: pwsh
env:
PESTER_APIKEY: ${{ secrets.PESTER_APIKEY }}
PESTER_ORGID: ${{ secrets.PESTER_ORGID }}
PESTER_MSP_APIKEY: ${{ secrets.PESTER_MSP_APIKEY }}
run: |
Set-PSRepository PSGallery -InstallationPolicy Trusted
If (!(Get-PackageProvider -Name:('NuGet') -ListAvailable -ErrorAction:('SilentlyContinue'))) {
Write-Host ('[status]Installing package provider NuGet');
Install-PackageProvider -Name:('NuGet') -Scope:('CurrentUser') -Force
}
$PSDependencies = @{
'PowerShellGet' = @{Repository = 'PSGallery'; RequiredVersion = '3.0.12-beta' }
'PackageManagement' = @{Repository = 'PSGallery'; RequiredVersion = '1.4.8.1' }
'PSScriptAnalyzer' = @{Repository = 'PSGallery'; RequiredVersion = '1.19.1' }
'PlatyPS' = @{Repository = 'PSGallery'; RequiredVersion = '0.14.2' }
'AWS.Tools.Common' = @{Repository = 'PSGallery'; RequiredVersion = '4.1.122' }
'AWS.Tools.CodeArtifact' = @{Repository = 'PSGallery'; RequiredVersion = '4.1.122' }
'JumpCloud.SDK.V1' = @{Repository = 'PSGallery'; RequiredVersion = 'latest'}
'JumpCloud.SDK.V2' = @{Repository = 'PSGallery'; RequiredVersion = 'latest'}
'JumpCloud.SDK.DirectoryInsights' = @{Repository = 'PSGallery'; RequiredVersion = 'latest'}
'powershell-yaml' = @{Repository = 'PSGallery'; RequiredVersion = '0.4.7'}
}
foreach ($RequiredModule in $PSDependencies.Keys) {
If ([System.String]::IsNullOrEmpty((Get-InstalledModule | Where-Object { $_.Name -eq $RequiredModule }))) {
Write-Host("[status]Installing module: '$RequiredModule'; version: $($PSDependencies[$RequiredModule].RequiredVersion) from $($PSDependencies[$RequiredModule].Repository)")
if ($($PSDependencies[$RequiredModule].RequiredVersion) -eq "latest"){
Install-Module -Name $RequiredModule -Repository:($($PSDependencies[$RequiredModule].Repository)) -AllowPrerelease -Force
} else {
Install-Module -Name $RequiredModule -Repository:($($PSDependencies[$RequiredModule].Repository)) -RequiredVersion:($($PSDependencies[$RequiredModule].RequiredVersion)) -AllowPrerelease -Force
}
}
}
Validate-Module:
needs: ["Setup-Build-Dependancies", "Check-PR-Labels"]
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
with:
sparse-checkout: |
PowerShell
.github
- uses: actions/cache@v3
with:
path: "/home/runner/.local/share/powershell/Modules/"
key: PS-Dependancies
- env:
RELEASE_TYPE: ${{ needs.Check-PR-Labels.outputs.RELEASE_TYPE }}
shell: pwsh
run: |
. "./PowerShell/JumpCloud Module/Tests/InvokePester.ps1" -ModuleValidation
Setup-Org:
needs: ["Setup-Build-Dependancies", "Check-PR-Labels"]
runs-on: ubuntu-latest
name: Setup Org Upload Variable Artifact
steps:
- uses: actions/checkout@v4
with:
sparse-checkout: |
PowerShell
.github
- uses: actions/cache@v3
with:
path: "/home/runner/.local/share/powershell/Modules/"
key: PS-Dependancies
- name: Setup Org Variables
shell: pwsh
env:
PESTER_APIKEY: ${{ secrets.PESTER_APIKEY }}
PESTER_ORGID: ${{ secrets.PESTER_ORGID }}
PESTER_MSP_APIKEY: ${{ secrets.PESTER_MSP_APIKEY }}
run: |
# Setup Org
# $variableArray = New-Object System.Collections.Generic.List[PSCustomObject]
# Load Get-Config.ps1
. "./PowerShell/Deploy/Get-Config.ps1"
Write-Host ('[status] Setting up org')
$variables = . ("./PowerShell/JumpCloud Module/Tests/SetupOrg.ps1") -JumpCloudApiKey "$env:PESTER_APIKEY" -JumpCloudApiKeyMsp "$env:PESTER_MSP_APIKEY"
$variables | ConvertTo-Json -Depth 10 | Out-File -FilePath /home/runner/.local/share/powershell/Modules/PesterVariables.json
- uses: ./.github/actions/upload-secure-artifact
with:
name: jumpcloud-vars
path: /home/runner/.local/share/powershell/Modules/PesterVariables.json
Test-Module:
needs: ["Setup-Build-Dependancies", "Check-PR-Labels", "Setup-Org"]
runs-on: ubuntu-latest
timeout-minutes: 75
strategy:
fail-fast: false
matrix:
job_group: [0, 1, 2]
name: Run Pester Tests and Upload Results
steps:
- uses: actions/checkout@v4
with:
sparse-checkout: |
PowerShell
.github
- uses: actions/cache@v3
with:
path: "/home/runner/.local/share/powershell/Modules/"
key: PS-Dependancies
- uses: actions/download-artifact@v4
with:
name: jumpcloud-vars
- name: Test PWSH Module
shell: pwsh
env:
PESTER_APIKEY: ${{ secrets.PESTER_APIKEY }}
PESTER_ORGID: ${{ secrets.PESTER_ORGID }}
PESTER_MSP_APIKEY: ${{ secrets.PESTER_MSP_APIKEY }}
run: |
# Setup Variables for Pester Run
$env:job_group = ${{ matrix.job_group }}
$PesterParams = Get-Content -Raw ${{ github.workspace }}/PesterVariables.json | ConvertFrom-JSON
write-host "[status] Importing $($PesterParams.count) variables from SetupOrg"
$PesterParams | Foreach-Object {
if ($_.Name){
Write-Host ("[status] Setting variable $($_.Name) with value $($_.Value)")
Set-Variable -Name $_.Name -Value $_.Value -Scope Global
}
}
Set-Variable -Name PesterParams_ApiKey -Value "$env:PESTER_APIKEY" -Scope Global
# Import JC Module
Import-Module "${{github.workspace}}/PowerShell/JumpCloud Module/JumpCloud.psd1"
# Authenticate to JC Org
Connect-JCOnline -JumpCloudApiKey:("$env:PESTER_APIKEY") -force | Out-Null
# Invoke Pester
. "./PowerShell/JumpCloud Module/Tests/InvokePester.ps1" -JumpCloudApiKey "$env:PESTER_APIKEY" -ExcludeTagList "ModuleValidation", "JCDeployment", "MSP", "JCModule" -IncludeTagList "*" -RequiredModulesRepo "PSGallery"
Test-Module-MSP:
needs: ["Setup-Build-Dependancies", "Check-PR-Labels"]
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v4
with:
sparse-checkout: |
PowerShell
.github
- uses: actions/cache@v3
with:
path: "/home/runner/.local/share/powershell/Modules/"
key: PS-Dependancies
- shell: pwsh
env:
PESTER_APIKEY: ${{ secrets.PESTER_APIKEY }}
PESTER_ORGID: ${{ secrets.PESTER_ORGID }}
PESTER_MSP_APIKEY: ${{ secrets.PESTER_MSP_APIKEY }}
PESTER_PROVIDER_ID: ${{ secrets.PESTER_PROVIDER_ID }}
run: |
Set-Variable -Name PesterParams_ApiKeyMsp -Value "$env:PESTER_MSP_APIKEY" -Scope Global
. "./PowerShell/JumpCloud Module/Tests/InvokePester.ps1" -JumpCloudApiKeyMsp "$env:PESTER_MSP_APIKEY" -JumpCloudMspOrg "$env:PESTER_ORGID" -ProviderID "$env:PESTER_PROVIDER_ID" -RequiredModulesRepo "PSGallery" -MSP