---
steps:
  ########
  # SETUP
  ########
  - name: 'gcr.io/cloud-builders/docker'
    id: 'build-tester-image'
    args: ['build', '-t', 'gcr.io/$PROJECT_ID/${_IMAGE_NAME}', 'asmcli/']

  - name: 'gcr.io/cloud-builders/docker'
    id: 'publish-tester-image'
    args: ['push', 'gcr.io/$PROJECT_ID/${_IMAGE_NAME}']

  - name: 'gcr.io/$PROJECT_ID/${_IMAGE_NAME}'
    entrypoint: '/usr/bin/bazel'
    args: ['build', 'merge']

  - name: 'bash'
    id: 'check-commit-artifacts'
    args:
    - '-eEuo'
    - 'pipefail'
    - '-c'
    - |-
      if ! diff ${_BAZEL_BIN}/${_SCRIPT_NAME} asmcli/${_SCRIPT_NAME}; then
        echo "ERROR: please run 'scripts/release-asm/precommit' and commit the changes!"
        exit 1
      fi

  - name: node
    entrypoint: npm
    args: ['install']

  - name: 'gcr.io/$PROJECT_ID/${_IMAGE_NAME}'
    dir: 'asmcli'
    id: 'lint-with-shellcheck'
    entrypoint: 'shellcheck'
    args:
      - '-e'
      - 'SC2230,SC2317'
      - '${_SCRIPT_NAME}'

  - name: 'gcr.io/$PROJECT_ID/${_IMAGE_NAME}'
    dir: 'asmcli'
    id: 'script-version-check'
    entrypoint: '/bin/bash'
    args:
      - '-c'
      - >
        ./tests/script_version_check.sh

  - name: 'gcr.io/$PROJECT_ID/${_IMAGE_NAME}'
    dir: 'asmcli'
    id: 'lint-with-yamllint'
    entrypoint: 'yamllint'
    args:
      - '../asm'

  - name: 'gcr.io/$PROJECT_ID/${_IMAGE_NAME}'
    id: 'lint-variables'
    entrypoint: '/usr/bin/bazel'
    args: ['test', 'lint_variables', '--test_output=all']

  - name: 'gcr.io/cloud-builders/gcloud'
    dir: 'asmcli'
    id: 'fetch-secrets'
    entrypoint: '/bin/bash'
    args:
      - '-c'
      - |
        date
        gcloud secrets versions access latest \
        --secret="${_SECRET_NAME}" \
        --format='get(payload.data)' \
        --project="${PROJECT_ID}" \
        | tr '_-' '/+' \
        | base64 -d > "${_KEY_FILE}"
    env:
      - 'USE_GKE_GCLOUD_AUTH_PLUGIN=True'

  - name: 'gcr.io/$PROJECT_ID/${_IMAGE_NAME}'
    dir: 'asmcli'
    id: 'setup-long-term-cluster'
    entrypoint: '/bin/bash'
    args:
      - '-c'
      - >
        ./tests/setup_longterm_cluster
    env:
      - 'SERVICE_ACCOUNT=${_SERVICE_ACCOUNT}@${PROJECT_ID}.iam.gserviceaccount.com'
      - 'KEY_FILE=${_KEY_FILE}'
      - 'USE_GKE_GCLOUD_AUTH_PLUGIN=True'
      - '_CI_ASM_IMAGE_LOCATION=${_IMAGE_LOCATION}'
      - '_CI_ASM_PKG_LOCATION=${_PKG_LOCATION}'
      - '_CI_ASM_KPT_BRANCH=${_ASM_PKG_BRANCH}'

  #############
  # FAST TESTS
  #############
  - name: 'gcr.io/$PROJECT_ID/${_IMAGE_NAME}'
    id: 'unit-test'
    entrypoint: '/usr/bin/bazel'
    args: ['test', 'test', "--test_output=streamed"]

  - name: 'gcr.io/$PROJECT_ID/${_IMAGE_NAME}'
    dir: 'asmcli'
    id: 'run-release-debug'
    entrypoint: '/bin/bash'
    args:
      - '-c'
      - >
        ../scripts/release-asm/release_asm_installer
    env:
      - '_DEBUG=1'
    timeout: 10s

    #############
    # SLOW TESTS
    #############
  - name: 'gcr.io/$PROJECT_ID/${_IMAGE_NAME}'
    dir: 'asmcli'
    id: 'run-basic-suite'
    entrypoint: '/bin/bash'
    args:
      - '-c'
      - >
        ./tests/run_basic_suite
        --PROJECT_ID "${PROJECT_ID}"
        --BUILD_ID "${BUILD_ID}"
    env:
      - 'SERVICE_ACCOUNT=${_SERVICE_ACCOUNT}@${PROJECT_ID}.iam.gserviceaccount.com'
      - 'KEY_FILE=${_KEY_FILE}'
      - 'USE_GKE_GCLOUD_AUTH_PLUGIN=True'
      - '_CI_ASM_IMAGE_LOCATION=${_IMAGE_LOCATION}'
      - '_CI_ASM_PKG_LOCATION=${_PKG_LOCATION}'
      - '_CI_ASM_KPT_BRANCH=${_ASM_PKG_BRANCH}'
    timeout: 900s

  - name: 'gcr.io/$PROJECT_ID/${_IMAGE_NAME}'
    dir: 'asmcli'
    id: 'run-basic-suite-managed'
    entrypoint: '/bin/bash'
    args:
      - '-c'
      - >
        ./tests/run_basic_suite_managed
        --PROJECT_ID "${PROJECT_ID}"
        --BUILD_ID "${BUILD_ID}"
    env:
      - 'SERVICE_ACCOUNT=${_SERVICE_ACCOUNT}@${PROJECT_ID}.iam.gserviceaccount.com'
      - 'KEY_FILE=${_KEY_FILE}'
      - 'USE_GKE_GCLOUD_AUTH_PLUGIN=True'
      - '_CI_ASM_IMAGE_LOCATION=${_IMAGE_LOCATION}'
      - '_CI_ASM_PKG_LOCATION=${_PKG_LOCATION}'
      - '_CI_ASM_KPT_BRANCH=${_ASM_PKG_BRANCH}'
    timeout: 900s

substitutions:
  _BUCKET_NAME: my-gcs-bucket
  _CLUSTER_LOCATION: us-central1-c
  _IMAGE_NAME: gcloud-with-kpt
  _SCRIPT_NAME: asmcli
  _ASM_PKG_BRANCH: $COMMIT_SHA
  _SERVICE_ACCOUNT: asm-installer
  _SECRET_NAME: asm-installer-creds
  _KEY_FILE: key.json
  _BAZEL_BIN: bazel-bin
timeout: 3600s  # 1 hour
