helper images:
  tags:
    - saas-linux-2xlarge-amd64
  extends:
  - .docker
  - .rules:merge_request_pipelines:no_docs
  stage: build
  needs:
    - 'binaries'
  script:
  - ./ci/touch_git
  - make helper-images
  - ls -alh out/helper-images/
  retry: 2
  artifacts:
    paths:
    - out/helper-images/
    expire_in: 7d
  parallel:
    matrix:
      - TARGETS:
        - alpine alpine-pwsh ubuntu ubuntu-pwsh ubi-fips
        - windows-nanoserver-ltsc2019 windows-servercore-ltsc2019
        - windows-nanoserver-ltsc2022 windows-servercore-ltsc2022

prebuilt helper images:
  tags:
    - !reference [.instance-2xlarge]
  extends:
  - .docker
  - .rules:merge_request_pipelines:no_docs
  stage: build
  image: "${RUNNER_IMAGES_REGISTRY}/ci:${RUNNER_IMAGES_VERSION}-prebuilt-images"
  needs:
    - 'helper images: [alpine alpine-pwsh ubuntu ubuntu-pwsh ubi-fips]'
  script:
  - apt-get update -yq && apt-get install -yq parallel time p7zip-full
  - make prebuilt-helper-images
  - ls -alh out/helper-images/
  artifacts:
    paths:
    - out/helper-images/*.tar.xz
    expire_in: 7d

runner images:
  tags:
    - !reference [.instance-2xlarge]
  extends:
  - .docker
  - .rules:merge_request_pipelines:no_docs
  stage: build
  needs:
    - 'binaries'
  script:
  - ./ci/touch_git
  - TARGETS="ubuntu alpine ubi-fips" make runner-images
  - ls -alh out/runner-images/
  retry: 2
  artifacts:
    paths:
    - out/runner-images/
    expire_in: 7d

test images:
  extends:
  - .docker
  - .rules:build:test:images:merge-requests
  stage: build
  needs:
    - 'binaries'
  script:
  - docker buildx create --name builder --use --driver docker-container default || true
  - echo "${CI_REGISTRY_PASSWORD}" | docker login --username "${CI_REGISTRY_USER}" --password-stdin "${CI_REGISTRY}"
  - cd tests/dockerfiles && docker buildx bake --progress plain tests-images --set *.output="type=registry,compression=zstd"
  - docker logout "${CI_REGISTRY}"

binaries:
  image: "${RUNNER_IMAGES_REGISTRY}/ubi-go:${RUNNER_IMAGES_VERSION}"
  tags:
    - !reference [.instance-2xlarge]
  extends:
  - .rules:merge_request_pipelines:no_docs
  - .go-cache
  stage: build
  needs: []
  script:
  - go mod download
  - make -j$(($(nproc) * 2)) helper-bin helper-bin-fips runner-bin runner-bin-fips
  artifacts:
    paths:
    - out/binaries/gitlab-runner*
    expire_in: 7d

clone test repo:
  extends:
  - .rules:merge_request_pipelines:no_docs
  - .no_cache_and_dependencies
  stage: build
  image: alpine:latest
  needs: []
  variables:
    GIT_STRATEGY: none
  script:
  - apk add git
  - mkdir tmp
  - succeed=0
  - for i in {1..3}; do git clone https://gitlab.com/gitlab-org/ci-cd/gitlab-runner-pipeline-tests/gitlab-test tmp/gitlab-test && succeed=1 && break; echo "retrying"; done
  - '[[ "$succeed" -eq 1 ]]'
  artifacts:
    paths:
    - tmp/gitlab-test
    expire_in: 7d

# prepare done is used as a sentinel for "Prepare" stage completion, so we can kick off builds in later stages
# without waiting for the completion of the Prebuild stage
prepare done:
  stage: build
  extends:
  - .stage_done
