# Tag all the 'release' docker images as 'prev-release' so they can be rollback.

timeout: 1800s

steps:
- name: 'google/cloud-sdk:alpine'
  args:
  - 'bash'
  - '-c'
  - |
    REGIONS_ARR=$(echo "$_REGIONS" | tr ";" "\n")
    TOOLS_ARR=$(echo "$_TOOLS" | tr ";" "\n")
    GCP_PROJECTS_ARR=($_IMAGE_PROJECT)

    exit_on_error() {
      ERR=$$1
      if [[ $$ERR != 0 ]]; then
      echo "Build preparation failed with exit code $$ERR. Please check the error message, debug and rerun."
      exit "$$ERR"
      fi
    }

    for _PROJECT in ${GCP_PROJECTS_ARR[@]}
    do
      echo "--> Start Tagging prev-release for project $$_PROJECT"
      for _REGION in $$REGIONS_ARR
      do
        for _TOOL in $$TOOLS_ARR
        do
          echo "--> Tagging prev-release... $$_TOOL -> $$_REGION"
          gcloud container images add-tag $$_REGION-docker.pkg.dev/$$_PROJECT/wrappers/$$_TOOL:release $$_REGION-docker.pkg.dev/$$_PROJECT/wrappers/$$_TOOL:prev-release -q
          exit_on_error $$?
        done
        echo "----> Tagged all tools for $$_REGION done."
      done
      echo "--> Finished Tagging prev-release for project $$_PROJECT"
    done


    for _PROJECT in ${GCP_PROJECTS_ARR[@]}
    do
      for _TOOL in $$TOOLS_ARR
      do
        echo "--> Tagging prev-release for project $$_PROJECT... $$_TOOL -> gcr.io"

        gcloud container images add-tag gcr.io/$$_PROJECT/$$_TOOL:release gcr.io/$$_PROJECT/$$_TOOL:prev-release -q
        exit_on_error $$?
      done
    done
    echo "----> Tagged all tools for gcr.io done."
