.env:
  variables:
    GHC_VERSION: "9.10.1"
    CABAL_VERSION: "3.12.1.0"
    STACK_VERSION: "3.1.1"
    HLS_VERSION: "2.9.0.1"
    BUILD_GHC: "true"
    BUILD_HLS: "false"
    TAG_VERSION_MAJ_MIN: "false"
    TAG_VERSION_MAJ: "false"

.before-script-build: &before-script-build
  - |
    if [[ ! -z "$CI_BUILD_PLATFORMS" ]]; then
      export OS_ARCH=`echo $CI_BUILD_PLATFORMS | tr ' ' '\n' | \
      sed 's|\/||2' | sed 's|\/|-|' | tr '\n' ' '`
    fi
  - |
    export CI_APP_REPO=${CI_APP_REPO:-$CI_REGISTRY_IMAGE}
    if [[ $CI_COMMIT_BRANCH == "main" ]]; then
      export CI_APP_TAG=${CI_APP_TAG:-latest}
    elif [[ $CI_COMMIT_BRANCH == "freeze-version" ]]; then
      export CI_APP_TAG=${CI_APP_TAG:-$GHC_VERSION}
    else
      export CI_APP_TAG=${CI_APP_TAG:-$CI_COMMIT_SHA}
    fi
  - >
    echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER
    --password-stdin $CI_REGISTRY
  - >
    export VERSION_MAJ_MIN_PAT=$GHC_VERSION

    export VERSION_MAJ_MIN=`echo $VERSION_MAJ_MIN_PAT |
    sed -n "s|\([[:digit:]]\+\.[[:digit:]]\+\).*|\1|p"`

    export VERSION_MAJ=`echo $VERSION_MAJ_MIN_PAT |
    sed -n "s|\([[:digit:]]\+\).*|\1|p"`

.before-script-mirror: &before-script-mirror
  - |
    if [[ ! -z "$DOCKER_HUB_REPO" ]]; then
      echo $DOCKER_HUB_PASSWORD | docker login -u $DOCKER_HUB_USER \
        --password-stdin docker.io
    fi
  - |
    if [[ ! -z "$QUAY_REPO" ]]; then
      echo $QUAY_PASSWORD | docker login -u $QUAY_USER \
        --password-stdin quay.io
    fi

.build:
  image: glcr.b-data.ch/docker/docker:dind
  extends: .env
  services:
    - name: glcr.b-data.ch/docker/docker:dind
      alias: docker
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: "/certs"
    BUILDX_NO_DEFAULT_ATTESTATIONS: "true"
  before_script:
    - *before-script-build
  retry: 2

.mirror:
  extends: .build
  before_script:
    - *before-script-build
    - *before-script-mirror

stages:
  - .pre
  - build1
  - build2
  - update
  - mirror
  - test
  - deploy
  - .post

build:latest-linux-amd64:
  extends: .build
  stage: build1
  tags:
    - arch:amd64
    - os:linux
  script:
    - >
      docker build
      --build-arg GHC_VERSION
      --build-arg CABAL_VERSION
      --build-arg STACK_VERSION
      --target test
      -t $CI_APP_REPO:$VERSION_MAJ_MIN_PAT-linux-amd64
      -t $CI_APP_REPO:$CI_APP_TAG-linux-amd64
      -f latest.Dockerfile .
    - >
      docker build
      --build-arg GHC_VERSION
      --build-arg CABAL_VERSION
      --build-arg STACK_VERSION
      -t $CI_APP_REPO:$VERSION_MAJ_MIN_PAT-linux-amd64
      -t $CI_APP_REPO:$CI_APP_TAG-linux-amd64
      -f latest.Dockerfile .
    - |
      docker push $CI_APP_REPO:$VERSION_MAJ_MIN_PAT-linux-amd64
      docker push $CI_APP_REPO:$CI_APP_TAG-linux-amd64
  rules:
    - if: $BUILD_GHC == "true" && $CI_COMMIT_BRANCH == "main" && $CI_BUILD_PLATFORMS =~ /(linux\/amd64)/
      changes:
        - latest.Dockerfile

build:latest-linux-arm64v8:
  extends: .build
  stage: build1
  tags:
    - arch:arm64/v8
    - os:linux
  script:
    - >
      docker build
      --build-arg GHC_VERSION
      --build-arg CABAL_VERSION
      --build-arg STACK_VERSION
      --target test
      -t $CI_APP_REPO:$VERSION_MAJ_MIN_PAT-linux-arm64v8
      -t $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
      -f latest.Dockerfile .
    - >
      docker build
      --build-arg GHC_VERSION
      --build-arg CABAL_VERSION
      --build-arg STACK_VERSION
      -t $CI_APP_REPO:$VERSION_MAJ_MIN_PAT-linux-arm64v8
      -t $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
      -f latest.Dockerfile .
    - |
      docker push $CI_APP_REPO:$VERSION_MAJ_MIN_PAT-linux-arm64v8
      docker push $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
  rules:
    - if: $BUILD_GHC == "true" && $CI_COMMIT_BRANCH == "main" && $CI_BUILD_PLATFORMS =~ /(linux\/arm64\/v8)/
      changes:
        - latest.Dockerfile

build:version-linux-amd64:
  extends: .build
  stage: build1
  tags:
    - arch:amd64
    - os:linux
  script:
    - >
      docker build
      --target test
      -t $CI_APP_REPO:$CI_APP_TAG-linux-amd64
      -f prior/$GHC_VERSION.Dockerfile .
    - >
      docker build
      -t $CI_APP_REPO:$CI_APP_TAG-linux-amd64
      -f prior/$GHC_VERSION.Dockerfile .
    - docker push $CI_APP_REPO:$CI_APP_TAG-linux-amd64
  rules:
    - if: $BUILD_GHC == "true" && $CI_COMMIT_BRANCH == "freeze-version" && $CI_BUILD_PLATFORMS =~ /(linux\/amd64)/
      changes:
        - prior/$GHC_VERSION.Dockerfile

build:version-linux-arm64v8:
  extends: .build
  stage: build1
  tags:
    - arch:arm64/v8
    - os:linux
  script:
    - >
      docker build
      --target test
      -t $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
      -f prior/$GHC_VERSION.Dockerfile .
    - >
      docker build
      -t $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
      -f prior/$GHC_VERSION.Dockerfile .
    - docker push $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
  rules:
    - if: $BUILD_GHC == "true" && $CI_COMMIT_BRANCH == "freeze-version" && $CI_BUILD_PLATFORMS =~ /(linux\/arm64\/v8)/
      changes:
        - prior/$GHC_VERSION.Dockerfile

build-manifest:latest-linux-multiarch:
  extends: .build
  stage: build1
  needs: ["build:latest-linux-amd64", "build:latest-linux-arm64v8"]
  script:
    - |
      for i in $OS_ARCH; do
        export CI_MANIFEST_LIST="$CI_MANIFEST_LIST $CI_APP_REPO:$CI_APP_TAG-$i"
      done
    - |
      docker manifest create $CI_APP_REPO:$VERSION_MAJ_MIN_PAT $CI_MANIFEST_LIST
      if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
        docker manifest annotate --variant v8 $CI_APP_REPO:$VERSION_MAJ_MIN_PAT \
          $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
      fi
      docker manifest push $CI_APP_REPO:$VERSION_MAJ_MIN_PAT
    - |
      docker manifest create $CI_APP_REPO:$VERSION_MAJ_MIN $CI_MANIFEST_LIST
      if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
        docker manifest annotate --variant v8 $CI_APP_REPO:$VERSION_MAJ_MIN \
          $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
      fi
      docker manifest push $CI_APP_REPO:$VERSION_MAJ_MIN
    - |
      docker manifest create $CI_APP_REPO:$VERSION_MAJ $CI_MANIFEST_LIST
      if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
        docker manifest annotate --variant v8 $CI_APP_REPO:$VERSION_MAJ \
          $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
      fi
      docker manifest push $CI_APP_REPO:$VERSION_MAJ
    - |
      docker manifest create $CI_APP_REPO:$CI_APP_TAG $CI_MANIFEST_LIST
      if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
        docker manifest annotate --variant v8 $CI_APP_REPO:$CI_APP_TAG \
          $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
      fi
      docker manifest push $CI_APP_REPO:$CI_APP_TAG
  rules:
    - if: $BUILD_GHC == "true" && $CI_COMMIT_BRANCH == "main"
      changes:
        - latest.Dockerfile

build-manifest:version-linux-multiarch:
  extends: .build
  stage: build1
  needs: ["build:version-linux-amd64", "build:version-linux-arm64v8"]
  script:
    - |
      for i in $OS_ARCH; do
        export CI_MANIFEST_LIST="$CI_MANIFEST_LIST $CI_APP_REPO:$CI_APP_TAG-$i"
      done
    - |
      docker manifest create $CI_APP_REPO:$CI_APP_TAG $CI_MANIFEST_LIST
      if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
        docker manifest annotate --variant v8 $CI_APP_REPO:$CI_APP_TAG \
          $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
      fi
      docker manifest push $CI_APP_REPO:$CI_APP_TAG
    - |
      if [[ "$TAG_VERSION_MAJ_MIN" == "true" ]]; then
        docker manifest create $CI_APP_REPO:$VERSION_MAJ_MIN $CI_MANIFEST_LIST
        if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
          docker manifest annotate --variant v8 $CI_APP_REPO:$VERSION_MAJ_MIN \
            $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
        fi
        docker manifest push $CI_APP_REPO:$VERSION_MAJ_MIN
      fi
    - |
      if [[ "$TAG_VERSION_MAJ" == "true" ]]; then
        docker manifest create $CI_APP_REPO:$VERSION_MAJ $CI_MANIFEST_LIST
        if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
          docker manifest annotate --variant v8 $CI_APP_REPO:$VERSION_MAJ \
            $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
        fi
        docker manifest push $CI_APP_REPO:$VERSION_MAJ
      fi
  rules:
    - if: $BUILD_GHC == "true" && $CI_COMMIT_BRANCH == "freeze-version"
      changes:
        - prior/$GHC_VERSION.Dockerfile

build-package-hls:latest-linux-amd64:
  extends: .build
  stage: build2
  tags:
    - arch:amd64
    - os:linux
  script:
    - >
      docker build
      --build-arg GHC_VERSION
      --build-arg HLS_VERSION
      -t $CI_APP_REPO/$GHC_VERSION/hls:$HLS_VERSION-linux-amd64
      -t $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION-linux-amd64
      -f packages/hls/$HLS_VERSION.Dockerfile .
    - |
      docker push $CI_APP_REPO/$GHC_VERSION/hls:$HLS_VERSION-linux-amd64
      docker push $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION-linux-amd64
  rules:
    - if: $BUILD_HLS == "true" && $CI_COMMIT_BRANCH == "main" && $CI_BUILD_PLATFORMS =~ /(linux\/amd64)/
      changes:
        - packages/hls/$HLS_VERSION.Dockerfile

build-package-hls:latest-linux-arm64v8:
  extends: .build
  stage: build2
  tags:
    - arch:arm64/v8
    - os:linux
  script:
    - >
      docker build
      --build-arg GHC_VERSION
      --build-arg HLS_VERSION
      -t $CI_APP_REPO/$GHC_VERSION/hls:$HLS_VERSION-linux-arm64v8
      -t $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION-linux-arm64v8
      -f packages/hls/$HLS_VERSION.Dockerfile .
    - |
      docker push $CI_APP_REPO/$GHC_VERSION/hls:$HLS_VERSION-linux-arm64v8
      docker push $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION-linux-arm64v8
  rules:
    - if: $BUILD_HLS == "true" && $CI_COMMIT_BRANCH == "main" && $CI_BUILD_PLATFORMS =~ /(linux\/arm64\/v8)/
      changes:
        - packages/hls/$HLS_VERSION.Dockerfile

build-package-hls:version-linux-amd64:
  extends: .build
  stage: build2
  tags:
    - arch:amd64
    - os:linux
  script:
    - >
      docker build
      --build-arg GHC_VERSION
      -t $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION-linux-amd64
      -f packages/hls/$HLS_VERSION.Dockerfile .
    - docker push $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION-linux-amd64
  rules:
    - if: $BUILD_HLS == "true" && $CI_COMMIT_BRANCH == "freeze-version" && $CI_BUILD_PLATFORMS =~ /(linux\/amd64)/
      changes:
        - prior/$GHC_VERSION.Dockerfile

build-package-hls:version-linux-arm64v8:
  extends: .build
  stage: build2
  tags:
    - arch:arm64/v8
    - os:linux
  script:
    - >
      docker build
      --build-arg GHC_VERSION
      -t $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION-linux-arm64v8
      -f packages/hls/$HLS_VERSION.Dockerfile .
    - docker push $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION-linux-arm64v8
  rules:
    - if: $BUILD_HLS == "true" && $CI_COMMIT_BRANCH == "freeze-version" && $CI_BUILD_PLATFORMS =~ /(linux\/arm64\/v8)/
      changes:
        - prior/$GHC_VERSION.Dockerfile

build-package-hls-manifest:latest-linux-multiarch:
  extends: .build
  stage: build2
  needs: ["build-package-hls:latest-linux-amd64", "build-package-hls:latest-linux-arm64v8"]
  script:
    - |
      for i in $OS_ARCH; do
        export CI_MANIFEST_LIST="$CI_MANIFEST_LIST $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION-$i"
      done
    - |
      docker manifest create $CI_APP_REPO/$VERSION_MAJ_MIN_PAT/hls:$HLS_VERSION $CI_MANIFEST_LIST
      if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
        docker manifest annotate --variant v8 $CI_APP_REPO/$VERSION_MAJ_MIN_PAT/hls:$HLS_VERSION \
          $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION-linux-arm64v8
      fi
      docker manifest push $CI_APP_REPO/$VERSION_MAJ_MIN_PAT/hls:$HLS_VERSION
    - |
      docker manifest create $CI_APP_REPO/$VERSION_MAJ_MIN/hls:$HLS_VERSION $CI_MANIFEST_LIST
      if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
        docker manifest annotate --variant v8 $CI_APP_REPO/$VERSION_MAJ_MIN/hls:$HLS_VERSION \
          $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION-linux-arm64v8
      fi
      docker manifest push $CI_APP_REPO/$VERSION_MAJ_MIN/hls:$HLS_VERSION
    - |
      docker manifest create $CI_APP_REPO/$VERSION_MAJ/hls:$HLS_VERSION $CI_MANIFEST_LIST
      if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
        docker manifest annotate --variant v8 $CI_APP_REPO/$VERSION_MAJ/hls:$HLS_VERSION \
          $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION-linux-arm64v8
      fi
      docker manifest push $CI_APP_REPO/$VERSION_MAJ/hls:$HLS_VERSION
    - |
      docker manifest create $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION $CI_MANIFEST_LIST
      if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
        docker manifest annotate --variant v8 $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION \
          $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION-linux-arm64v8
      fi
      docker manifest push $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION
    - |
      docker manifest create $CI_APP_REPO/$CI_APP_TAG/hls:$CI_APP_TAG $CI_MANIFEST_LIST
      if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
        docker manifest annotate --variant v8 $CI_APP_REPO/$CI_APP_TAG/hls:$CI_APP_TAG \
          $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION-linux-arm64v8
      fi
      docker manifest push $CI_APP_REPO/$CI_APP_TAG/hls:$CI_APP_TAG
  rules:
    - if: $BUILD_HLS == "true" && $CI_COMMIT_BRANCH == "main"
      changes:
        - packages/hls/$HLS_VERSION.Dockerfile

build-package-hls-manifest:version-linux-multiarch:
  extends: .build
  stage: build2
  needs: ["build-package-hls:version-linux-amd64", "build-package-hls:version-linux-arm64v8"]
  script:
    - |
      for i in $OS_ARCH; do
        export CI_MANIFEST_LIST="$CI_MANIFEST_LIST $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION-$i"
      done
    - |
      docker manifest create $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION $CI_MANIFEST_LIST
      if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
        docker manifest annotate --variant v8 $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION \
          $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION-linux-arm64v8
      fi
      docker manifest push $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION
    - |
      if [[ "$TAG_VERSION_MAJ_MIN" == "true" ]]; then
        docker manifest create $CI_APP_REPO/$VERSION_MAJ_MIN/hls:$HLS_VERSION $CI_MANIFEST_LIST
        if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
          docker manifest annotate --variant v8 $CI_APP_REPO/$VERSION_MAJ_MIN/hls:$HLS_VERSION \
            $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION-linux-arm64v8
        fi
        docker manifest push $CI_APP_REPO/$VERSION_MAJ_MIN/hls:$HLS_VERSION
      fi
    - |
      if [[ "$TAG_VERSION_MAJ" == "true" ]]; then
        docker manifest create $CI_APP_REPO/$VERSION_MAJ/hls:$HLS_VERSION $CI_MANIFEST_LIST
        if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
          docker manifest annotate --variant v8 $CI_APP_REPO/$VERSION_MAJ/hls:$HLS_VERSION \
            $CI_APP_REPO/$CI_APP_TAG/hls:$HLS_VERSION-linux-arm64v8
        fi
        docker manifest push $CI_APP_REPO/$VERSION_MAJ/hls:$HLS_VERSION
      fi
  rules:
    - if: $BUILD_HLS == "true" && $CI_COMMIT_BRANCH == "freeze-version"
      changes:
        - prior/$GHC_VERSION.Dockerfile

update-stack:latest-linux-amd64:
  extends: .build
  stage: update
  tags:
    - arch:amd64
    - os:linux
  script:
    - >
      docker build
      --build-arg GHC_VERSION
      --build-arg STACK_VERSION
      --build-arg PREFIX
      -t $CI_APP_REPO:$VERSION_MAJ_MIN_PAT-linux-amd64
      -t $CI_APP_REPO:$CI_APP_TAG-linux-amd64
      -f Stack.Dockerfile .
    - |
      docker push $CI_APP_REPO:$VERSION_MAJ_MIN_PAT-linux-amd64
      docker push $CI_APP_REPO:$CI_APP_TAG-linux-amd64
  rules:
    - if: $UPDATE_STACK == "true" && $CI_COMMIT_BRANCH == "main" && $CI_BUILD_PLATFORMS =~ /(linux\/amd64)/

update-stack:latest-linux-arm64v8:
  extends: .build
  stage: update
  tags:
    - arch:arm64/v8
    - os:linux
  script:
    - >
      docker build
      --build-arg GHC_VERSION
      --build-arg STACK_VERSION
      --build-arg PREFIX
      -t $CI_APP_REPO:$VERSION_MAJ_MIN_PAT-linux-arm64v8
      -t $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
      -f Stack.Dockerfile .
    - |
      docker push $CI_APP_REPO:$VERSION_MAJ_MIN_PAT-linux-arm64v8
      docker push $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
  rules:
    - if: $UPDATE_STACK == "true" && $CI_COMMIT_BRANCH == "main" && $CI_BUILD_PLATFORMS =~ /(linux\/arm64\/v8)/

update-stack:version-linux-amd64:
  extends: .build
  stage: update
  tags:
    - arch:amd64
    - os:linux
  script:
    - >
      docker build
      --build-arg GHC_VERSION
      --build-arg STACK_VERSION
      --build-arg PREFIX
      -t $CI_APP_REPO:$CI_APP_TAG-linux-amd64
      -f Stack.Dockerfile .
    - docker push $CI_APP_REPO:$CI_APP_TAG-linux-amd64
  rules:
    - if: $UPDATE_STACK == "true" && $CI_COMMIT_BRANCH == "freeze-version" && $CI_BUILD_PLATFORMS =~ /(linux\/amd64)/

update-stack:version-linux-arm64v8:
  extends: .build
  stage: update
  tags:
    - arch:arm64/v8
    - os:linux
  script:
    - >
      docker build
      --build-arg GHC_VERSION
      --build-arg STACK_VERSION
      --build-arg PREFIX
      -t $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
      -f Stack.Dockerfile .
    - docker push $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
  rules:
    - if: $UPDATE_STACK == "true" && $CI_COMMIT_BRANCH == "freeze-version" && $CI_BUILD_PLATFORMS =~ /(linux\/arm64\/v8)/

update-stack-manifest:latest-linux-multiarch:
  extends: .build
  stage: update
  needs: ["update-stack:latest-linux-amd64", "update-stack:latest-linux-arm64v8"]
  script:
    - |
      for i in $OS_ARCH; do
        export CI_MANIFEST_LIST="$CI_MANIFEST_LIST $CI_APP_REPO:$CI_APP_TAG-$i"
      done
    - |
      docker manifest create $CI_APP_REPO:$VERSION_MAJ_MIN_PAT $CI_MANIFEST_LIST
      if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
        docker manifest annotate --variant v8 $CI_APP_REPO:$VERSION_MAJ_MIN_PAT \
          $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
      fi
      docker manifest push $CI_APP_REPO:$VERSION_MAJ_MIN_PAT
    - |
      docker manifest create $CI_APP_REPO:$VERSION_MAJ_MIN $CI_MANIFEST_LIST
      if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
        docker manifest annotate --variant v8 $CI_APP_REPO:$VERSION_MAJ_MIN \
          $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
      fi
      docker manifest push $CI_APP_REPO:$VERSION_MAJ_MIN
    - |
      docker manifest create $CI_APP_REPO:$VERSION_MAJ $CI_MANIFEST_LIST
      if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
        docker manifest annotate --variant v8 $CI_APP_REPO:$VERSION_MAJ \
          $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
      fi
      docker manifest push $CI_APP_REPO:$VERSION_MAJ
    - |
      docker manifest create $CI_APP_REPO:$CI_APP_TAG $CI_MANIFEST_LIST
      if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
        docker manifest annotate --variant v8 $CI_APP_REPO:$CI_APP_TAG \
          $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
      fi
      docker manifest push $CI_APP_REPO:$CI_APP_TAG
  rules:
    - if: $UPDATE_STACK == "true" && $CI_COMMIT_BRANCH == "main"

update-stack-manifest:version-linux-multiarch:
  extends: .build
  stage: update
  needs: ["update-stack:version-linux-amd64", "update-stack:version-linux-arm64v8"]
  script:
    - |
      for i in $OS_ARCH; do
        export CI_MANIFEST_LIST="$CI_MANIFEST_LIST $CI_APP_REPO:$CI_APP_TAG-$i"
      done
    - |
      docker manifest create $CI_APP_REPO:$CI_APP_TAG $CI_MANIFEST_LIST
      if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
        docker manifest annotate --variant v8 $CI_APP_REPO:$CI_APP_TAG \
          $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
      fi
      docker manifest push $CI_APP_REPO:$CI_APP_TAG
    - |
      if [[ "$TAG_VERSION_MAJ_MIN" == "true" ]]; then
        docker manifest create $CI_APP_REPO:$VERSION_MAJ_MIN $CI_MANIFEST_LIST
        if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
          docker manifest annotate --variant v8 $CI_APP_REPO:$VERSION_MAJ_MIN \
            $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
        fi
        docker manifest push $CI_APP_REPO:$VERSION_MAJ_MIN
      fi
    - |
      if [[ "$TAG_VERSION_MAJ" == "true" ]]; then
        docker manifest create $CI_APP_REPO:$VERSION_MAJ $CI_MANIFEST_LIST
        if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
          docker manifest annotate --variant v8 $CI_APP_REPO:$VERSION_MAJ \
            $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
        fi
        docker manifest push $CI_APP_REPO:$VERSION_MAJ
      fi
  rules:
    - if: $UPDATE_STACK == "true" && $CI_COMMIT_BRANCH == "freeze-version"

mirror:latest-linux-multiarch:
  extends: .mirror
  stage: mirror
  script:
    - |
      if [[ ! -z "$DOCKER_HUB_REPO" ]]; then
        for i in $OS_ARCH; do
          docker pull $CI_APP_REPO:$CI_APP_TAG-$i
          docker tag $CI_APP_REPO:$CI_APP_TAG-$i $DOCKER_HUB_REPO:$VERSION_MAJ_MIN_PAT-$i
          docker push $DOCKER_HUB_REPO:$VERSION_MAJ_MIN_PAT-$i
          docker tag $CI_APP_REPO:$CI_APP_TAG-$i $DOCKER_HUB_REPO:$CI_APP_TAG-$i
          docker push $DOCKER_HUB_REPO:$CI_APP_TAG-$i
          export DOCKER_HUB_MANIFEST_LIST="$DOCKER_HUB_MANIFEST_LIST $DOCKER_HUB_REPO:$CI_APP_TAG-$i"
        done

        docker manifest create $DOCKER_HUB_REPO:$VERSION_MAJ_MIN_PAT $DOCKER_HUB_MANIFEST_LIST
        if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
          docker manifest annotate --variant v8 $DOCKER_HUB_REPO:$VERSION_MAJ_MIN_PAT \
            $DOCKER_HUB_REPO:$CI_APP_TAG-linux-arm64v8
        fi
        docker manifest push $DOCKER_HUB_REPO:$VERSION_MAJ_MIN_PAT

        docker manifest create $DOCKER_HUB_REPO:$VERSION_MAJ_MIN $DOCKER_HUB_MANIFEST_LIST
        if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
          docker manifest annotate --variant v8 $DOCKER_HUB_REPO:$VERSION_MAJ_MIN \
            $DOCKER_HUB_REPO:$CI_APP_TAG-linux-arm64v8
        fi
        docker manifest push $DOCKER_HUB_REPO:$VERSION_MAJ_MIN

        docker manifest create $DOCKER_HUB_REPO:$VERSION_MAJ $DOCKER_HUB_MANIFEST_LIST
        if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
          docker manifest annotate --variant v8 $DOCKER_HUB_REPO:$VERSION_MAJ \
            $DOCKER_HUB_REPO:$CI_APP_TAG-linux-arm64v8
        fi
        docker manifest push $DOCKER_HUB_REPO:$VERSION_MAJ

        docker manifest create $DOCKER_HUB_REPO:$CI_APP_TAG $DOCKER_HUB_MANIFEST_LIST
        if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
          docker manifest annotate --variant v8 $DOCKER_HUB_REPO:$CI_APP_TAG \
            $DOCKER_HUB_REPO:$CI_APP_TAG-linux-arm64v8
        fi
        docker manifest push $DOCKER_HUB_REPO:$CI_APP_TAG
      fi
    - |
      if [[ ! -z "$QUAY_REPO" ]]; then
        for i in $OS_ARCH; do
          docker pull $CI_APP_REPO:$CI_APP_TAG-$i
          docker tag $CI_APP_REPO:$CI_APP_TAG-$i $QUAY_REPO:$VERSION_MAJ_MIN_PAT-$i
          docker push $QUAY_REPO:$VERSION_MAJ_MIN_PAT-$i
          docker tag $CI_APP_REPO:$CI_APP_TAG-$i $QUAY_REPO:$CI_APP_TAG-$i
          docker push $QUAY_REPO:$CI_APP_TAG-$i
          export QUAY_MANIFEST_LIST="$QUAY_MANIFEST_LIST $QUAY_REPO:$CI_APP_TAG-$i"
        done

        docker manifest create $QUAY_REPO:$VERSION_MAJ_MIN_PAT $QUAY_MANIFEST_LIST
        if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
          docker manifest annotate --variant v8 $QUAY_REPO:$VERSION_MAJ_MIN_PAT \
            $QUAY_REPO:$CI_APP_TAG-linux-arm64v8
        fi
        docker manifest push $QUAY_REPO:$VERSION_MAJ_MIN_PAT

        docker manifest create $QUAY_REPO:$VERSION_MAJ_MIN $QUAY_MANIFEST_LIST
        if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
          docker manifest annotate --variant v8 $QUAY_REPO:$VERSION_MAJ_MIN \
            $QUAY_REPO:$CI_APP_TAG-linux-arm64v8
        fi
        docker manifest push $QUAY_REPO:$VERSION_MAJ_MIN

        docker manifest create $QUAY_REPO:$VERSION_MAJ $QUAY_MANIFEST_LIST
        if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
          docker manifest annotate --variant v8 $QUAY_REPO:$VERSION_MAJ \
            $QUAY_REPO:$CI_APP_TAG-linux-arm64v8
        fi
        docker manifest push $QUAY_REPO:$VERSION_MAJ

        docker manifest create $QUAY_REPO:$CI_APP_TAG $QUAY_MANIFEST_LIST
        if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
          docker manifest annotate --variant v8 $QUAY_REPO:$CI_APP_TAG \
            $QUAY_REPO:$CI_APP_TAG-linux-arm64v8
        fi
        docker manifest push $QUAY_REPO:$CI_APP_TAG
      fi
  rules:
    - if: ($BUILD_GHC == "true" || $UPDATE_STACK == "true") && $CI_COMMIT_BRANCH == "main"
      changes:
        - latest.Dockerfile

mirror:version-linux-multiarch:
  extends: .mirror
  stage: mirror
  script:
    - |
      if [[ ! -z "$DOCKER_HUB_REPO" ]]; then
        for i in $OS_ARCH; do
          docker pull $CI_APP_REPO:$CI_APP_TAG-$i
          docker tag $CI_APP_REPO:$CI_APP_TAG-$i $DOCKER_HUB_REPO:$CI_APP_TAG-$i
          docker push $DOCKER_HUB_REPO:$CI_APP_TAG-$i
          export DOCKER_HUB_MANIFEST_LIST="$DOCKER_HUB_MANIFEST_LIST $DOCKER_HUB_REPO:$CI_APP_TAG-$i"
        done

        docker manifest create $DOCKER_HUB_REPO:$CI_APP_TAG $DOCKER_HUB_MANIFEST_LIST
        if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
          docker manifest annotate --variant v8 $DOCKER_HUB_REPO:$CI_APP_TAG \
            $DOCKER_HUB_REPO:$CI_APP_TAG-linux-arm64v8
        fi
        docker manifest push $DOCKER_HUB_REPO:$CI_APP_TAG

        if [[ "$TAG_VERSION_MAJ_MIN" == "true" ]]; then
          docker manifest create $DOCKER_HUB_REPO:$VERSION_MAJ_MIN $DOCKER_HUB_MANIFEST_LIST
          if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
            docker manifest annotate --variant v8 $DOCKER_HUB_REPO:$VERSION_MAJ_MIN \
              $DOCKER_HUB_REPO:$CI_APP_TAG-linux-arm64v8
          fi
          docker manifest push $DOCKER_HUB_REPO:$VERSION_MAJ_MIN
        fi

        if [[ "$TAG_VERSION_MAJ" == "true" ]]; then
          docker manifest create $DOCKER_HUB_REPO:$VERSION_MAJ $DOCKER_HUB_MANIFEST_LIST
          if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
            docker manifest annotate --variant v8 $DOCKER_HUB_REPO:$VERSION_MAJ \
              $DOCKER_HUB_REPO:$CI_APP_TAG-linux-arm64v8
          fi
          docker manifest push $DOCKER_HUB_REPO:$VERSION_MAJ
        fi
      fi
    - |
      if [[ ! -z "$QUAY_REPO" ]]; then
        for i in $OS_ARCH; do
          docker pull $CI_APP_REPO:$CI_APP_TAG-$i
          docker tag $CI_APP_REPO:$CI_APP_TAG-$i $QUAY_REPO:$CI_APP_TAG-$i
          docker push $QUAY_REPO:$CI_APP_TAG-$i
          export QUAY_MANIFEST_LIST="$QUAY_MANIFEST_LIST $QUAY_REPO:$CI_APP_TAG-$i"
        done

        docker manifest create $QUAY_REPO:$CI_APP_TAG $QUAY_MANIFEST_LIST
        if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
          docker manifest annotate --variant v8 $QUAY_REPO:$CI_APP_TAG \
            $QUAY_REPO:$CI_APP_TAG-linux-arm64v8
        fi
        docker manifest push $QUAY_REPO:$CI_APP_TAG

        if [[ "$TAG_VERSION_MAJ_MIN" == "true" ]]; then
          docker manifest create $QUAY_REPO:$VERSION_MAJ_MIN $QUAY_MANIFEST_LIST
          if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
            docker manifest annotate --variant v8 $QUAY_REPO:$VERSION_MAJ_MIN \
              $QUAY_REPO:$CI_APP_TAG-linux-arm64v8
          fi
          docker manifest push $QUAY_REPO:$VERSION_MAJ_MIN
        fi

        if [[ "$TAG_VERSION_MAJ" == "true" ]]; then
          docker manifest create $QUAY_REPO:$VERSION_MAJ $QUAY_MANIFEST_LIST
          if echo $OS_ARCH | grep "linux-arm64v8" >/dev/null ; then
            docker manifest annotate --variant v8 $QUAY_REPO:$VERSION_MAJ \
              $QUAY_REPO:$CI_APP_TAG-linux-arm64v8
          fi
          docker manifest push $QUAY_REPO:$VERSION_MAJ
        fi
      fi
  rules:
    - if: ($BUILD_GHC == "true" || $UPDATE_STACK == "true") && $CI_COMMIT_BRANCH == "freeze-version"
      changes:
        - prior/$GHC_VERSION.Dockerfile
