steps:
  # TODO(#67): Need golang+gcloud container.
  # - id: 'System & Unit Tests'
  #   name: 'golang:1.15'
  #   dir: '${_SAMPLE_DIR}'
  #   args: ['go', 'test', '-v', '.']
  #   env:
  #     - "GCLOUD_REPORT_BUCKET=${_BUCKET}"

  - id: "Build Container Image"
    name: "gcr.io/cloud-builders/docker:latest"
    dir: "${_SAMPLE_DIR}"
    args: [
      "build",
      "-t",
      "${_REGION}-docker.pkg.dev/${PROJECT_ID}/${_REPO_NAME}/${_SERVICE}:${SHORT_SHA}",
      ".",
    ]  # Tag docker image with git commit sha

  - id: "Push Container Image"
    name: "gcr.io/cloud-builders/docker:latest"
    args: [
      "push",
      "${_REGION}-docker.pkg.dev/${PROJECT_ID}/${_REPO_NAME}/${_SERVICE}:${SHORT_SHA}",
    ]

  - id: "Deploy to Cloud Run"
    name: "gcr.io/google.com/cloudsdktool/cloud-sdk:$_CLOUDSDK_VERSION"
    entrypoint: /bin/bash
    args:
      - "-c"
      - |
        gcloud run deploy ${_SERVICE}-$BUILD_ID \
          --image '${_REGION}-docker.pkg.dev/${PROJECT_ID}/${_REPO_NAME}/${_SERVICE}:${SHORT_SHA}' \
          --update-env-vars GCLOUD_REPORT_BUCKET=${_BUCKET} \
          --no-allow-unauthenticated

  - id: "Get Cloud Run URL"
    name: "gcr.io/google.com/cloudsdktool/cloud-sdk:$_CLOUDSDK_VERSION"
    entrypoint: "/bin/bash"
    args:
      - "-c"
      - |
        set -e
        . /workspace/testing/cloudbuild-templates/common.sh
        echo $(get_url ${BUILD_ID}) > _service_url
        echo "Cloud Run URL for ${_SERVICE}-${BUILD_ID} is $(cat _service_url)"
        echo $(get_idtoken) > _id_token
    env:
      # Make substitutions available in shell script.
      - "_SECRET_NAME=${_SECRET_NAME}"
      - "_RUNNER_IDENTITY=${_RUNNER_IDENTITY}"
      - "_SERVICE=${_SERVICE}"

  - id: "End-to-End Tests"
    name: "gcr.io/google.com/cloudsdktool/cloud-sdk:$_CLOUDSDK_VERSION"
    entrypoint: "/bin/sh"
    args:
      - "-c"
      - |
        set -e
        out=$(curl -si --fail --show-error -H "Authorization: Bearer $(cat _id_token)" "$(cat _service_url)")
        echo "$out"
        obj=$(echo $out | grep -o gs://.*)
        echo "Downloading report ${obj} for local validation"
        gsutil cp $obj .
        cat $(basename $obj) | grep ${_SERVICE}
    env:
      - "GCLOUD_REPORT_BUCKET=${_BUCKET}"

  - id: "Teardown"
    name: "gcr.io/google.com/cloudsdktool/cloud-sdk:$_CLOUDSDK_VERSION"
    entrypoint: "/bin/bash"
    args:
      - "-c"
      - |
        set -x
        gcloud --quiet artifacts docker images delete '${_REGION}-docker.pkg.dev/${PROJECT_ID}/${_REPO_NAME}/${_SERVICE}:${SHORT_SHA}'
        gcloud --quiet run services delete ${_SERVICE}-${BUILD_ID} --region ${_REGION}
        set +x
        echo "View build details in the console: https://console.cloud.google.com/cloud-build/builds/${BUILD_ID}"

# Uncomment if skipping teardown to associate build with container image.
# images:
# - '${_REGION}-docker.pkg.dev/${PROJECT_ID}/${_REPO_NAME}/${_SERVICE}:${SHORT_SHA}'

options:
  env:
    - "CLOUDSDK_RUN_REGION=${_REGION}"
    - "GOOGLE_CLOUD_PROJECT=${PROJECT_ID}"

# TODO: Update these User-defined substitutions
substitutions:
  _SERVICE: gcloud-report
  _SAMPLE_DIR: gcloud-report
  _REPO_NAME: samples  # Artifact Registry repo, "samples", was manually created
  _REGION: us-central1
  _CLOUDSDK_VERSION: latest
