1bf215546Sopenharmony_ci# Implements the equivalent of ci-templates container-ifnot-exists, using
2bf215546Sopenharmony_ci# Docker directly as we don't have buildah/podman/skopeo available under
3bf215546Sopenharmony_ci# Windows, nor can we execute Docker-in-Docker
4bf215546Sopenharmony_ci$registry_uri = $args[0]
5bf215546Sopenharmony_ci$registry_username = $args[1]
6bf215546Sopenharmony_ci$registry_password = $args[2]
7bf215546Sopenharmony_ci$registry_user_image = $args[3]
8bf215546Sopenharmony_ci$registry_central_image = $args[4]
9bf215546Sopenharmony_ci$build_dockerfile = $args[5]
10bf215546Sopenharmony_ci$registry_base_image = $args[6]
11bf215546Sopenharmony_ci
12bf215546Sopenharmony_ciSet-Location -Path ".\.gitlab-ci\windows"
13bf215546Sopenharmony_ci
14bf215546Sopenharmony_cidocker --config "windows-docker.conf" login -u "$registry_username" -p "$registry_password" "$registry_uri"
15bf215546Sopenharmony_ciif (!$?) {
16bf215546Sopenharmony_ci  Write-Host "docker login failed to $registry_uri"
17bf215546Sopenharmony_ci  Exit 1
18bf215546Sopenharmony_ci}
19bf215546Sopenharmony_ci
20bf215546Sopenharmony_ci# if the image already exists, don't rebuild it
21bf215546Sopenharmony_cidocker --config "windows-docker.conf" pull "$registry_user_image"
22bf215546Sopenharmony_ciif ($?) {
23bf215546Sopenharmony_ci  Write-Host "User image $registry_user_image already exists; not rebuilding"
24bf215546Sopenharmony_ci  docker --config "windows-docker.conf" logout "$registry_uri"
25bf215546Sopenharmony_ci  Exit 0
26bf215546Sopenharmony_ci}
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci# if the image already exists upstream, copy it
29bf215546Sopenharmony_cidocker --config "windows-docker.conf" pull "$registry_central_image"
30bf215546Sopenharmony_ciif ($?) {
31bf215546Sopenharmony_ci  Write-Host "Copying central image $registry_central_image to user image $registry_user_image"
32bf215546Sopenharmony_ci  docker --config "windows-docker.conf" tag "$registry_central_image" "$registry_user_image"
33bf215546Sopenharmony_ci  docker --config "windows-docker.conf" push "$registry_user_image"
34bf215546Sopenharmony_ci  $pushstatus = $?
35bf215546Sopenharmony_ci  docker --config "windows-docker.conf" logout "$registry_uri"
36bf215546Sopenharmony_ci  if (!$pushstatus) {
37bf215546Sopenharmony_ci    Write-Host "Pushing image to $registry_user_image failed"
38bf215546Sopenharmony_ci    Exit 1
39bf215546Sopenharmony_ci  }
40bf215546Sopenharmony_ci  Exit 0
41bf215546Sopenharmony_ci}
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ciWrite-Host "No image found at $registry_user_image or $registry_central_image; rebuilding"
44bf215546Sopenharmony_cidocker --config "windows-docker.conf" build --no-cache -t "$registry_user_image" -f "$build_dockerfile" --build-arg base_image="$registry_base_image" .
45bf215546Sopenharmony_ciif (!$?) {
46bf215546Sopenharmony_ci  Write-Host "Container build failed"
47bf215546Sopenharmony_ci  docker --config "windows-docker.conf" logout "$registry_uri"
48bf215546Sopenharmony_ci  Exit 1
49bf215546Sopenharmony_ci}
50bf215546Sopenharmony_ciGet-Date
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_cidocker --config "windows-docker.conf" push "$registry_user_image"
53bf215546Sopenharmony_ci$pushstatus = $?
54bf215546Sopenharmony_cidocker --config "windows-docker.conf" logout "$registry_uri"
55bf215546Sopenharmony_ciif (!$pushstatus) {
56bf215546Sopenharmony_ci  Write-Host "Pushing image to $registry_user_image failed"
57bf215546Sopenharmony_ci  Exit 1
58bf215546Sopenharmony_ci}
59