version: "2017-09-20"
allow_concurrent_steps: true

build_env: &BUILD_ENV
  PYTHON_BASE_IMAGE: container-registry.zalando.net/library/python-3.11-slim
  ALPINE_BASE_IMAGE: container-registry.zalando.net/library/alpine-3
  UBUNTU_BASE_IMAGE: container-registry.zalando.net/library/ubuntu-22.04
  MULTI_ARCH_REGISTRY: container-registry-test.zalando.net/acid

pipeline:
  - id: build-postgres-operator
    env:
      <<: *BUILD_ENV
    type: script
    vm_config:
      type: linux
      size: large
      image: cdp-runtime/go
    cache:
      paths:
        - /go/pkg/mod        # pkg cache for Go modules
        - ~/.cache/go-build  # Go build cache
    commands:
      - desc: Run unit tests
        cmd: |
          make mocks test
          if ! git diff --quiet; then
            echo "Build resulted in files being changed, likely they were not checked in"
            exit 1
          fi
      - desc: Build Docker image
        cmd: |
          if [ -z ${CDP_SOURCE_BRANCH} ]; then
            IMAGE=${MULTI_ARCH_REGISTRY}/postgres-operator
          else
            IMAGE=${MULTI_ARCH_REGISTRY}/postgres-operator-test
          fi

          docker buildx create --config /etc/cdp-buildkitd.toml --driver-opt network=host --bootstrap --use
          docker buildx build --platform "linux/amd64,linux/arm64" \
                    --build-arg BASE_IMAGE="${ALPINE_BASE_IMAGE}" \
                    -t "${IMAGE}:${CDP_BUILD_VERSION}" \
                    -f docker/Dockerfile \
                    --push .

  - id: build-pooler
    env:
      <<: *BUILD_ENV
    type: script
    vm_config:
      type: linux

    commands:
      - desc: Build image
        cmd: |
          cd pooler
          if [ -z ${CDP_SOURCE_BRANCH} ]; then
            IMAGE=${MULTI_ARCH_REGISTRY}/pgbouncer
          else
            IMAGE=${MULTI_ARCH_REGISTRY}/pgbouncer-test
          fi

          docker buildx create --config /etc/cdp-buildkitd.toml --driver-opt network=host --bootstrap --use
          docker buildx build --platform "linux/amd64,linux/arm64" \
                    --build-arg BASE_IMAGE="${ALPINE_BASE_IMAGE}" \
                    -t "${IMAGE}:${CDP_BUILD_VERSION}" \
                    --push .

          if [ -z ${CDP_SOURCE_BRANCH} ]; then
            cdp-promote-image ${IMAGE}:${CDP_BUILD_VERSION}
          fi

  - id: build-operator-ui
    env:
      <<: *BUILD_ENV
    type: script
    vm_config:
      type: linux

    commands:
      - desc: 'Prepare environment'
        cmd: |
          apt-get update
          apt-get install -y build-essential

      - desc: 'Compile JavaScript app'
        cmd: |
          cd ui
          make appjs

      - desc: 'Build and push Docker image'
        cmd: |
          cd ui
          if [ -z ${CDP_SOURCE_BRANCH} ]; then
            IMAGE=${MULTI_ARCH_REGISTRY}/postgres-operator-ui
          else
            IMAGE=${MULTI_ARCH_REGISTRY}/postgres-operator-ui-test
          fi

          make appjs
          docker buildx create --config /etc/cdp-buildkitd.toml --driver-opt network=host --bootstrap --use
          docker buildx build --platform linux/amd64,linux/arm64 \
                              --build-arg BASE_IMAGE="${PYTHON_BASE_IMAGE}" \
                              -t ${IMAGE}:${CDP_BUILD_VERSION} \
                              --push .

          if [ -z ${CDP_SOURCE_BRANCH} ]; then
            cdp-promote-image ${IMAGE}:${CDP_BUILD_VERSION}
          fi

  - id: build-logical-backup
    env:
      <<: *BUILD_ENV
    type: script
    vm_config:
      type: linux

    commands:
      - desc: Build image
        cmd: |
          cd logical-backup
          if [ -z ${CDP_SOURCE_BRANCH} ]; then
            IMAGE=${MULTI_ARCH_REGISTRY}/logical-backup
          else
            IMAGE=${MULTI_ARCH_REGISTRY}/logical-backup-test
          fi

          docker buildx create --config /etc/cdp-buildkitd.toml --driver-opt network=host --bootstrap --use
          docker buildx build --platform linux/amd64,linux/arm64 \
                              --build-arg BASE_IMAGE="${UBUNTU_BASE_IMAGE}" \
                              -t ${IMAGE}:${CDP_BUILD_VERSION} \
                              --push .

          if [ -z ${CDP_SOURCE_BRANCH} ]; then
            cdp-promote-image ${IMAGE}:${CDP_BUILD_VERSION}
          fi
