From b17cbde5f16515f9a051b4314a0d43fc0dcf1a98 Mon Sep 17 00:00:00 2001 From: Shivam Gutgutia Date: Sun, 19 Nov 2023 22:11:35 +0530 Subject: [PATCH] Modified cloudbuild file --- cloudbuild.yaml | 59 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/cloudbuild.yaml b/cloudbuild.yaml index 0c13324..48466df 100644 --- a/cloudbuild.yaml +++ b/cloudbuild.yaml @@ -1,30 +1,43 @@ steps: - # Set the project ID as a variable - - name: 'gcr.io/cloud-builders/gcloud' + # Delete previous containers based on the image (if any running) + - name: 'gcr.io/cloud-builders/docker' entrypoint: 'bash' args: - '-c' - | - PROJECT_ID=$(gcloud config get-value project -q) - echo "export PROJECT_ID=${PROJECT_ID}" >> '$HOME/.bashrc' + docker ps -a | grep 'gcr.io/shivam-contacts-generator/github.com/shivamgutgutia/contactsgeneratorbackend' | awk '{print $1}' | xargs -r docker rm -f - # Enable Cloud Logging for the build - - name: 'gcr.io/cloud-builders/gcloud' + # Delete previous images + - name: 'gcr.io/cloud-builders/docker' entrypoint: 'bash' args: - '-c' - | - gcloud config set builds/use_kaniko True + docker images -a | grep 'gcr.io/shivam-contacts-generator/github.com/shivamgutgutia/contactsgeneratorbackend' | awk '{print $3}' | xargs -r docker rmi -f - # Delete previous containers based on the image (if any running) + # Build the new image with the latest commit SHA as a tag - name: 'gcr.io/cloud-builders/docker' + args: + - 'build' + - '-t' + - 'gcr.io/shivam-contacts-generator/github.com/shivamgutgutia/contactsgeneratorbackend:$COMMIT_SHA' + - '-f' + - 'Dockerfile' + - '.' + + # Push the new image to Google Container Registry + - name: 'gcr.io/cloud-builders/docker' + args: + - 'push' + - 'gcr.io/shivam-contacts-generator/github.com/shivamgutgutia/contactsgeneratorbackend:$COMMIT_SHA' + + # Fetch the latest commit SHA from environment variables + - name: 'gcr.io/cloud-builders/gcloud' entrypoint: 'bash' args: - '-c' - | - docker ps -a | grep 'gcr.io/shivam-contacts-generator/github.com/shivamgutgutia/contactsgeneratorbackend' | awk '{print $1}' | xargs -r docker rm -f - - # ... (rest of your build steps) + COMMIT_SHA=$(echo $COMMIT_SHA) # Deploy the new revision to Cloud Run using the updated image with the latest commit SHA - name: 'gcr.io/cloud-builders/gcloud' @@ -36,12 +49,26 @@ steps: --image=gcr.io/shivam-contacts-generator/github.com/shivamgutgutia/contactsgeneratorbackend:$COMMIT_SHA \ --region=asia-south1 \ --project=shivam-contacts-generator \ - --platform=managed \ - --use-http2 \ - --quiet \ - --log-http + && gcloud run services update-traffic contacts-generator-backend-continuous --to-latest + + # Get the latest revision name + - name: 'gcr.io/cloud-builders/gcloud' + entrypoint: 'bash' + args: + - '-c' + - | + LATEST_REVISION=$(gcloud run services describe contacts-generator-backend-continuous --platform=managed --region=asia-south1 --format='value(status.latestRevisionName)') + + # Delete older revisions + - name: 'gcr.io/cloud-builders/gcloud' + entrypoint: 'bash' + args: + - '-c' + - | + gcloud run revisions list --platform=managed --region=asia-south1 --format='value(REVISION)%0A' \ + | grep -v $LATEST_REVISION \ + | xargs -I{} gcloud run revisions delete {} --platform=managed --region=asia-south1 --quiet - # ... (remaining steps) options: logging: CLOUD_LOGGING_ONLY