#
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, eithern express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

timeout: 3600s
substitutions:
  _ZONE: 'asia-northeast1-a'
  _COMPUTE_IMAGE: 'silicon-design-ubuntu-2004'
  _CONTAINER_IMAGE: 'silicon-design-ubuntu-2004'
  _NOTEBOOKS_BUCKET: 'silicon-design-notebooks'
  _COMPUTE_NETWORK: 'global/networks/default'
  _COMPUTE_SUBNET: ''
  _CLOUD_BUILD_SA: ''
options:
  logging: CLOUD_LOGGING_ONLY
steps:
- id: 'notebooks-build'
  name: 'python'
  entrypoint: '/bin/bash'
  args:
  - '-c'
  - |-
    python3 -m venv env-jupytext/
    env-jupytext/bin/python -m pip install jupytext
    env-jupytext/bin/jupytext --to notebook scripts/build/notebooks/*.md
    echo 'gsutil cp gs://$_NOTEBOOKS_BUCKET/*.ipynb /home/jupyter/' > scripts/build/notebooks/copy-notebooks.sh
  waitFor: ['-']
- id: 'compute-image-build'
  name: 'gcr.io/cloud-builders/gcloud'
  entrypoint: '/bin/bash'
  args:
  - '-c'
  - |-
    cd scripts/build/images/
    gsutil cp gs://compute-image-tools/release/linux/daisy .
    chmod +x daisy
    ./daisy -project $PROJECT_ID -zone $_ZONE -variables image_name=$_COMPUTE_IMAGE,image_tag=$BUILD_ID,network=$_COMPUTE_NETWORK,subnet=$_COMPUTE_SUBNET,service_account=$_CLOUD_BUILD_SA compute_image.wf.json
  waitFor: ['-']
- id: 'container-image-build'
  name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', '$_CONTAINER_IMAGE:$BUILD_ID', './scripts/build/images']
  waitFor: ['-']
- id: 'container-image-tag'
  name: 'gcr.io/cloud-builders/docker'
  args: ['tag', '$_CONTAINER_IMAGE:$BUILD_ID', '$_CONTAINER_IMAGE:latest']
  waitFor: ['container-image-build']
- id: 'container-image-test'
  name: 'gcr.io/cloud-builders/docker'
  args: ['run', '$_CONTAINER_IMAGE:$BUILD_ID', 'flow.tcl', '-design', 'inverter']
  waitFor: ['container-image-tag']
images:
- '$_CONTAINER_IMAGE:$BUILD_ID'
- '$_CONTAINER_IMAGE:latest'
artifacts:
  objects:
    location: gs://$_NOTEBOOKS_BUCKET/
    paths:
    - 'scripts/build/notebooks/copy-notebooks.sh'
    - 'scripts/build/notebooks/*.ipynb'
