.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_BUILD}
    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_BUILD
    
    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"`

.build:
  image: registry.gitlab.b-data.ch/docker/docker-buildx:latest
  stage: build
  services:
    - docker:dind
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: "/certs"
    GHC_VERSION_BUILD: "9.4.3"
    CABAL_VERSION_BUILD: "3.8.1.0"
  before_script:
    - *before-script-build

build:latest-linux-amd64:
  extends: .build
  tags:
    - arch:amd64
    - os:linux
  script:
    - >
      docker build
      --build-arg GHC_VERSION_BUILD
      --build-arg CABAL_VERSION_BUILD
      -t $CI_APP_REPO:$CI_APP_TAG-linux-amd64
      -t $CI_APP_REPO:$VERSION_MAJ_MIN_PAT-linux-amd64
      -f latest.Dockerfile .
    - |
      docker push $CI_APP_REPO:$CI_APP_TAG-linux-amd64
      docker push $CI_APP_REPO:$VERSION_MAJ_MIN_PAT-linux-amd64
  rules:
    - if: $CI_COMMIT_BRANCH == "main" && $CI_BUILD_PLATFORMS =~ /(linux\/amd64)/
      changes:
        - latest.Dockerfile

build:latest-linux-arm64v8:
  extends: .build
  tags:
    - arch:arm64/v8
    - os:linux
  script:
    - >
      docker build
      --build-arg GHC_VERSION_BUILD
      --build-arg CABAL_VERSION_BUILD
      -t $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
      -t $CI_APP_REPO:$VERSION_MAJ_MIN_PAT-linux-arm64v8
      -f latest.Dockerfile .
    - |
      docker push $CI_APP_REPO:$CI_APP_TAG-linux-arm64v8
      docker push $CI_APP_REPO:$VERSION_MAJ_MIN_PAT-linux-arm64v8
  rules:
    - if: $CI_COMMIT_BRANCH == "main" && $CI_BUILD_PLATFORMS =~ /(linux\/arm64\/v8)/
      changes:
        - latest.Dockerfile

build:version-linux-amd64:
  extends: .build
  tags:
    - arch:amd64
    - os:linux
  script:
    - >
      docker build
      -t $CI_APP_REPO:$CI_APP_TAG-linux-amd64
      -f $GHC_VERSION_BUILD.Dockerfile .
    - docker push $CI_APP_REPO:$CI_APP_TAG-linux-amd64
  rules:
    - if: $CI_COMMIT_BRANCH == "freeze-version" && $CI_BUILD_PLATFORMS =~ /(linux\/amd64)/
      changes:
        - $GHC_VERSION_BUILD.Dockerfile

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

build-manifest:latest-linux-multiarch:
  extends: .build
  stage: deploy
  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
    - |
      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
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      changes:
        - latest.Dockerfile

build-manifest:version-linux-multiarch:
  extends: .build
  stage: deploy
  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
  rules:
    - if: $CI_COMMIT_BRANCH == "freeze-version"
      changes:
        - $GHC_VERSION_BUILD.Dockerfile
