version: 0.2

env:
  shell: bash
  exported-variables:
    - BSS_IMAGE_ASSET_REPOSITORY_NAME
    - BUILD_VERSION
    - CN_ASSETS
    - GLOBAL_ASSETS
    - ECR_REPOS
    - CN_ECR_REPOS
    - TAG_NUMBER

phases:
  install:
    runtime-versions:
      nodejs: 18
    commands:
      - echo "nothing to do in install"
  pre_build:
    commands:
      - cd infrastructure
      - export BSS_IMAGE_ASSET_REPOSITORY_NAME='stable-diffusion-aws-extension'
      - export TAG_NAME=$(curl -s https://api.github.com/repos/awslabs/stable-diffusion-aws-extension/tags | jq -r '.[0].name')
      # Retry logic for fetching TAG_NAME
      - |-
        MAX_RETRIES=5
        RETRY_COUNT=0
        until [[ ! -z "$TAG_NAME" || $RETRY_COUNT -eq $MAX_RETRIES ]]
        do
          export TAG_NAME=$(curl -s https://api.github.com/repos/awslabs/stable-diffusion-aws-extension/tags | jq -r '.[0].name')
          if [[ -z "$TAG_NAME" ]]; then
            let RETRY_COUNT=RETRY_COUNT+1
            echo "TAG_NAME is empty, retrying ($RETRY_COUNT/$MAX_RETRIES)..."
            sleep 5 # wait for 5 seconds before retrying
          fi
        done

        if [[ -z "$TAG_NAME" ]]; then
          echo "Failed to fetch TAG_NAME after $MAX_RETRIES attempts."
          exit 1
        fi
      - export BUILD_VERSION=${TAG_NAME}-${CODEBUILD_RESOLVED_SOURCE_VERSION:0:7}
      - export CN_ASSETS='cn/'
      - export TAG_NUMBER="${TAG_NAME#v}"
      - |-
        set -euxo pipefail
        __dir="${CODEBUILD_SRC_DIR}/deployment"
        function join_by { local IFS="$1"; shift; echo "$*"; }
        export CN_ECR_REPOS=$(join_by , `cat "${__dir}/cn-ecr-repos"`)
      - export GLOBAL_ASSETS='custom-domain/,default/'
  build:
    commands:
      - echo "Starting build the public ECR"
      - cd ../build_scripts
      - chmod +x ./build_and_push_inference_private.sh
      - json_output=$(aws sts assume-role  --role-arn "$AWS_ASSET_PUBLISH_ROLE"  --role-session-name "asset-publishing-role"  --duration-seconds "3600"  2>&1)
      - set +x
      - export AWS_ACCESS_KEY_ID=$(echo "${json_output}" | jq --raw-output ".Credentials[\"AccessKeyId\"]")
      - export AWS_SECRET_ACCESS_KEY=$(echo "${json_output}" | jq --raw-output ".Credentials[\"SecretAccessKey\"]")
      - export AWS_SESSION_TOKEN=$(echo "${json_output}" | jq --raw-output ".Credentials[\"SessionToken\"]")
      - echo "BUILD VERSION is ${BUILD_VERSION}"
      - echo "./build_and_push_inference_private.sh Dockerfile.inference.from_scratch esd-inference dev ${REGION} ${BUILD_VERSION} ${CODEBUILD_RESOLVED_SOURCE_VERSION}"
      - ./build_and_push_inference_private.sh Dockerfile.inference.from_scratch esd-inference dev ${REGION} ${BUILD_VERSION} ${CODEBUILD_RESOLVED_SOURCE_VERSION}
      - unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
      - set -x

artifacts:
  files:
    - deployment/**/*
    - infrastructure/**/*
    - middleware_api/**/*
    - .gitignore
    - sonar-project.properties
    - buildspec.yml
    - CHANGELOG.md
    - .cfnnag_*
