From 208e72daf91509f0928ba88d9b632eafcf7ee033 Mon Sep 17 00:00:00 2001 From: qwiklabs-student Date: Sun, 1 Feb 2026 07:24:45 +0000 Subject: [PATCH] Create cloudbuild.yaml for deployment --- cloudbuild.yaml | 98 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 77 insertions(+), 21 deletions(-) diff --git a/cloudbuild.yaml b/cloudbuild.yaml index feac50e..d485a0a 100644 --- a/cloudbuild.yaml +++ b/cloudbuild.yaml @@ -12,34 +12,90 @@ # See the License for the specific language governing permissions and # limitations under the License. -# [START cloudbuild] + +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START cloudbuild-delivery] steps: -# This step runs the unit tests on the app -- name: 'python:3.7-slim' - id: Test - entrypoint: /bin/sh +# This step deploys the new version of our container image +# in the hello-cloudbuild Kubernetes Engine cluster. +- name: 'gcr.io/cloud-builders/kubectl' + id: Deploy + args: + - 'apply' + - '-f' + - 'kubernetes.yaml' + env: + - 'CLOUDSDK_COMPUTE_REGION=us-west1' + - 'CLOUDSDK_CONTAINER_CLUSTER=hello-cloudbuild' + +# Access the id_github file from Secret Manager, and setup SSH +- name: 'gcr.io/cloud-builders/git' + secretEnv: ['SSH_KEY'] + entrypoint: 'bash' args: - -c - - 'pip install flask && python test_app.py -v' + - | + echo "$$SSH_KEY" >> /root/.ssh/id_rsa + chmod 400 /root/.ssh/id_rsa + cp known_hosts.github /root/.ssh/known_hosts + volumes: + - name: 'ssh' + path: /root/.ssh -# This step builds the container image. -- name: 'gcr.io/cloud-builders/docker' - id: Build +# Clone the repository +- name: 'gcr.io/cloud-builders/git' args: - - 'build' - - '-t' - - 'us-west1-docker.pkg.dev/$PROJECT_ID/my-repository/hello-cloudbuild:$SHORT_SHA' - - '.' + - clone + - --recurse-submodules + - git@github.com:${GITHUB-USERNAME}/hello-cloudbuild-env.git + volumes: + - name: ssh + path: /root/.ssh -# This step pushes the image to Artifact Registry -# The PROJECT_ID and SHORT_SHA variables are automatically +# This step copies the applied manifest to the production branch +# The COMMIT_SHA variable is automatically # replaced by Cloud Build. -- name: 'gcr.io/cloud-builders/docker' - id: Push +- name: 'gcr.io/cloud-builders/gcloud' + id: Copy to production branch + entrypoint: /bin/sh args: - - 'push' - - 'us-west1-docker.pkg.dev/$PROJECT_ID/my-repository/hello-cloudbuild:$SHORT_SHA' -# [END cloudbuild] + - '-c' + - | + set -x && \ + cd hello-cloudbuild-env && \ + git config user.email $(gcloud auth list --filter=status:ACTIVE --format='value(account)') + sed "s/GOOGLE_CLOUD_PROJECT/${PROJECT_ID}/g" kubernetes.yaml.tpl | \ + git fetch origin production && \ + # Switch to the production branch and copy the kubernetes.yaml file from the candidate branch + git checkout production && \ + git checkout $COMMIT_SHA kubernetes.yaml && \ + # Commit the kubernetes.yaml file with a descriptive commit message + git commit -m "Manifest from commit $COMMIT_SHA + $(git log --format=%B -n 1 $COMMIT_SHA)" && \ + # Push the changes back to Cloud Source Repository + git push origin production + volumes: + - name: ssh + path: /root/.ssh + +availableSecrets: + secretManager: + - versionName: projects/${PROJECT_NUMBER}/secrets/ssh_key_secret/versions/1 + env: 'SSH_KEY' +# [END cloudbuild-delivery] options: - logging: CLOUD_LOGGING_ONLY \ No newline at end of file + logging: CLOUD_LOGGING_ONLY