stages:
  - build

docker-build-rolling:
  stage: build
  image:
    name: docker.io/docker:23.0.3-dind
  services:
    - docker:23.0.3-dind
  variables:
    TAG: $CI_COMMIT_BRANCH
    DOCKER_BUILDKIT: 1
    DOCKER_HOST: tcp://docker:2376
    DOCKER_TLS_CERTDIR: "/certs"
  script:
    - mkdir -p /root/.docker
    - cp ${DOCKER_HUB_CONFIG} /root/.docker/config.json
    - docker context create tls-environment
    - docker buildx create --use tls-environment
    - docker buildx build --push --platform=linux/amd64 --file=docker/production/castopod/Dockerfile --tag=${DOCKER_IMAGE_CASTOPOD}:${TAG} .
    - docker buildx build --push --platform=linux/amd64 --file=docker/production/web-server/Dockerfile --tag=${DOCKER_IMAGE_WEB_SERVER}:${TAG} .
    - docker buildx build --push --platform=linux/amd64 --file=docker/production/app/Dockerfile --tag=${DOCKER_IMAGE_APP}:${TAG} .
  needs:
    - pipeline: $PARENT_PIPELINE_ID
      job: bundle
  only:
    refs:
      - develop

docker-build-main-release:
  stage: build
  image:
    name: docker.io/docker:23.0.3-dind
  services:
    - docker:23.0.3-dind
  variables:
    DOCKER_BUILDKIT: 1
    DOCKER_HOST: tcp://docker:2376
    DOCKER_TLS_CERTDIR: "/certs"
  script:
    - mkdir -p /root/.docker
    - cp ${DOCKER_HUB_CONFIG} /root/.docker/config.json
    - export CP_VERSION=$(cat CP_VERSION.env)
    - docker context create tls-environment
    - docker buildx create --use tls-environment
    - docker buildx build --push --platform=linux/amd64 --file=docker/production/castopod/Dockerfile --tag=${DOCKER_IMAGE_CASTOPOD}:${CP_VERSION} --tag=${DOCKER_IMAGE_CASTOPOD}:latest .
    - docker buildx build --push --platform=linux/amd64 --file=docker/production/web-server/Dockerfile --tag=${DOCKER_IMAGE_WEB_SERVER}:${CP_VERSION} --tag=${DOCKER_IMAGE_WEB_SERVER}:latest .
    - docker buildx build --push --platform=linux/amd64 --file=docker/production/app/Dockerfile --tag=${DOCKER_IMAGE_APP}:${CP_VERSION} --tag=${DOCKER_IMAGE_APP}:latest .
    # when --platform=linux/amd64,linux/arm64: amd64 image takes too long to be pushed as it needs to wait for arm64 to be built
    # --> build and push amd64 image to be pushed first, then overwrite manifest after building arm64
    - docker buildx build --push --platform=linux/amd64,linux/arm64 --file=docker/production/castopod/Dockerfile --tag=${DOCKER_IMAGE_CASTOPOD}:${CP_VERSION} --tag=${DOCKER_IMAGE_CASTOPOD}:latest .
  needs:
    - pipeline: $PARENT_PIPELINE_ID
      job: release
  only:
    refs:
      - main

docker-build-alpha-beta-release:
  stage: build
  image:
    name: docker.io/docker:23.0.3-dind
  services:
    - docker:23.0.3-dind
  variables:
    TAG: $CI_COMMIT_BRANCH
    DOCKER_BUILDKIT: 1
    DOCKER_HOST: tcp://docker:2376
    DOCKER_TLS_CERTDIR: "/certs"
  script:
    - mkdir -p /root/.docker
    - cp ${DOCKER_HUB_CONFIG} /root/.docker/config.json
    - export CP_VERSION=$(cat CP_VERSION.env)
    - docker context create tls-environment
    - docker buildx create --use tls-environment
    - docker buildx build --push --platform=linux/amd64 --file=docker/production/castopod/Dockerfile --tag=${DOCKER_IMAGE_CASTOPOD}:${CP_VERSION} --tag=${DOCKER_IMAGE_CASTOPOD}:${TAG} .
    - docker buildx build --push --platform=linux/amd64 --file=docker/production/web-server/Dockerfile --tag=${DOCKER_IMAGE_WEB_SERVER}:${CP_VERSION} --tag=${DOCKER_IMAGE_WEB_SERVER}:${TAG} .
    - docker buildx build --push --platform=linux/amd64 --file=docker/production/app/Dockerfile --tag=${DOCKER_IMAGE_APP}:${CP_VERSION} --tag=${DOCKER_IMAGE_APP}:${TAG} .
    # when --platform=linux/amd64,linux/arm64: amd64 image takes too long to be pushed as it needs to wait for arm64 to be built
    # --> build and push amd64 image to be pushed first, then overwrite manifest after building arm64
    - docker buildx build --push --platform=linux/amd64,linux/arm64 --file=docker/production/castopod/Dockerfile --tag=${DOCKER_IMAGE_CASTOPOD}:${CP_VERSION} --tag=${DOCKER_IMAGE_CASTOPOD}:${TAG} .
  needs:
    - pipeline: $PARENT_PIPELINE_ID
      job: release
  only:
    refs:
      - alpha
      - beta
