timeout: 14400s

options:
  env:
  - GO111MODULE=auto
  - GOPROXY=https://proxy.golang.org
  volumes:
  - name: go-pkg
    path: /go/pkg
  - name: go-src
    path: /go/src

substitutions:
  _TAG: 'latest'
  _RELEASE_PROJECT_FLAG: 'github.com/GoogleCloudPlatform/compute-image-import/cli_tools/common/utils/param.ReleaseProject'
  _IMAGE_PROJECT: 'compute-image-import'

steps:

# Build import_precheck.
- name: 'golang'
  dir: 'cli_tools/import_precheck'
  args: ['go', 'build', '-o=/workspace/linux/import_precheck']
  env: ['CGO_ENABLED=0']
- name: 'golang'
  dir: 'cli_tools/import_precheck'
  args: ['go', 'build', '-o=/workspace/windows/import_precheck.exe']
  env: ['GOOS=windows']
- name: 'golang'
  dir: 'cli_tools/import_precheck'
  args: ['go', 'build', '-o=/workspace/linux/import_precheck_32bit']
  env:
  - CGO_ENABLED=0
  - GOARCH=386
- name: 'golang'
  dir: 'cli_tools/import_precheck'
  args: ['go', 'build', '-o=/workspace/windows/import_precheck_32bit.exe']
  env:
  - GOOS=windows
  - GOARCH=386

# Build gce_vm_image_import.
- name: 'golang'
  dir: 'cli_tools/gce_vm_image_import'
  args: ['go', 'build', '-o=/workspace/linux/gce_vm_image_import', '-ldflags=-X $_RELEASE_PROJECT_FLAG=$_IMAGE_PROJECT']
  env: ['CGO_ENABLED=0']
- name: 'gcr.io/kaniko-project/executor:v1.1.0'
  args:
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_vm_image_import:$_TAG
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_vm_image_import:$COMMIT_SHA
  - --context=/workspace
  - --dockerfile=gce_vm_image_import.Dockerfile

# Build gce_onestep_image_import.
- name: 'golang'
  dir: 'cli_tools/gce_onestep_image_import'
  args: ['go', 'build', '-o=/workspace/linux/gce_onestep_image_import', '-ldflags=-X $_RELEASE_PROJECT_FLAG=$_IMAGE_PROJECT']
  env: ['CGO_ENABLED=0']
- name: 'gcr.io/kaniko-project/executor:v1.1.0'
  args:
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_onestep_image_import:$_TAG
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_onestep_image_import:$COMMIT_SHA
  - --context=/workspace
  - --dockerfile=gce_onestep_image_import.Dockerfile

# Build gce_vm_image_export.
- name: 'golang'
  dir: 'cli_tools/gce_vm_image_export'
  args: ['go', 'build', '-o=/workspace/linux/gce_vm_image_export', '-ldflags=-X $_RELEASE_PROJECT_FLAG=$_IMAGE_PROJECT']
  env: ['CGO_ENABLED=0']
- name: 'gcr.io/kaniko-project/executor:v1.1.0'
  args:
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_vm_image_export:$_TAG
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_vm_image_export:$COMMIT_SHA
  - --context=/workspace
  - --dockerfile=gce_vm_image_export.Dockerfile

# Build gce_ovf_import.
- name: 'golang'
  dir: 'cli_tools/gce_ovf_import'
  args: ['go', 'build', '-o=/workspace/linux/gce_ovf_import', '-ldflags=-X $_RELEASE_PROJECT_FLAG=$_IMAGE_PROJECT']
  env: ['CGO_ENABLED=0']
- name: 'gcr.io/kaniko-project/executor:v1.1.0'
  args:
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_ovf_import:$_TAG
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_ovf_import:$COMMIT_SHA
  - --context=/workspace
  - --dockerfile=gce_ovf_import.Dockerfile

# Build gce_ovf_export.
- name: 'golang'
  dir: 'cli_tools/gce_ovf_export'
  args: ['go', 'build', '-o=/workspace/linux/gce_ovf_export', '-ldflags=-X $_RELEASE_PROJECT_FLAG=$_IMAGE_PROJECT']
  env: ['CGO_ENABLED=0']
- name: 'gcr.io/kaniko-project/executor:v1.1.0'
  args:
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_ovf_export:$_TAG
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_ovf_export:$COMMIT_SHA
  - --context=/workspace
  - --dockerfile=gce_ovf_export.Dockerfile

# Copy Linux binaries to GCS
- name: 'gcr.io/cloud-builders/gsutil'
  args: ['cp', '/workspace/linux/*', 'gs://$_IMAGE_PROJECT/$_TAG/linux/']

# Copy Windows binaries to GCS
- name: 'gcr.io/cloud-builders/gsutil'
  args: ['cp', '/workspace/windows/*', 'gs://$_IMAGE_PROJECT/$_TAG/windows/']

# Push Wrappers to all the Artifact Registry regions
- name: 'google/cloud-sdk:alpine'
  args:
  - 'bash'
  - '-c'
  - |
    GCP_PROJECTS=($_IMAGE_PROJECT)

    REGIONS=()
    readarray -t REGIONS < ./supported-regions
    TOOLS=("gce_vm_image_import" "gce_onestep_image_import" "gce_vm_image_export" "gce_ovf_import" "gce_ovf_export")

    for _PROJECT in ${GCP_PROJECTS[@]}
      do
      for _REGION in ${REGIONS[@]}
        do
          for _TOOL in ${TOOLS[@]}
          do
            echo "--> Deploying... $$_TOOL -> $$_REGION in $$_PROJECT project."
            gcloud container images add-tag gcr.io/$$_PROJECT/$$_TOOL:$_TAG $$_REGION-docker.pkg.dev/$$_PROJECT/wrappers/$$_TOOL:$_TAG -q
            gcloud container images add-tag gcr.io/$$_PROJECT/$$_TOOL:$COMMIT_SHA $$_REGION-docker.pkg.dev/$$_PROJECT/wrappers/$$_TOOL:$COMMIT_SHA -q
          done
        done
      echo "----> Deployed all tools for all regions in $$_PROJECT project"
    done
