0

I have a GCP project where I continuously deploy changes (PRs) made to a GitHub repository to a cloud-run service using cloud build triggers
the way i set it up at first is that i use GCP GUI enter image description here

this results in a trigger in cloud-build\ the cloud-build trigger has the yaml file that looks like this

  - name: gcr.io/cloud-builders/docker
    args:
      - build
      - '--no-cache'
      - '-t'
      - '$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
      - .
      - '-f'
      - Dockerfile
    id: Build
  - name: gcr.io/cloud-builders/docker
    args:
      - push
      - '$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
    id: Push
  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:slim'
    args:
      - run
      - services
      - update
      - $_SERVICE_NAME
      - '--platform=managed'
      - '--image=$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
      - >-
        --labels=managed-by=gcp-cloud-build-deploy-cloud-run,commit-sha=$COMMIT_SHA,gcb-build-id=$BUILD_ID,gcb-trigger-id=$_TRIGGER_ID,$_LABELS
      - '--region=$_DEPLOY_REGION'
      - '--quiet'
    id: Deploy
    entrypoint: gcloud
images:
  - '$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
options:
  substitutionOption: ALLOW_LOOSE
substitutions:
  _PLATFORM: managed
  _SERVICE_NAME: bordereau
  _DEPLOY_REGION: europe-west1
  _LABELS: gcb-trigger-id=((a long random id goes here))
  _TRIGGER_ID: ((an other long random id goes here))
  _GCR_HOSTNAME: eu.gcr.io
tags:
  - gcp-cloud-build-deploy-cloud-run
  - gcp-cloud-build-deploy-cloud-run-managed
  - bordereau

when ever this trigger is run, a new cloud-run revision is created like this enter image description here

then i can create a url that points to a specific url like this enter image description here

enter image description here

enter image description here

that helps me access each revision using its unique URL
i tried many ways to eddit the cloud-build YAML file to give each revision a unique URL automaticly ( not manually through the GCP GUI ) but i dont seem to find a way! i tried many keywords, and read the documentation but that didnt help either!
any help is very much appreciated.
it would be great if the revision URL (tag) was something unique and short like first charecters of the commit SHA or the PR number

  • 1
    Did you try something like that `gcloud run services update-traffic --set-tags=....`?? – guillaume blaquiere Jan 31 '23 at 13:46
  • how would that be written in the yaml file? – Mustapha Khial Jan 31 '23 at 14:48
  • 1
    Exactly the same way as you wrote your `id: Deploy` step in Cloud Build. Use the gcloud step to invoke that new update traffic method. but, is your problem related to the tags name creation (string manipulation in bash script before invoking the update service method)? – guillaume blaquiere Jan 31 '23 at 20:12
  • i added a step that does what you told me, but the build stays forever. can you please try to edit the provided file that way you think it will work? – Mustapha Khial Feb 01 '23 at 23:05

1 Answers1

0

Usually you can do like that (see step id: tag)

  - name: gcr.io/cloud-builders/docker
    args:
      - build
      - '--no-cache'
      - '-t'
      - '$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
      - .
      - '-f'
      - Dockerfile
    id: Build
  - name: gcr.io/cloud-builders/docker
    args:
      - push
      - '$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
    id: Push
  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:slim'
    args:
      - run
      - services
      - update
      - $_SERVICE_NAME
      - '--platform=managed'
      - '--image=$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
      - >-
        --labels=managed-by=gcp-cloud-build-deploy-cloud-run,commit-sha=$COMMIT_SHA,gcb-build-id=$BUILD_ID,gcb-trigger-id=$_TRIGGER_ID,$_LABELS
      - '--region=$_DEPLOY_REGION'
      - '--quiet'
    id: Deploy
    entrypoint: gcloud
  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:slim'
    args:
      - -c
      - |
         export sha=$COMMIT_SHA
         export CUSTOM_TAG=${sha:0:8}
         export CURRENT_REV=$(gcloud alpha run services describe $_SERVICE_NAME --region=$_DEPLOY_REGION --platform=managed --format='value(status.traffic[0].revisionName)')
         gcloud run services update-traffic $_SERVICE_NAME --set-tags=$$CUSTOM_TAG=$$CURRENT_REV --region=$_DEPLOY_REGION --platform=managed
    id: tag
    entrypoint: bash
images:
  - '$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
options:
  substitutionOption: ALLOW_LOOSE
substitutions:
  _PLATFORM: managed
  _SERVICE_NAME: bordereau
  _DEPLOY_REGION: europe-west1
  _LABELS: gcb-trigger-id=((a long random id goes here))
  _TRIGGER_ID: ((an other long random id goes here))
  _GCR_HOSTNAME: eu.gcr.io
tags:
  - gcp-cloud-build-deploy-cloud-run
  - gcp-cloud-build-deploy-cloud-run-managed
  - bordereau

In that custom tag, I put the 8 first character of the commit SHA

You can note the weird env var COMMIT_SHA copy to a local env var. It's a strange thing with CloudBuild.

guillaume blaquiere
  • 66,369
  • 2
  • 47
  • 76
  • the first attempt using the changes you suggested caused a syntax error, so i corrected it to this --set-tags=prbuild=$$CUSTOM_TAG but then the build started to hang at step 3 'deploy', and stops at creating revision ...... done any thing you might suggest? note that the build works fine when the added step is not there. – Mustapha Khial Feb 02 '23 at 20:27
  • 1
    My bad, it's not so simple. Let me test I will update my answer – guillaume blaquiere Feb 02 '23 at 20:33
  • @MustaphaKhial, it should work now! – guillaume blaquiere Feb 02 '23 at 20:43