# Build all the docker images and put them into container registry (gcr.io)
# Later, these images will be copied to artifacts registry on demand.

timeout: 1800s

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

substitutions:
  _RELEASE: 'release'
  _PREV_RELEASE: 'prev-release'
  _RELEASE_PROJECT_FLAG: 'github.com/GoogleCloudPlatform/compute-image-import/cli_tools/common/utils/param.ReleaseProject'

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:$_RELEASE
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_vm_image_import:$COMMIT_SHA
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_vm_image_import:$_WORKFLOW_EXECUTION_ID
  - --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:$_RELEASE
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_onestep_image_import:$COMMIT_SHA
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_onestep_image_import:$_WORKFLOW_EXECUTION_ID
  - --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:$_RELEASE
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_vm_image_export:$COMMIT_SHA
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_vm_image_export:$_WORKFLOW_EXECUTION_ID
  - --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:$_RELEASE
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_ovf_import:$COMMIT_SHA
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_ovf_import:$_WORKFLOW_EXECUTION_ID
  - --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:$_RELEASE
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_ovf_export:$COMMIT_SHA
  - --destination=gcr.io/$_IMAGE_PROJECT/gce_ovf_export:$_WORKFLOW_EXECUTION_ID
  - --context=/workspace
  - --dockerfile=gce_ovf_export.Dockerfile

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

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

# Make binaries world-readable - _IMAGE_PROJECT
- name: 'gcr.io/cloud-builders/gsutil'
  args: ['-m', 'acl', '-r', 'ch', '-u', 'AllUsers:R', 'gs://$_IMAGE_PROJECT/$_RELEASE/*']
