google-cloud-platformgoogle-cloud-rungoogle-cloud-buildgoogle-secret-manager

GCP Cloud Run - Cannot update environment variable to string literal because it has already been set with a different type


Problem

First of all, this is the error that I have in Cloud Run.

Step #2 - "deploy-image-to-cloud-run": ERROR: (gcloud.run.deploy) Cannot update environment variable [MONGODB_CONNECTION_STRING] to string literal because it has already been set with a different type.

CI/CD Flow

I have a cloudbuild.yaml. Whenever changes are pushed to the main branch, the automated build and deployment will happen.

Build Docker image -> Push Image to Container Registry -> Deploy to Cloud Run

steps:
  # Build the container image
  ...
  # Push the container image to Container Registry
  ...
  # Deploy container image to Cloud Run
  - id: 'deploy-image-to-cloud-run'
    name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: bash
    args: [
        '-c',
        'gcloud run deploy my-project --image gcr.io/my-project/backend:$COMMIT_SHA
        --region us-central1
        --set-env-vars MONGODB_CONNECTION_STRING=$$SECRET_MONGODB_CONNECTION_STRING
        ',
      ]
    secretEnv: ['SECRET_MONGODB_CONNECTION_STRING']
images:
  - gcr.io/my-project/backend:$COMMIT_SHA
availableSecrets:
  secretManager:
    - versionName: projects/my-project/secrets/MONGODB_CONNECTION_STRING/versions/1
      env: 'SECRET_MONGODB_CONNECTION_STRING'

In Secret Manager
enter image description here

I've tried to search for related error, but only a few related issues without a solution.


Solution

  • Solved it by updating cloudbuild.yaml as below

    steps:
      # Build the container image
      ...
      # Push the container image to Container Registry
      ...
      # Deploy container image to Cloud Run
      - id: 'deploy-image-to-cloud-run'
        name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
        entrypoint: bash
        args: [
            '-c',
            'gcloud run deploy my-project --image gcr.io/my-project/backend:$COMMIT_SHA
            --region us-central1
            --set-env-vars MONGODB_CONNECTION_STRING=MONGODB_CONNECTION_STRING:1
            ',
          ]
    images:
      - gcr.io/my-project/backend:$COMMIT_SHA