Building and Deploying with Google Cloud Build: A Step-by-Step Guide
In the fast-paced world of cloud computing, the need for seamless integration and continuous deployment has never been greater. Google Cloud Build (GCB) is a robust CI/CD tool designed to automate the build, test, and deployment of your applications. In this guide, we’ll walk you through creating a container registry to store Docker images and deploying those images using Cloud Run.
Introduction to Google Cloud Build
Google Cloud Build is a managed service that allows you to automate your builds, tests, and deployments. It integrates with various Google Cloud services and supports multiple languages and environments. By automating these processes, GCB helps developers reduce manual intervention, speed up development cycles, and ensure consistent, reliable deployments.
Prerequisites
Before you begin, make sure you have the following:
- A Google Cloud account
- A project created in Google Cloud
- Basic understanding of Docker
Step 1: Set Up Your Google Cloud Project
- Create a New Project: Visit the Google Cloud Console, click the project dropdown, and select “New Project.”
- Enable Required APIs: In the left sidebar, navigate to APIs & Services > Library, and enable the following APIs:
- Cloud Build API
- Container Registry API
- Cloud Run API
Step 2: Create a Container Registry
You will use Google Container Registry (GCR) to store your Docker images. Here’s how to create it using the Google Cloud UI:
- Navigate to Container Registry: In the Google Cloud Console, go to Container Registry from the left sidebar menu.
- Create a New Repository: Click on “Create Repository.”
- Repository ID: Enter a unique name for your repository.
- Location Type: Choose “Region” and select your preferred region.
- Location: Pick a specific region close to your user base.
Confirm: Click “Create” to finalize the creation of your new container repository.
Step 3: Build and Push Your Docker Image
Follow these steps to build and push your Docker image using the Google Cloud Build UI:
- Navigate to Cloud Build: Go to Cloud Build > Triggers from the left sidebar.
- Create a New Trigger: Click on “Create Trigger.”
- Source: Select the source repository where your cloudbuild.yaml is located. This could be GitHub, Cloud Source Repositories, or another supported repository.
- Event: Choose the event that triggers the build, such as pushing code to the main branch.
- Configuration: Opt for “Cloud Build configuration file” if you have a
cloudbuild.yaml
file or use "Cloud Build Editor" to create it directly in the UI.
Step 4: Deploy to Cloud Run
Integrate the deployment to Cloud Run directly into your Cloud Build pipeline by updating the cloudbuild.yaml
file. Here’s how to configure and understand each step:
- Edit the Build Configuration: Expand your
cloudbuild.yaml
to include the steps for building the Docker image, pushing it to Google Container Registry (GCR), and deploying it to Cloud Run. Here’s a samplecloudbuild.yaml
file and a breakdown of each step:
steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/my-app', '.']
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/my-app']
- name: 'gcr.io/cloud-builders/gcloud'
args: ['run', 'deploy', 'my-app', '--image', 'gcr.io/$PROJECT_ID/my-app', '--platform', 'managed', '--region', 'us-central1', '--allow-unauthenticated']
images: - 'gcr.io/$PROJECT_ID/my-app'
Build the Docker Image:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/my-app', '.']
- name: Specifies the Docker image to use for this step,
gcr.io/cloud-builders/docker
, which contains Docker CLI tools. - args: Defines the arguments to pass to Docker. Here,
build
is the Docker command used to build the image,-t
tags the image with a name (gcr.io/$PROJECT_ID/my-app
), and.
indicates the build context, typically the current directory containing the Dockerfile.
Push the Docker Image to GCR:
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/my-app']
- name: Again, specifies the Docker image to use,
gcr.io/cloud-builders/docker
. - args: Defines the arguments to pass to Docker. Here,
push
is the Docker command used to upload the built image to GCR,gcr.io/$PROJECT_ID/my-app
is the repository location and image name.
Deploy the Docker Image to Cloud Run:
- name: 'gcr.io/cloud-builders/gcloud'
args: ['run', 'deploy', 'my-app', '--image', 'gcr.io/$PROJECT_ID/my-app', '--platform', 'managed', '--region', 'us-central1', '--allow-unauthenticated']
- name: Specifies the gcloud CLI tool for deploying the image.
- args: Defines the arguments for gcloud. The
run deploy
command deploys the image to Cloud Run. - ‘my-app’: Specifies the service name in Cloud Run.
- ‘ — image gcr.io/$PROJECT_ID/my-app’: Points to the Docker image stored in GCR.
- ‘ — platform managed’: Deploys the image to the fully managed Cloud Run environment.
- ‘ — region us-central1’: Sets the region for deployment. Adjust this to your preferred region.
- ‘ — allow-unauthenticated’: Allows public access to the deployed service. For more restricted access, omit this flag or adjust permissions accordingly.
Define the Images Section:
images: - 'gcr.io/$PROJECT_ID/my-app'
- images: Lists the Docker image built in the steps above. This ensures the built image is recorded in Google Cloud Build’s image repository.
2. Save the Configuration:
Ensure this configuration is saved in your source repository as cloudbuild.yaml
.
3. Run the Pipeline:
Manually trigger the build or let it run based on the event configuration in Cloud Build. This process will build your Docker image, push it to GCR, and deploy it to Cloud Run as part of a single automated pipeline.
Conclusion
With Google Cloud Build and Cloud Run, you can create a streamlined pipeline that automates the building, pushing, and deploying of Docker images. Using the Google Cloud UI, you avoid the complexities of the command line while achieving efficient and reliable deployments. This setup ensures that every code change is automatically built and deployed, enhancing your development and operational workflows.
Stay tuned for future posts where we’ll explore more advanced features like managing secrets with Google Secret Manager, accessing Google Cloud Storage buckets via your CI/CD pipeline, and much more!