diff --git a/Jenkinsfile b/Jenkinsfile index 82298c6..206b7a7 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -3,11 +3,13 @@ // // Linux agent requirements: // - git -// - PowerShell 7.2.11+ (`pwsh`) -// - network access to PowerShell Gallery on first run +// - curl +// - tar +// - sha256sum +// - network access to GitHub and PowerShell Gallery // -// PSScriptAnalyzer is installed into the Jenkins user's CurrentUser scope -// when absent, so no root privileges are required. +// PowerShell is downloaded as a portable archive into the workspace. +// PSScriptAnalyzer is installed into a workspace-local module directory. pipeline { agent { @@ -22,21 +24,96 @@ pipeline { environment { POWERSHELL_TELEMETRY_OPTOUT = '1' + + // Pin the CI runtime for reproducible builds. + PWSH_VERSION = '7.6.6' + PWSH_HOME = "${WORKSPACE}/.tools/powershell-7.6.6" + PWSH = "${WORKSPACE}/.tools/powershell-7.6.6/pwsh" + + // Keep installed PowerShell modules inside the Jenkins workspace. + PS_MODULE_HOME = "${WORKSPACE}/.tools/psmodules" } stages { + stage('Bootstrap PowerShell') { + steps { + sh ''' + set -eu + + mkdir -p "$WORKSPACE/.tools" + + ARCH="$(uname -m)" + + case "$ARCH" in + x86_64|amd64) + PWSH_ARCH="x64" + PWSH_SHA256="DDBC4A2D113BBD46D283CFEDCBCD117A70CAEFD7673F41F2B4E0000BADF103BC" + ;; + aarch64|arm64) + PWSH_ARCH="arm64" + PWSH_SHA256="924829E54C983648F6F1419A2DC7F9433C861B2FB5BD57736FF096C24F133729" + ;; + *) + echo "Unsupported Linux architecture: $ARCH" >&2 + exit 1 + ;; + esac + + PACKAGE="powershell-${PWSH_VERSION}-linux-${PWSH_ARCH}.tar.gz" + URL="https://github.com/PowerShell/PowerShell/releases/download/v${PWSH_VERSION}/${PACKAGE}" + ARCHIVE="$WORKSPACE/.tools/$PACKAGE" + + if [ -x "$PWSH" ]; then + CURRENT_VERSION="$($PWSH -NoLogo -NoProfile -Command '$PSVersionTable.PSVersion.ToString()')" + + if [ "$CURRENT_VERSION" = "$PWSH_VERSION" ]; then + echo "Using cached PowerShell $CURRENT_VERSION" + "$PWSH" --version + exit 0 + fi + + echo "Cached PowerShell version is $CURRENT_VERSION; reinstalling $PWSH_VERSION." + rm -rf "$PWSH_HOME" + fi + + echo "Downloading PowerShell $PWSH_VERSION for $PWSH_ARCH..." + curl \ + --fail \ + --location \ + --retry 3 \ + --retry-delay 2 \ + --output "$ARCHIVE" \ + "$URL" + + echo "Verifying SHA-256..." + printf '%s %s\n' "$PWSH_SHA256" "$ARCHIVE" | sha256sum --check - + + rm -rf "$PWSH_HOME" + mkdir -p "$PWSH_HOME" + tar -xzf "$ARCHIVE" -C "$PWSH_HOME" + chmod 755 "$PWSH" + rm -f "$ARCHIVE" + + echo "PowerShell installed locally:" + "$PWSH" --version + ''' + } + } + stage('Environment') { steps { sh ''' set -eu echo "PowerShell:" - command -v pwsh - pwsh --version + "$PWSH" --version echo "Repository:" git --version git status --short + + echo "Architecture:" + uname -m ''' } } @@ -46,7 +123,7 @@ pipeline { sh ''' set -eu - pwsh -NoLogo -NoProfile -NonInteractive -Command ' + "$PWSH" -NoLogo -NoProfile -NonInteractive -Command ' $ErrorActionPreference = "Stop" $files = @(Get-ChildItem -Path . -Recurse -File | @@ -101,16 +178,22 @@ pipeline { steps { sh ''' set -eu + mkdir -p "$PS_MODULE_HOME" - pwsh -NoLogo -NoProfile -NonInteractive -Command ' + "$PWSH" -NoLogo -NoProfile -NonInteractive -Command ' $ErrorActionPreference = "Stop" $ProgressPreference = "SilentlyContinue" + $moduleHome = $env:PS_MODULE_HOME + $separator = [IO.Path]::PathSeparator + $env:PSModulePath = $moduleHome + $separator + $env:PSModulePath + if (-not (Get-Module -ListAvailable -Name PSScriptAnalyzer)) { - Write-Host "Installing PSScriptAnalyzer..." - Set-PSRepository PSGallery -InstallationPolicy Trusted - Install-Module PSScriptAnalyzer ` - -Scope CurrentUser ` + Write-Host "Installing PSScriptAnalyzer into $moduleHome..." + + Save-Module PSScriptAnalyzer ` + -Path $moduleHome ` + -Repository PSGallery ` -Force ` -ErrorAction Stop } @@ -156,8 +239,8 @@ pipeline { $warnings.Count ) - // Warnings are reported but do not fail CI. - // Static analyzer errors do fail the build. + # Warnings are reported but do not fail CI. + # Static analyzer errors do fail the build. if ($errors.Count -gt 0) { throw "PSScriptAnalyzer reported errors." } @@ -171,7 +254,7 @@ pipeline { sh ''' set -eu - pwsh -NoLogo -NoProfile -NonInteractive -Command ' + "$PWSH" -NoLogo -NoProfile -NonInteractive -Command ' $ErrorActionPreference = "Stop" $script = Get-Content `