File: build_release_windows_images.ps1

package info (click to toggle)
gitlab-ci-multi-runner 14.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 31,248 kB
  • sloc: sh: 1,694; makefile: 384; asm: 79; ruby: 68
file content (310 lines) | stat: -rw-r--r-- 9,949 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
Set-StrictMode -Version latest
$ErrorActionPreference = "Stop"
$InformationPreference = "Continue"
# ---------------------------------------------------------------------------
# This script depends on a few environment variables that should be populated
# before running the script:
#
# - $Env:WINDOWS_VERSION - This is the version of windows that is going to be
#   used for building the Docker image. It is important for the version to match
#   one of the mcr.microsoft.com/windows/servercore or https://hub.docker.com/_/microsoft-windows-nanoserver
#   tag prefixes (discarding the architecture suffix).
#   For example, `servercoreYYH1` will build from mcr.microsoft.com/windows/servercore:YYH1-amd64.
# - $Env:GIT_VERSION - Specify which version of Git needs to be installed on
#   the Docker image. This is done through Docker build args.
# - $Env:GIT_VERSION_BUILD - Specify which build is needed to download for the
#   GIT_VERSION you specified.
# - $Env:GIT_WINDOWS_AMD64_CHECKSUM - The checksum of the downloaded zip, usually found in
#   the GitHub release page.
# - $Env:GIT_LFS_VERSION - The Git LFS version needed to install on the
#   Docker image.
# - $Env:GIT_LFS_WINDOWS_AMD64_CHECKSUM - The checksum of the downloaded .tar.gz file, usually
#   found in the GitHub release page.
# - $Env:PWSH_VERSION - The Powershell Core version needed to install on the
#   Docker image.
# - $Env:PWSH_WINDOWS_AMD64_CHECKSUM - The checksum of the downloaded MSI, usually
#   found in the GitHub release page.
# - $Env:IS_LATEST - When we want to tag current tag as the latest, this is usually
#   used when we are tagging a release for the runner (which is not a patch
#   release or RC)
# - $Env:DOCKER_HUB_USER - The user we want to login with for docker hub.
# - $Env:DOCKER_HUB_PASSWORD - The password we want to login with for docker hub.
# - $Env:PUSH_TO_DOCKER_HUB - If set to true, it will login to the registry and
#   push the tags.
# - $Env:SKIP_CLEANUP - By default this PowerShell script will delete the image
#   it just build.
# - $Env:DOCKER_HUB_NAMESPACE - Usually empty and only set for development, to
#   use your own namespace instead of `gitlab`.
# - $Env:CI_REGISTRY_IMAGE - Image name to push to GitLab registry. Usually set
#   by CI.
# - $Env:CI_REGISTRY - The GitLab registry name. Usually set by CI.
# - $Env:CI_REGISTRY_USER - The user used to login CI_REGISTRY. Usually set by
#   CI.
# - $Env:CI_REGISTRY_PASSWORD - The password used to login CI_REGISTRY. Usually
#   set by CI.
# - $Env:PUSH_TO_ECR_PUBLIC - If set to true, it will login to the registry and
#   push the tags.
# - $Env:ECR_PUBLIC_PASSWORD - The password used to login to ECR repository.
#   This is usually generated by `aws ecr-public get-login-password`
#   https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ecr-public/get-login-password.html
# - $Env:ECR_PUBLIC_REGISTRY - The ecr public registry. If it's not defined it
#   will fallback to the default production registry.
# ---------------------------------------------------------------------------
$imagesBasePath = "dockerfiles/runner-helper/Dockerfile.x86_64"

function Main
{
    if (-not (Test-Path Env:IS_LATEST))
    {
        $Env:IS_LATEST = Is-Latest
    }

    $tag = Get-Tag

    Build-Image $tag

    if ($Env:PUSH_TO_DOCKER_HUB -eq "true")
    {
        $namespace = DockerHub-Namespace

        Connect-Registry $Env:DOCKER_HUB_USER $Env:DOCKER_HUB_PASSWORD
        Push-Tag $namespace $tag
        Push-As-Ref $namespace $tag

        if ($Env:IS_LATEST -eq "true")
        {
            Push-As-Latest $namespace $tag
        }
        Disconnect-Registry
    }

    if ($Env:PUBLISH_IMAGES -eq "true")
    {
        Connect-Registry $Env:CI_REGISTRY_USER $Env:CI_REGISTRY_PASSWORD $Env:CI_REGISTRY

        Push-Tag "${Env:CI_REGISTRY_IMAGE}" $tag
        Push-As-Ref "${Env:CI_REGISTRY_IMAGE}" $tag

        if ($Env:IS_LATEST -eq "true")
        {
            Push-As-Latest $Env:CI_REGISTRY_IMAGE $tag
        }

        Disconnect-Registry $env:CI_REGISTRY
    }

    if ($Env:PUSH_TO_ECR_PUBLIC -eq "true")
    {
        $ecrPublicRegistry = ECR-Public-Registry

        Connect-Registry AWS $Env:ECR_PUBLIC_PASSWORD $ecrPublicRegistry

        Push-Tag $ecrPublicRegistry $tag
        Push-As-Ref $ecrPublicRegistry $tag

        if ($Env:IS_LATEST -eq "true")
        {
            Push-As-Latest $ecrPublicRegistry $tag
        }

        Disconnect-Registry $ecrPublicRegistry
    }
}

function Get-Tag
{
    $revision = & 'git' rev-parse --short=8 HEAD

    return "x86_64-$revision-$Env:WINDOWS_VERSION"
}

function Get-Latest-Stable-Tag
{
    $versions = & git -c versionsort.prereleaseSuffix="-rc" -c versionsort.prereleaseSuffix="-RC" tag -l "v*.*.*" |
        Where-Object { $_ -notlike "*-rc*" } |
        %{[System.Version]$_.Substring(1)} |
        sort -descending
    $latestTag = $versions[0].ToString()

    return "v$latestTag"
}

function Is-Latest
{
    $prevErrorPreference = $ErrorActionPreference
    $ErrorActionPreference = 'Continue' # We expect errors from `git describe`, so temporarily disable handling

    try
    {
        $latestTag = Get-Latest-Stable-Tag
        & git describe --exact-match --match $latestTag 2>&1 | out-null
        $isLatest = $LASTEXITCODE -eq 0
    }
    finally
    {
        $ErrorActionPreference = $prevErrorPreference
    }

    return $isLatest
}

function Build-Image($tag)
{
    $windowsFlavor = $env:WINDOWS_VERSION.Substring(0, $env:WINDOWS_VERSION.length -4)
    $windowsVersion = $env:WINDOWS_VERSION.Substring($env:WINDOWS_VERSION.length -4)
    $dockerHubNamespace = DockerHub-Namespace
    $ecrPublicRegistry = ECR-Public-Registry

    Write-Information "Build image for x86_64_${env:WINDOWS_VERSION}"

    $dockerFile = "${imagesBasePath}_${windowsFlavor}"
    $context = "dockerfiles/runner-helper"
    New-Item -ItemType Directory -Force -Path $context\binaries
    Copy-Item -Path "out\binaries\gitlab-runner-helper\gitlab-runner-helper.x86_64-windows.exe" -Destination "$context/binaries"
    $buildArgs = @(
        '--build-arg', "BASE_IMAGE_TAG=mcr.microsoft.com/windows/${windowsFlavor}:${windowsVersion}-amd64",
        '--build-arg', "PWSH_VERSION=$Env:PWSH_VERSION",
        '--build-arg', "PWSH_AMD64_CHECKSUM=$Env:PWSH_WINDOWS_AMD64_CHECKSUM",
        '--build-arg', "GIT_VERSION=$Env:GIT_VERSION",
        '--build-arg', "GIT_VERSION_BUILD=$Env:GIT_VERSION_BUILD",
        '--build-arg', "GIT_AMD64_CHECKSUM=$Env:GIT_WINDOWS_AMD64_CHECKSUM"
        '--build-arg', "GIT_LFS_VERSION=$Env:GIT_LFS_VERSION"
        '--build-arg', "GIT_LFS_AMD64_CHECKSUM=$Env:GIT_LFS_WINDOWS_AMD64_CHECKSUM"
    )

    $imageNames = @(
        '-t', "$dockerHubNamespace/gitlab-runner-helper:$tag",
        '-t', "$Env:CI_REGISTRY_IMAGE/gitlab-runner-helper:$tag",
        '-t', "$ecrPublicRegistry/gitlab-runner-helper:$tag"
    )

    & 'docker' build $imageNames --force-rm --no-cache $buildArgs -f $dockerFile $context
    if ($LASTEXITCODE -ne 0) {
        throw ("Failed to build docker image" )
    }
}

function Push-Tag($namespace, $tag)
{
    Write-Information "Push $tag"

    & 'docker' push ${namespace}/gitlab-runner-helper:$tag
    if ($LASTEXITCODE -ne 0) {
        throw ("Failed to push docker image ${namespace}/gitlab-runner-helper:$tag" )
    }
}

function Push-As-Latest($namespace, $tag)
{
    Push-As $namespace $tag "latest"
}

function Push-As-Ref($namespace, $tag)
{
    $ref = "${Env:CI_COMMIT_TAG}"
    if ($ref -eq "") {
       $ref = "${Env:CI_COMMIT_REF_SLUG}"
    }
    if($ref -eq "") {
        $ref = "main"
    }
    if($ref -eq "main") {
        $ref = "bleeding"
    }
    Push-As $namespace $tag $ref
}

function Push-As($namespace, $tag, $alias)
{
    $image = "${namespace}/gitlab-runner-helper:$tag"

    $newTag = "x86_64-${alias}-$Env:WINDOWS_VERSION"
    $newImage = "${namespace}/gitlab-runner-helper:$newTag"

    Write-Information "Tag $tag as $newTag"
    & 'docker' tag $image $newImage
    if ($LASTEXITCODE -ne 0) {
        throw ("Failed to tag $tag as $newTag" )
    }

    Write-Information "Push image $newImage"
    & 'docker' push $newImage
    if ($LASTEXITCODE -ne 0) {
        throw ("Failed to push image $newImage to registry" )
    }
}

function Push-Latest($namespace)
{
    Write-Information "Push latest tag"

    & 'docker' push "${namespace}/gitlab-runner-helper:x86_64-latest-$Env:WINDOWS_VERSION"
    if ($LASTEXITCODE -ne 0) {
        throw ("Failed to push image to registry" )
    }
}

function Connect-Registry($username, $password, $registry)
{
    Write-Information "Login registry $registry"

    & 'docker' login --username $username --password $password $registry
    if ($LASTEXITCODE -ne 0) {
        throw ("Failed to login Docker hub" )
    }
}

function Disconnect-Registry($registry)
{
    Write-Information "Logout registry $registry"

    & 'docker' logout $registry
    if ($LASTEXITCODE -ne 0) {
        throw ("Failed to logout from Docker hub" )
    }
}

function DockerHub-Namespace
{
    if(-not (Test-Path env:DOCKER_HUB_NAMESPACE))
    {
        return "gitlab"
    }

    return $Env:DOCKER_HUB_NAMESPACE
}

function ECR-Public-Registry
{
    if(-not (Test-Path env:ECR_PUBLIC_REGISTRY))
    {
        return "public.ecr.aws/gitlab"
    }

    return $Env:ECR_PUBLIC_REGISTRY
}

Try
{
    if (-not (Test-Path env:WINDOWS_VERSION))
    {
        throw '$Env:WINDOWS_VERSION is not set'
    }

    Main
}
Finally
{
    if (-not (Test-Path env:SKIP_CLEANUP))
    {
        Write-Information "Cleaning up the build image"
        $tag = Get-Tag
        $dockerHubNamespace = DockerHub-Namespace

        # We don't really care if these fail or not, clean up shouldn't fail
        # the pipelines.
        & 'docker' rmi -f $dockerHubNamespace/gitlab-runner-helper:$tag
        & 'docker' rmi -f $Env:CI_REGISTRY_IMAGE/gitlab-runner-helper:$tag
        & 'docker' image prune -f
    }
}