steps:
  # Login private registry
  - name: 'node:18-slim'
    entrypoint: 'yarn'
    args: ['login:artifact']

  # Install Dependencies
  - name: 'node:18-slim'
    entrypoint: 'yarn'
    args: ['install', '--frozen-lockfile', '--silent']

  # Lint code
  - name: 'node:18-slim'
    entrypoint: 'yarn'
    args: ['lint']

  # Test code
  - name: 'node:18-slim'
    entrypoint: 'yarn'
    args: ['test']

  # Loading Cache Image
  - name: "gcr.io/cloud-builders/docker"
    entrypoint: "bash"
    args: [
            "-c",
            "docker pull ${_REPOSITORY}/$_IMAGE_NAME:$BRANCH_NAME || exit 0"
          ]
    id: Loading Cache Image

  # Build the container image
  - name: "gcr.io/cloud-builders/docker"
    args: [
            "build",
            "--cache-from", "${_REPOSITORY}/$_IMAGE_NAME:$BRANCH_NAME",
            "-t",
            "${_REPOSITORY}/$_IMAGE_NAME:$BRANCH_NAME",
            "."
          ]
    id: Build the container image

  # Push the container image to Artifact Registry
  - name: "gcr.io/cloud-builders/docker"
    args: [
            "push",
            "${_REPOSITORY}/$_IMAGE_NAME:$BRANCH_NAME"
          ]
    id: Push the container image to Container Registry

  # Deploy container image to Cloud Run
  - name: "gcr.io/google.com/cloudsdktool/cloud-sdk:slim"
    entrypoint: gcloud
    args: [
            run, jobs, update, $_SERVICE_NAME,
            --image, $_REPOSITORY/$_IMAGE_NAME:$BRANCH_NAME,
            --region, $_RUN_LOCATION
          ]
    id: Deploy container image to Cloud Run

images:
  - "${_REPOSITORY}/$_IMAGE_NAME:$BRANCH_NAME"

tags:
  - gcp-cloud-build-deploy-cloud-run
  - gcp-cloud-build-deploy-cloud-run-managed
  - $_SERVICE_NAME

timeout: 3600s
