# This cloudbuild.yaml config file is triggered in two contexts:
#
#
# Trigger #1: Before master merge
#
# GoogleCloudPlatform/marketplace-k8s-app-tools push to any branch
# https://console.cloud.google.com/cloud-build/triggers?project=marketplace-k8s-app-tools
#
# This build trigger asserts the invariant that the source code can
# be built and pushed into a repository. Its success blocks pull
# request. gcr.io/marketplace-k8s-app-tools read access restricted.
#
#
# Trigger #2: After master merge
#
# GoogleCloudPlatform/marketplace-k8s-app-tools push to master branch
# https://console.cloud.google.com/cloud-build/triggers?project=cloud-marketplace-tools
#
# This build trigger asserts the invariant that each commit on the master
# branch of GoogleCloudPlatform/marketplace-k8s-app-tools will be built
# and tagged in gcr.io/cloud-marketplace-tools/k8s.

# The following variables will be provided by Cloud Build.
#
# $PROJECT_ID: build.ProjectId
# $BUILD_ID: build.BuildId
# $COMMIT_SHA: build.SourceProvenance.ResolvedRepoSource.Revision.CommitSha
# $SHORT_SHA : The first seven characters of COMMIT_SHA
# $REPO_NAME: build.Source.RepoSource.RepoName (only available for triggered builds)
# $BRANCH_NAME: build.Source.RepoSource.Revision.BranchName (only available for triggered builds)
# $TAG_NAME: build.Source.RepoSource.Revision.TagName (only available for triggered builds)
# $REVISION_ID: build.SourceProvenance.ResolvedRepoSource.Revision.CommitSha (only available for triggered builds)
#
# Source: https://cloud.google.com/cloud-build/docs/configuring-builds/substitute-variable-values
timeout: 1200s # 20m
options:
  machineType: 'N1_HIGHCPU_8'

substitutions:
  _CLUSTER_LOCATION: null

steps:

- id: &BuildDev Build dev
  name: gcr.io/cloud-builders/docker
  waitFor:
  - '-'
  args:
  - build
  - --tag
  - gcr.io/$PROJECT_ID/k8s/dev:sha_$COMMIT_SHA
  - --tag
  - gcr.io/$PROJECT_ID/k8s/dev:sha_$SHORT_SHA
  - --file
  - marketplace/dev/Dockerfile
  - .

- id: &BuildEnvsubst Build deployer_envsubst
  name: gcr.io/cloud-builders/docker
  waitFor:
  - '-'
  args:
  - build
  - --tag
  - gcr.io/$PROJECT_ID/k8s/deployer_envsubst:sha_$COMMIT_SHA
  - --tag
  - gcr.io/$PROJECT_ID/k8s/deployer_envsubst:sha_$SHORT_SHA
  - --file
  - marketplace/deployer_envsubst_base/Dockerfile
  - .

- id: &BuildEnvsubstOnBuild Build deployer_envsubst onbuild
  name: gcr.io/cloud-builders/docker
  waitFor:
  - *BuildEnvsubst
  args:
  - build
  - --build-arg
  - FROM=gcr.io/$PROJECT_ID/k8s/deployer_envsubst:sha_$COMMIT_SHA
  - --tag
  - gcr.io/$PROJECT_ID/k8s/deployer_envsubst/onbuild:sha_$COMMIT_SHA
  - --tag
  - gcr.io/$PROJECT_ID/k8s/deployer_envsubst/onbuild:sha_$SHORT_SHA
  - --file
  - marketplace/deployer_envsubst_base/onbuild/Dockerfile
  - .

- id: &BuildHelm Build deployer_helm
  name: gcr.io/cloud-builders/docker
  waitFor:
  - '-'
  args:
  - build
  - --tag
  - gcr.io/$PROJECT_ID/k8s/deployer_helm:sha_$COMMIT_SHA
  - --tag
  - gcr.io/$PROJECT_ID/k8s/deployer_helm:sha_$SHORT_SHA
  - --file
  - marketplace/deployer_helm_base/Dockerfile
  - .

- id: &BuildHelmOnbuild Build deployer_helm onbuild
  name: gcr.io/cloud-builders/docker
  waitFor:
  - *BuildHelm
  args:
  - build
  - --build-arg
  - FROM=gcr.io/$PROJECT_ID/k8s/deployer_helm:sha_$COMMIT_SHA
  - --tag
  - gcr.io/$PROJECT_ID/k8s/deployer_helm/onbuild:sha_$COMMIT_SHA
  - --tag
  - gcr.io/$PROJECT_ID/k8s/deployer_helm/onbuild:sha_$SHORT_SHA
  - --file
  - marketplace/deployer_helm_base/onbuild/Dockerfile
  - .

- id: &BuildMarketplaceIntegrationChart Build marketplace-integration Chart
  name: gcr.io/$PROJECT_ID/k8s/dev:sha_$COMMIT_SHA
  waitFor:
  - *BuildDev
  entrypoint: bash
  args:
  - -exc
  - |
    mkdir charts-tgz/
    helm package marketplace/charts/marketplace-integration/ \
        --version "0.0.0+sha.$COMMIT_SHA" \
        --destination charts-tgz/

    gsutil cp gs://$PROJECT_ID-charts/index.yaml index.yaml
    helm repo index --merge index.yaml charts-tgz/

- id: &InitializeCredentials Initialize Credentials
  name: gcr.io/cloud-builders/gcloud
  waitFor:
  - '-'
  entrypoint: bash
  args:
  - -exc
  - |
    # Use the 'c-$BUILD_ID' as the cluster name as it must start with a letter,
    # and have a max length of 40 chars (BUILD_ID is 37). Then install App CRD.
    gcloud container clusters create 'c-$BUILD_ID' --zone '${_CLUSTER_LOCATION}' --project '$PROJECT_ID' --labels=creation-timestamp=`date +%s`
    gcloud container clusters get-credentials 'c-$BUILD_ID' --zone '${_CLUSTER_LOCATION}' --project '$PROJECT_ID'
    kubectl apply -f "https://raw.githubusercontent.com/GoogleCloudPlatform/marketplace-k8s-app-tools/$COMMIT_SHA/crd/app-crd.yaml"
    mkdir -p /workspace/.kube/
    cp -r $$HOME/.kube/ /workspace/
    mkdir -p /workspace/.config/gcloud/
    cp -r $$HOME/.config/gcloud/ /workspace/.config/

- id: &Verify Verify
  name: gcr.io/$PROJECT_ID/k8s/dev:sha_$COMMIT_SHA
  waitFor:
  - *InitializeCredentials
  - *BuildDev
  - *BuildEnvsubst
  - *BuildEnvsubstOnBuild
  - *BuildHelm
  - *BuildHelmOnbuild
  env:
  - 'KUBE_CONFIG=/workspace/.kube'
  - 'GCLOUD_CONFIG=/workspace/.config/gcloud'
  - 'VERIFICATION_LOGS_PATH=/workspace/.mpdev_logs'
  # Use local Docker network named cloudbuild as described here:
  # https://cloud.google.com/cloud-build/docs/overview#build_configuration_and_build_steps
  - 'DOCKER_NETWORK=cloudbuild'
  - MARKETPLACE_TOOLS_TAG=testing-sha_$COMMIT_SHA
  args:
  - make
  - -j10
  - --output-sync
  - tests/integration

- id: Cleanup
  name: gcr.io/cloud-builders/gcloud
  waitFor:
  - *Verify
  entrypoint: bash
  args:
  - -exc
  - |
    gcloud container clusters delete 'c-$BUILD_ID' --zone '${_CLUSTER_LOCATION}' --project '$PROJECT_ID' --quiet

images:
  - gcr.io/$PROJECT_ID/k8s/dev:sha_$COMMIT_SHA
  - gcr.io/$PROJECT_ID/k8s/dev:sha_$SHORT_SHA
  - gcr.io/$PROJECT_ID/k8s/deployer_envsubst:sha_$COMMIT_SHA
  - gcr.io/$PROJECT_ID/k8s/deployer_envsubst:sha_$SHORT_SHA
  - gcr.io/$PROJECT_ID/k8s/deployer_envsubst/onbuild:sha_$COMMIT_SHA
  - gcr.io/$PROJECT_ID/k8s/deployer_envsubst/onbuild:sha_$SHORT_SHA
  - gcr.io/$PROJECT_ID/k8s/deployer_helm:sha_$COMMIT_SHA
  - gcr.io/$PROJECT_ID/k8s/deployer_helm:sha_$SHORT_SHA
  - gcr.io/$PROJECT_ID/k8s/deployer_helm/onbuild:sha_$COMMIT_SHA
  - gcr.io/$PROJECT_ID/k8s/deployer_helm/onbuild:sha_$SHORT_SHA

artifacts:
  objects:
    location: gs://$PROJECT_ID-charts/
    paths:
    - charts-tgz/*
