yamlcontinuous-integrationgoogle-cloud-buildgoogle-secret-managergoogle-artifact-registry

My secrets are blank in my cloudbuild.yaml file


I'm currently learning how to do CI/CD in GCP. In a personal project I try to make a CI/CD between my GitHub repository and my artifact registry depo and my cloud run service. Also, I set up all my secrets in google cloud. I use google cloud build and I set up a cloud build.yaml file:

steps:
  - name: 'bash'
    entrypoint: 'bash'
    args:
      - '-c'
      - |
        echo "Test de l'URL de l'image:"
        echo -n "$$_LOCATION-docker.pkg.dev/${PROJECT_ID}/$$_REPOSITORY/$$_IMAGE:$$_TAG"
        echo "Location: $$_LOCATION"
        echo "Repository: $$_REPOSITORY"
        echo "Image: $$_IMAGE"
        echo "Tag: $$_TAG"
    secretEnv: ['_LOCATION', '_REPOSITORY', '_IMAGE', '_TAG']

  # Build et push de l'image container vers Artifact Registry
  - name: 'gcr.io/cloud-builders/docker'
    args: [ 'build', '-t', '$_LOCATION-docker.pkg.dev/$PROJECT_ID/$_REPOSITORY/$_IMAGE', '.' ]
    secretEnv: ['_LOCATION', '_REPOSITORY', '_IMAGE', '_TAG']

  # Déploiement vers Cloud Run
  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: gcloud
    args:
      - 'run'
      - 'deploy'
      - '$_SERVICE_NAME'
      - '--image'
      - '$$_LOCATION-docker.pkg.dev/${PROJECT_ID}/$$_REPOSITORY/$$_IMAGE:$$_TAG'
      - '--region'
      - '$_REGION'
      - '--platform'
      - 'managed'
    secretEnv: ['_LOCATION', '_REPOSITORY', '_IMAGE', '_TAG', '_SERVICE_NAME', '_REGION']

availableSecrets:
  secretManager:
    - versionName: projects/${PROJECT_ID}/secrets/_LOCATION/versions/latest
      env: '_LOCATION'
    - versionName: projects/${PROJECT_ID}/secrets/_REPOSITORY/versions/latest
      env: '_REPOSITORY'
    - versionName: projects/${PROJECT_ID}/secrets/_IMAGE/versions/latest
      env: '_IMAGE'
    - versionName: projects/${PROJECT_ID}/secrets/_TAG/versions/latest
      env: '_TAG'
    - versionName: projects/${PROJECT_ID}/secrets/_SERVICE_NAME/versions/latest
      env: '_SERVICE_NAME'
    - versionName: projects/${PROJECT_ID}/secrets/_REGION/versions/latest
      env: '_REGION'

I have this issue:

Step #1: invalid argument "-docker.pkg.dev/[MY_PROJECT_ID]//" for "-t, --tag" flag: invalid reference format

I try to use the $$ argument but I have this issue:

"$_LOCATION-docker.pkg.dev/[MY_PROJECT_ID]/$_REPOSITORY/$_IMAGE$"

My project id is the only variable I can retrieve. Also I set up in step 0 a test:

steps:
  - name: 'bash'
    entrypoint: 'bash'
    args:
      - '-c'
      - |
        echo "Test de l'URL de l'image:"
        echo -n "$$_LOCATION-docker.pkg.dev/${PROJECT_ID}/$$_REPOSITORY/$$_IMAGE:$$_TAG"
        echo "Location: $$_LOCATION"
        echo "Repository: $$_REPOSITORY"
        echo "Image: $$_IMAGE"
        echo "Tag: $$_TAG"
    secretEnv: ['_LOCATION', '_REPOSITORY', '_IMAGE', '_TAG']

And in the log I have those results:

Step #0: Status: Downloaded newer image for bash:latest
Step #0: docker.io/library/bash:latest
Step #0: Test de l'URL de l'image:
Step #0: europe-west1-docker.pkg.dev/[MY_PROJECT_ID]/streamlit/portofolio-flenne:1Location: europe-west1
Step #0: Repository: streamlit
Step #0: Image: portofolio-flenne
Step #0: Tag: 1

How can I resolve this?


Solution

  • Sorry didn't have time to read your comment until now.

    Variable substitution can be challenging.

    I think the issue is that you're not getting an (shell) evaluation of your environment variables in the step's args: [...].

    You've demonstrated the variables are substituted in the Bash debug step.

    gcr.io/cloud-builders/docker contains a Bash shell, so you can:

    - name: gcr.io/cloud-builders/docker
      entrypoint: bash
      args:
      - -c
      - |
        docker build \
        --tag=$${_LOCATION}-docker.pkg.dev/${PROJECT_ID}/$${_REPOSITORY}/$${_IMAGE} \
        --file=./Dockerfile \
        .