Skip to content

Build and Publish Module #3

Build and Publish Module

Build and Publish Module #3

Workflow file for this run

name: Build and Publish Module
env:
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true
on:
push:
tags:
- 'v*'
workflow_dispatch:
permissions:
contents: write
jobs:
build:
name: Build Module
runs-on: windows-latest
if: startsWith(github.ref, 'refs/tags/v')
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Validate tag source
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
$tagName = $env:GITHUB_REF_NAME
$tagCommit = $env:GITHUB_SHA
if ($tagName -match '-(alpha|beta|rc)') {
Write-Host "Pre-release tag detected: $tagName"
$developCommit = (git rev-parse origin/develop)
$mergeBase = (git merge-base $tagCommit $developCommit)
if ($mergeBase -ne $tagCommit) {
throw 'Pre-release tag commit is not reachable from develop.'
}
Write-Host 'Tag commit is reachable from develop.'
} else {
Write-Host "Stable tag detected: $tagName"
$mainCommit = (git rev-parse origin/main)
$mergeBase = (git merge-base $tagCommit $mainCommit)
if ($mergeBase -ne $tagCommit) {
throw 'Stable tag commit is not reachable from main.'
}
Write-Host 'Tag commit is reachable from main.'
}
- name: Bootstrap environment
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
& "$env:GITHUB_WORKSPACE\DevOps\Build\bootstrap.ps1"
- name: Build module
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
& "$env:GITHUB_WORKSPACE\DevOps\Build\build.ps1" -TaskNames 'clean', 'build'
- name: Generate documentation
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
& "$env:GITHUB_WORKSPACE\DevOps\Build\build.ps1" -TaskNames 'updateHelp', 'publishDocs'
- name: Upload build artifacts
uses: actions/upload-artifact@v6
with:
name: module-build
path: Output/
retention-days: 30
- name: Upload documentation
uses: actions/upload-artifact@v6
with:
name: module-docs
path: .build/docs/
retention-days: 30
publish:
name: Publish Module
runs-on: windows-latest
needs: build
if: startsWith(github.ref, 'refs/tags/v')
environment:
name: production
url: https://www.powershellgallery.com/packages/HaloAPI
env:
TF_BUILD: true
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Bootstrap environment
shell: pwsh
working-directory: ${{ github.workspace }}
run: |
$ErrorActionPreference = 'Stop'
& "$env:GITHUB_WORKSPACE\DevOps\Build\bootstrap.ps1"
- name: Download build artifacts
shell: pwsh
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
$ErrorActionPreference = 'Stop'
gh run download ${{ github.run_id }} --name module-build --dir Output
- name: Package module for release
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
$tagName = "$env:GITHUB_REF_NAME"
$zipName = "HaloAPI-$tagName.zip"
$zipPath = Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath $zipName
if (Test-Path -Path $zipPath) {
Remove-Item -Path $zipPath -Force
}
Compress-Archive -Path (Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath 'Output\*') -DestinationPath $zipPath
- name: Validate PowerShell Gallery secret
shell: pwsh
env:
PSGalleryAPIKey: ${{ secrets.PSGALLERY_API_KEY }}
run: |
$ErrorActionPreference = 'Stop'
if ([string]::IsNullOrWhiteSpace($env:PSGalleryAPIKey)) {
throw 'The PSGALLERY_API_KEY secret is required to publish releases.'
}
- name: Publish to PowerShell Gallery
shell: pwsh
env:
PSGalleryAPIKey: ${{ secrets.PSGALLERY_API_KEY }}
run: |
$ErrorActionPreference = 'Stop'
$moduleManifest = Get-ChildItem -Path "$env:GITHUB_WORKSPACE\Output" -Recurse -Filter 'HaloAPI.psd1' | Select-Object -First 1
if (-not $moduleManifest) {
throw 'Module manifest not found under Output/. Ensure build artifacts are present.'
}
$manifest = Test-ModuleManifest -Path $moduleManifest.FullName
$version = $manifest.Version.ToString()
if ($manifest.PrivateData.PSData.Prerelease) {
$version = "$version-$($manifest.PrivateData.PSData.Prerelease)"
}
Write-Host "Checking if HaloAPI version $version already exists on PSGallery..."
try {
$existingModule = Find-Module -Name 'HaloAPI' -RequiredVersion $version -AllowPrerelease -ErrorAction SilentlyContinue
if ($existingModule) {
Write-Host "::warning::Module version $version already exists on PSGallery. Skipping publish."
exit 0
}
} catch {
Write-Host "Module version $version not found on PSGallery. Proceeding with publish."
}
& "$env:GITHUB_WORKSPACE\DevOps\Build\build.ps1" -TaskNames 'publish'