steps:
  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "build",
        "-t",
        "${_GCR_PATH}/kaggle_download:$COMMIT_SHA",
        "-t",
        "${_GCR_PATH}/kaggle_download:latest",
        "${_CODE_PATH}/download_dataset",
        "-f",
        "${_CODE_PATH}/download_dataset/Dockerfile",
      ]
    id: "BuildDownloadDataImage"

  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "push",
        "${_GCR_PATH}/kaggle_download:$COMMIT_SHA",
      ]
    id: "PushDownloadDataImage"
    waitFor: ["BuildDownloadDataImage"]

  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "build",
        "-t",
        "${_GCR_PATH}/kaggle_visualize_table:$COMMIT_SHA",
        "-t",
        "${_GCR_PATH}/kaggle_visualize_table:latest",
        "${_CODE_PATH}/visualize_table",
        "-f",
        "${_CODE_PATH}/visualize_table/Dockerfile",
      ]
    id: "BuildVisualizeTableImage"
  
  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "push",
        "${_GCR_PATH}/kaggle_visualize_table:$COMMIT_SHA",
      ]
    id: "PushVisualizeTableImage"
    waitFor: ["BuildVisualizeTableImage"]

  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "build",
        "-t",
        "${_GCR_PATH}/kaggle_visualize_html:$COMMIT_SHA",
        "-t",
        "${_GCR_PATH}/kaggle_visualize_html:latest",
        "${_CODE_PATH}/visualize_html",
        "-f",
        "${_CODE_PATH}/visualize_html/Dockerfile",
      ]
    id: "BuildVisualizeHTMLImage"

  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "push",
        "${_GCR_PATH}/kaggle_visualize_html:$COMMIT_SHA",
      ]
    id: "PushVisualizeHTMLImage"
    waitFor: ["BuildVisualizeHTMLImage"]

  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "build",
        "-t",
        "${_GCR_PATH}/kaggle_train:$COMMIT_SHA",
        "-t",
        "${_GCR_PATH}/kaggle_train:latest",
        "${_CODE_PATH}/train_model",
        "-f",
        "${_CODE_PATH}/train_model/Dockerfile",
      ]
    id: "BuildTrainImage"

  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "push",
        "${_GCR_PATH}/kaggle_train:$COMMIT_SHA",
      ]
    id: "PushTrainImage"
    waitFor: ["BuildTrainImage"]

  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "build",
        "-t",
        "${_GCR_PATH}/kaggle_submit:$COMMIT_SHA",
        "-t",
        "${_GCR_PATH}/kaggle_submit:latest",
        "${_CODE_PATH}/submit_result",
        "-f",
        "${_CODE_PATH}/submit_result/Dockerfile",
      ]
    id: "BuildSubmitImage"

  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "push",
        "${_GCR_PATH}/kaggle_submit:$COMMIT_SHA",
      ]
    id: "PushSubmitImage"
    waitFor: ["BuildSubmitImage"]

  - name: "python:3.7-slim"
    entrypoint: "/bin/sh"
    args: [
        "-c",
        "set -ex;
        cd ${_CODE_PATH};
        pip3 install cffi==1.12.3 --upgrade;
        pip3 install kfp==0.1.38;
        sed -i 's|image: download_image_location|image: ${_GCR_PATH}/kaggle_download:$COMMIT_SHA|g' ./download_dataset/component.yaml;
        sed -i 's|image: visualizetable_image_location|image: ${_GCR_PATH}/kaggle_visualize_table:$COMMIT_SHA|g' ./visualize_table/component.yaml;
        sed -i 's|image: visualizehtml_image_location|image: ${_GCR_PATH}/kaggle_visualize_html:$COMMIT_SHA|g' ./visualize_html/component.yaml;
        sed -i 's|image: train_image_location|image: ${_GCR_PATH}/kaggle_train:$COMMIT_SHA|g' ./train_model/component.yaml;
        sed -i 's|image: submit_image_location|image: ${_GCR_PATH}/kaggle_submit:$COMMIT_SHA|g' ./submit_result/component.yaml;
        python pipeline.py
        --gcr_address ${_GCR_PATH};
        cp pipeline.py.zip /workspace/pipeline.zip",
      ]
    id: "KagglePackagePipeline"

  - name: "gcr.io/cloud-builders/gsutil"
    args:
      [
        "cp",
        "/workspace/pipeline.zip",
        "${_GS_BUCKET}/$COMMIT_SHA/pipeline.zip"
      ]
    id: "KaggleUploadPipeline"
    waitFor: ["KagglePackagePipeline"]


  - name: "gcr.io/cloud-builders/kubectl"
    entrypoint: "/bin/sh"
    args: [
        "-c",
        "cd ${_CODE_PATH};
        apt-get update;
        apt-get install -y python3-pip;
        apt-get install -y libssl-dev libffi-dev;
        /builder/kubectl.bash;
        pip3 install kfp;
        pip3 install kubernetes;
        python3 create_pipeline_version_and_run.py 
        --pipeline_id ${_PIPELINE_ID} 
        --commit_sha $COMMIT_SHA
        --bucket_name ${_GS_BUCKET}
        --gcr_address ${_GCR_PATH}"
      ]
    env:
      - "CLOUDSDK_COMPUTE_ZONE=[Your cluster zone, for example: us-central1-a]"
      - "CLOUDSDK_CONTAINER_CLUSTER=[Your cluster name, for example: my-cluster]"
    id: "KaggleCreatePipelineVersionAndRun"

images:
  - "${_GCR_PATH}/kaggle_download:latest"
  - "${_GCR_PATH}/kaggle_visualize_table:latest"
  - "${_GCR_PATH}/kaggle_visualize_html:latest"
  - "${_GCR_PATH}/kaggle_train:latest"
  - "${_GCR_PATH}/kaggle_submit:latest"


substitutions:
  _CODE_PATH: /workspace/samples/contrib/versioned-pipeline-ci-samples/kaggle-ci-sample
  _NAMESPACE: kubeflow
  _GCR_PATH: [Your cloud registry path. For example, gcr.io/my-project-id]
  _GS_BUCKET: [Name of your cloud storage bucket. For example, gs://my-project-bucket]
  _PIPELINE_ID: [Your kubeflow pipeline id to create a version on. Get it from Kubeflow Pipeline UI.
                 For example, f6f8558a-6eec-4ef4-b343-a650473ee613]