Fix docker-less script execution #3
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: Swift Linux Matrix | ||
on: | ||
workflow_call: | ||
inputs: | ||
linux_exclude_swift_versions: | ||
type: string | ||
description: "Exclude Linux Swift version list (JSON)" | ||
default: "[{\"swift_version\": \"\"}]" | ||
linux_os_versions: | ||
type: string | ||
description: "Linux OS version list (JSON)" | ||
default: "[\"jammy\"]" | ||
windows_exclude_swift_versions: | ||
type: string | ||
description: "Exclude Windows Swift version list (JSON)" | ||
default: "[{\"swift_version\": \"\"}]" | ||
swift_flags: | ||
type: string | ||
description: "Swift flags for release version" | ||
default: "" | ||
swift_nightly_flags: | ||
type: string | ||
description: "Swift flags for nightly version" | ||
default: "" | ||
linux_pre_build_command: | ||
type: string | ||
description: "Linux command to execute before building the Swift package" | ||
default: "" | ||
linux_build_command: | ||
type: string | ||
description: "Linux command to build and test the package" | ||
default: "swift test" | ||
windows_pre_build_command: | ||
type: string | ||
description: "Windows Command Prompt command to execute before building the Swift package" | ||
default: "" | ||
windows_build_command: | ||
type: string | ||
description: | | ||
Windows Command Prompt command to build and test the package. | ||
Note that Powershell does not automatically exit if a subcommand fails. The Invoke-Program utility is available to propagate non-zero exit codes. | ||
It is strongly encouraged to run all command using `Invoke-Program` unless you want to continue on error eg. `Invoke-Program git apply patch.diff` instead of `git apply patch.diff`. | ||
default: "swift test" | ||
linux_env_vars: | ||
description: "List of environment variables" | ||
type: string | ||
windows_env_vars: | ||
description: "List of environment variables" | ||
type: string | ||
enable_linux_checks: | ||
type: boolean | ||
description: "Boolean to enable linux testing. Defaults to true" | ||
default: true | ||
enable_windows_checks: | ||
type: boolean | ||
description: "Boolean to enable windows testing. Defaults to true" | ||
default: true | ||
enable_windows_docker: | ||
type: boolean | ||
description: "Boolean to enable running build in windows docker container. Defaults to true" | ||
default: true | ||
jobs: | ||
linux-build: | ||
name: Linux (${{ matrix.swift_version }} - ${{ matrix.os_version }}) | ||
if: ${{ inputs.enable_linux_checks }} | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
swift_version: ['5.8', '5.9', '5.10', '6.0', 'nightly-main', 'nightly-6.0'] | ||
os_version: ${{ fromJson(inputs.linux_os_versions) }} | ||
exclude: | ||
- ${{ fromJson(inputs.linux_exclude_swift_versions) }} | ||
container: | ||
image: ${{ (contains(matrix.swift_version, 'nightly') && 'swiftlang/swift') || 'swift' }}:${{ matrix.swift_version }}-${{ matrix.os_version }} | ||
steps: | ||
- name: Swift version | ||
run: swift --version | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
- name: Set environment variables | ||
if: ${{ inputs.linux_env_vars }} | ||
run: | | ||
for i in "${{ inputs.linux_env_vars }}" | ||
do | ||
printf "%s\n" $i >> $GITHUB_ENV | ||
done | ||
- name: Pre-build | ||
run: ${{ inputs.linux_pre_build_command }} | ||
- name: Build / Test | ||
run: ${{ inputs.linux_build_command }} ${{ (contains(matrix.swift_version, 'nightly') && inputs.swift_nightly_flags) || inputs.swift_flags }} | ||
windows-build: | ||
name: Windows (${{ matrix.swift_version }} - ${{ contains(matrix.swift_version, 'nightly') && 'windows-2019' || 'windows-2022' }}) | ||
if: ${{ inputs.enable_windows_checks }} | ||
runs-on: ${{ contains(matrix.swift_version, 'nightly') && 'windows-2019' || 'windows-2022' }} | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
swift_version: ['5.9', '6.0', 'nightly', 'nightly-6.0'] | ||
exclude: | ||
- ${{ fromJson(inputs.windows_exclude_swift_versions) }} | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
- name: Set environment variables | ||
if: ${{ inputs.windows_env_vars }} | ||
run: | | ||
$lines = "${{ inputs.windows_env_vars }}" -split "`r`n" | ||
foreach ($line in $lines) { | ||
echo $line | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append | ||
} | ||
- name: Pull Docker image | ||
id: pull_docker_image | ||
if: ${{ inputs.enable_windows_docker }} | ||
run: | | ||
if ("${{ matrix.swift_version }}".Contains("nightly")) { | ||
$Image = "swiftlang/swift:${{ matrix.swift_version }}-windowsservercore-1809" | ||
} else { | ||
$Image = "swift:${{ matrix.swift_version }}-windowsservercore-ltsc2022" | ||
} | ||
docker pull $Image | ||
echo "image=$Image" >> "$env:GITHUB_OUTPUT" | ||
- name: Install Visual Studio Build Tools | ||
if: ${{ !inputs.enable_windows_docker }} | ||
run: | | ||
Invoke-WebRequest -Uri https://raw.githubusercontent.com/award999/github-workflows/refs/heads/dockerless-windows/.github/workflows/scripts/windows/install-vsb.ps1 -OutFile $env:TEMP\install-vsb.ps1 | ||
. $env:TEMP\install-vsb.ps1 | ||
del $env:TEMP\install-vsb.ps1 | ||
- name: Install Swift | ||
if: ${{ !inputs.enable_windows_docker }} | ||
run: | | ||
Invoke-WebRequest -Uri https://raw.githubusercontent.com/award999/github-workflows/refs/heads/dockerless-windows/.github/workflows/scripts/windows/swift/install-swift.ps1 -OutFile $env:TEMP\install-swift.ps1 | ||
Invoke-WebRequest -Uri https://raw.githubusercontent.com/award999/github-workflows/refs/heads/dockerless-windows/.github/workflows/scripts/windows/swift/install-swift-${{ matrix.swift_version }}.ps1 -OutFile $env:TEMP\install-swift-${{ matrix.swift_version }}.ps1 | ||
. $env:TEMP\install-swift-${{ matrix.swift_version }}.ps1 | ||
del $env:TEMP\install-swift*.ps1 | ||
- name: Create test script | ||
run: | | ||
mkdir $env:TEMP\test-script | ||
if ("${{ inputs.enable_windows_docker }}" -eq "true") { | ||
$Source = C:\source | ||
} else { | ||
$Source = $env:GITHUB_WORKSPACE | ||
} | ||
echo @' | ||
Set-PSDebug -Trace 1 | ||
# Run the command following `Invoke-Program`. | ||
# If that command returns a non-zero exit code, return the same exit code from this script. | ||
function Invoke-Program($Executable) { | ||
& $Executable @args | ||
if ($LastExitCode -ne 0) { | ||
exit $LastExitCode | ||
} | ||
} | ||
Invoke-Program swift --version | ||
Invoke-Program swift test --version | ||
Invoke-Program cd $Source | ||
${{ inputs.windows_pre_build_command }} | ||
Invoke-Program ${{ inputs.windows_build_command }} ${{ (contains(matrix.swift_version, 'nightly') && inputs.swift_nightly_flags) || inputs.swift_flags }} | ||
'@ >> $env:TEMP\test-script\run.ps1 | ||
# Docker build | ||
- name: Docker Build / Test | ||
timeout-minutes: 60 | ||
if: ${{ inputs.enable_windows_docker }} | ||
run: | | ||
docker run -v ${{ github.workspace }}:C:\source -v $env:TEMP\test-script:C:\test-script ${{ steps.pull_docker_image.outputs.image }} powershell.exe -NoLogo -File C:\test-script\run.ps1 | ||
# Docker-less build | ||
- name: Build / Test | ||
timeout-minutes: 60 | ||
if: ${{ !inputs.enable_windows_docker }} | ||
run: | | ||
refreshenv | ||
powershell.exe -NoLogo -File $env:TEMP\test-script\run.ps1; exit $LastExitCode |