| 1 |
# Copyright 2019-2022, Mesa contributors |
| 2 |
# Copyright 2022, Collabora, Ltd. |
| 3 |
# SPDX-License-Identifier: MIT |
| 4 |
# Based on https://gitlab.freedesktop.org/mesa/mesa/-/blob/8396df5ad90aeb6ab2267811aba2187954562f81/.gitlab-ci/windows/mesa_container.ps1 |
| 5 |
|
| 6 |
# Implements the equivalent of ci-templates container-ifnot-exists, using |
| 7 |
# Docker directly as we don't have buildah/podman/skopeo available under |
| 8 |
# Windows, nor can we execute Docker-in-Docker |
| 9 |
[CmdletBinding()] |
| 10 |
param ( |
| 11 |
# Address for container registry |
| 12 |
[Parameter()] |
| 13 |
[string] |
| 14 |
$RegistryUri, |
| 15 |
|
| 16 |
# Username for container registry |
| 17 |
[Parameter()] |
| 18 |
[ValidateNotNullOrEmpty()] |
| 19 |
[string] |
| 20 |
$RegistryUsername, |
| 21 |
|
| 22 |
# The path of the image for this user's fork |
| 23 |
[Parameter()] |
| 24 |
[ValidateNotNullOrEmpty()] |
| 25 |
[string] |
| 26 |
$UserImage, |
| 27 |
|
| 28 |
# The path of the image in the upstream registry |
| 29 |
[Parameter()] |
| 30 |
[string] |
| 31 |
$UpstreamImage, |
| 32 |
|
| 33 |
# Dockerfile to build |
| 34 |
[Parameter()] |
| 35 |
[string] |
| 36 |
$Dockerfile = "Dockerfile", |
| 37 |
|
| 38 |
# Base image to use for this container, if any |
| 39 |
[Parameter()] |
| 40 |
[string] |
| 41 |
$BaseImage, |
| 42 |
|
| 43 |
# Base image to use for this container, from the upstream repo, if any |
| 44 |
[Parameter()] |
| 45 |
[string] |
| 46 |
$BaseUpstreamImage |
| 47 |
) |
| 48 |
|
| 49 |
$RegistryPassword = "$env:CI_REGISTRY_PASSWORD" |
| 50 |
|
| 51 |
$CommonDockerArgs = @( |
| 52 |
"--config" |
| 53 |
"windows-docker.conf" |
| 54 |
) |
| 55 |
|
| 56 |
$ErrorActionPreference = 'Stop' |
| 57 |
|
| 58 |
# Returns $true on a zero error code |
| 59 |
# If $AllowFailure is not set, throws on a nonzero exit code |
| 60 |
function Start-Docker { |
| 61 |
param ( |
| 62 |
# Should we just return the exit code on failure instead of throwing? |
| 63 |
[Parameter()] |
| 64 |
[switch] |
| 65 |
$AllowFailure = $false, |
| 66 |
|
| 67 |
# Should we try to log out before throwing in case of an error? |
| 68 |
[Parameter()] |
| 69 |
[switch] |
| 70 |
$LogoutOnFailure = $false, |
| 71 |
|
| 72 |
# What arguments should be passed to docker (besides the config) |
| 73 |
[Parameter(Mandatory = $true)] |
| 74 |
[string[]] |
| 75 |
$ArgumentList |
| 76 |
) |
| 77 |
$DockerArgs = $CommonDockerArgs + $ArgumentList |
| 78 |
Write-Verbose ("Will run docker " + ($DockerArgs -join " ")) |
| 79 |
$proc = Start-Process -FilePath "docker" -ArgumentList $DockerArgs -NoNewWindow -PassThru -WorkingDirectory "$PSScriptRoot" -Wait |
| 80 |
|
| 81 |
if ($proc.ExitCode -eq 0) { |
| 82 |
Write-Verbose "Success!" |
| 83 |
return $true |
| 84 |
} |
| 85 |
if (!$AllowFailure) { |
| 86 |
Write-Error ($ArgumentList[0] + " failed") |
| 87 |
if ($LogoutOnFailure) { |
| 88 |
Write-Host "Logging out" |
| 89 |
Start-Process -FilePath "docker" -ArgumentList ($CommonDockerArgs + @("logout", "$RegistryUri")) ` |
| 90 |
-NoNewWindow -PassThru -WorkingDirectory "$PSScriptRoot" -Wait |
| 91 |
} |
| 92 |
throw ("docker " + $ArgumentList[0] + " invocation failed") |
| 93 |
} |
| 94 |
|
| 95 |
return $false |
| 96 |
} |
| 97 |
|
| 98 |
# Returns $true if the $Image exists (whether or not we had to copy $UpstreamImage) |
| 99 |
function Test-Image { |
| 100 |
param ( |
| 101 |
# Image to look for |
| 102 |
[Parameter(Mandatory = $true)] |
| 103 |
[ValidateNotNullOrEmpty()] |
| 104 |
[string] |
| 105 |
$Image, |
| 106 |
|
| 107 |
# Equivalent image from the upstream repo, if any |
| 108 |
[Parameter()] |
| 109 |
[string] |
| 110 |
$UpstreamImage |
| 111 |
) |
| 112 |
|
| 113 |
# if the image already exists, great |
| 114 |
Write-Verbose "Looking for $Image" |
| 115 |
# $pullResult = Start-Docker -AllowFailure -ArgumentList ("pull", "$Image") |
| 116 |
docker @CommonDockerArgs pull "$Image" |
| 117 |
if ($?) { |
| 118 |
Write-Host "Image $UserImage exists" |
| 119 |
return $true |
| 120 |
} |
| 121 |
if (!$UpstreamImage) { |
| 122 |
Write-Host "Cannot find $Image" |
| 123 |
return $false |
| 124 |
} |
| 125 |
# if it's only upstream, copy it |
| 126 |
Write-Host "Cannot find $Image, looking for upstream $UpstreamImage" |
| 127 |
docker @CommonDockerArgs pull "$UpstreamImage" |
| 128 |
if ($?) { |
| 129 |
Write-Host "Found upstream image, copying image from upstream $UpstreamImage to user $Image" |
| 130 |
Start-Docker -LogoutOnFailure -ArgumentList ("tag", "$UpstreamImage", "$Image") |
| 131 |
Start-Docker -LogoutOnFailure -ArgumentList ("push", "$Image") |
| 132 |
return $true |
| 133 |
} |
| 134 |
Write-Host "Cannot find $Image nor $UpstreamImage" |
| 135 |
return $false |
| 136 |
} |
| 137 |
|
| 138 |
if ($BaseImage -and (!$BaseUpstreamImage)) { |
| 139 |
$BaseUpstreamImage = $BaseImage |
| 140 |
} |
| 141 |
|
| 142 |
Write-Host "Will log in to $RegistryUri as $RegistryUsername" |
| 143 |
Write-Host "Will check for image $UserImage - if it does not exist but $UpstreamImage does, we copy that one, otherwise we need to build it." |
| 144 |
if ($BaseImage) { |
| 145 |
Write-Host "This image builds on $BaseImage so we will check for it." |
| 146 |
if ($BaseUpstreamImage) { |
| 147 |
Write-Host "If it is missing but $BaseUpstreamImage exists, we copy that one. If both are missing, we error out." |
| 148 |
} |
| 149 |
else { |
| 150 |
Write-Host "If it is missing, we error out." |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
if ($RegistryPassword) { |
| 155 |
# Start-Docker -ArgumentList ("login", "-u", "$RegistryUsername", "--password-stdin", "$RegistryPassword", "$RegistryUri") |
| 156 |
$loginProc = Start-Process -FilePath "docker" -ArgumentList ($CommonDockerArgs + @("login", "-u", "$RegistryUsername", "--password", "$RegistryPassword", "$RegistryUri")) ` |
| 157 |
-NoNewWindow -PassThru -WorkingDirectory "$PSScriptRoot" -Wait |
| 158 |
if ($loginProc.ExitCode -ne 0) { |
| 159 |
throw "docker login failed" |
| 160 |
} |
| 161 |
} |
| 162 |
else { |
| 163 |
Write-Host "Skipping docker login, password not available" |
| 164 |
} |
| 165 |
|
| 166 |
# if the image already exists, don't rebuild it |
| 167 |
$imageResult = Test-Image -Image $UserImage -UpstreamImage $UpstreamImage |
| 168 |
if ($imageResult) { |
| 169 |
Write-Host "User image $UserImage already exists; not rebuilding" |
| 170 |
Start-Docker -ArgumentList ("logout", "$RegistryUri") |
| 171 |
Exit 0 |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
# do we need a base image? |
| 176 |
if ($BaseImage) { |
| 177 |
$baseImageResult = Test-Image -Image "$BaseImage" -UpstreamImage "$BaseUpstreamImage" |
| 178 |
if (!$baseImageResult) { |
| 179 |
throw "Could not find base image: neither '$BaseImage' nor '$BaseUpstreamImage' exist." |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
Write-Host "No image found at $UserImage or $UpstreamImage; rebuilding, this may take a while" |
| 184 |
$DockerBuildArgs = @( |
| 185 |
"build" |
| 186 |
"--no-cache" |
| 187 |
"-t" |
| 188 |
"$UserImage" |
| 189 |
"-f" |
| 190 |
"$Dockerfile" |
| 191 |
) |
| 192 |
|
| 193 |
if ($BaseImage) { |
| 194 |
$DockerBuildArgs += @( |
| 195 |
"--build-arg" |
| 196 |
"base_image=$BaseImage" |
| 197 |
) |
| 198 |
} |
| 199 |
|
| 200 |
$DockerBuildArgs += "." |
| 201 |
Start-Docker -LogoutOnFailure -ArgumentList (, $DockerBuildArgs) |
| 202 |
|
| 203 |
Get-Date |
| 204 |
|
| 205 |
Write-Host "Done building image, now pushing $UserImage" |
| 206 |
Start-Docker -LogoutOnFailure -ArgumentList ("push", "$UserImage") |
| 207 |
|
| 208 |
if ($RegistryPassword) { |
| 209 |
Start-Docker -ArgumentList ("logout", "$RegistryUri") |
| 210 |
} |
| 211 |
else { |
| 212 |
Write-Host "Skipping docker logout, password not available so we did not login" |
| 213 |
} |