DEVELOPER BLOG

HOME > DEVELOPER BLOG > 【Introduction to GCP Automation】Building Container Images with Cloud Build - PrismScaler

【Introduction to GCP Automation】Building Container Images with Cloud Build - PrismScaler

1. Introduction

Hello! We are a writer team from Definer Inc. Building container images with Cloud Build is a service provided by Google Cloud Platform (GCP) that allows you to automate the process of building and deploying containerized applications. Cloud Build provides a managed environment to build container images using build configurations defined in configuration files, such as cloudbuild.yaml. It integrates seamlessly with other GCP services and supports popular containerization technologies like Docker. In this issue, you are wondering about the use of container image build with Cloud Build. Let's take a look at the actual screens and resources to explain in detail.

2. Purpose/Use Cases

Purpose of Building Container Images with Cloud Build: The purpose of building container images with Cloud Build is to streamline and automate the process of creating containerized applications, enabling efficient development and deployment workflows. Here are some key purposes and benefits of using Cloud Build for building container images: (1) Automation and Reproducibility (2) Scalability and Speed: (3) Integration with GCP Services: (4) Customizability and Flexibility (5) Version Control and Collaboration (6) Cost Optimization

3. What is Cloud Build?

First, let's review Cloud Build. Cloud Build is a CI/CD pipeline building service for Google Cloud. Similar services include CircleCI and GitHub Actions.   The functional advantage is that it is an official Google Cloud service, so you don't have to consider the cost and risk of managing authentication information compared to other services. The disadvantage is that there are some features that exist in other CI/CD services but not in Cloud Build. For example, Slack integration.

4. Cloud Build Setup

Now let's set up Cloud Build.   (1) Connection with GitHub: To establish a connection between Cloud Build and your GitHub repository, follow these steps:  - Go to the Google Cloud Console and navigate to "Cloud Build" -> "Triggers".  - In the "Repository Management" section, click on "Repository Connection".  - You will be prompted to log in to your GitHub account and grant access to Cloud Build.  - Once logged in, select the GitHub repository you want to connect with Cloud Build. By connecting your GitHub repository to Cloud Build, you enable Cloud Build to automatically trigger builds when changes are pushed to the repository.   (2) Trigger settings: To configure triggers in Cloud Build, follow these steps:  - Go to the Google Cloud Console and navigate to "Cloud Build" -> "Triggers".  - Click on "Create Trigger" to create a new trigger.  - Select the desired trigger event based on your workflow requirements. Common options include "Push to branch", "Push new tag", and "Pull request".  - Specify the source repository and branch that should trigger the build.  - Choose the "Cloud Build configuration file" as the build configuration. This refers to the configuration file, often named cloudbuild.yaml, which defines the build steps and settings for the container image. By setting up triggers, you define the conditions that will initiate a build in Cloud Build. These triggers can be based on events like code pushes, new tags, or pull requests in your GitHub repository. The configuration file, such as cloudbuild.yaml, contains the instructions for Cloud Build on how to build the container image. It includes build steps, environment variables, and other settings specific to your application and build process. This configuration file is stored in your GitHub repository and will be used by Cloud Build when the trigger conditions are met. By connecting GitHub with Cloud Build and configuring triggers, you can automate the build process for your container images whenever there are changes in your GitHub repository. This integration streamlines your development workflow and ensures that your container images stay up-to-date with the latest code changes.     (3) Create/push cloudbuild.yaml: To create and push the cloudbuild.yaml file, which serves as the configuration file for Cloud Build, follow these steps:  - Create a new file named cloudbuild.yaml in the root directory of your GitHub repository.  - Open the cloudbuild.yaml file in a text editor and define the necessary build steps and configurations. This file is written in YAML format.  - Specify the deployment target for your build. In this example, the target is Cloud Functions, but it can be adjusted based on your specific use case.  - Define the necessary steps in the cloudbuild.yaml file. These steps can include building the container image, running tests, and deploying the application to the chosen target (e.g., Cloud Functions).  - Once you have defined the cloudbuild.yaml file, commit and push it to your GitHub repository.   After pushing to Git, Cloud Build is kicked and we can confirm that the deployment to Cloud Functions is running!
## cloudbuild.yaml

steps:
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
  args:
  - gcloud
  - functions
  - deploy
  - cloud-functions-sample
  - --region=${Region Name}
  - --source=.
  - --trigger-http
  - --runtime=python38                
In the provided cloudbuild.yaml, the build steps are as follows:

(1) The name field specifies the container image used to execute the build steps. In this case, it uses the gcr.io/google.com/cloudsdktool/cloud-sdk image, which contains the Google Cloud SDK (gcloud) and other necessary tools.

(2) The args field provides the command-line arguments that will be passed to the container image (Google Cloud SDK) for execution. Here's a breakdown of the arguments:

 - gcloud: This instructs the Cloud SDK to run the gcloud command.

 - functions: This indicates that we are working with Google Cloud Functions, which allows you to deploy serverless functions.

 - deploy: This tells the Cloud SDK to deploy a Cloud Function.

 - cloud-functions-sample: This is the name of the Cloud Function that will be deployed.

 - --region=${Region Name}: Replace ${Region Name} with the desired region where you want to deploy the Cloud Function. Regions are geographical locations where Google Cloud services are hosted.

 - --source=.: This specifies the source directory for the Cloud Function code. The . indicates that the current directory (where the cloudbuild.yaml file is located) contains the code to deploy.

 - --trigger-http: This configures the deployed Cloud Function to be triggered by HTTP requests. This means the function can be accessed using HTTP URLs.

 - --runtime=python38: This indicates the runtime environment for the Cloud Function, in this case, Python 3.8.

When you trigger the Cloud Build using this cloudbuild.yaml file, it will use the specified Google Cloud SDK container image to execute the gcloud functions deploy command with the provided arguments.

5. Cited/Referenced Articles

6. About the proprietary solution "PrismScaler"

・PrismScaler is a web service that enables the construction of multi-cloud infrastructures such as AWS, Azure, and GCP in just three steps, without requiring development and operation. ・PrismScaler is a web service that enables multi-cloud infrastructure construction such as AWS, Azure, GCP, etc. in just 3 steps without development and operation. ・The solution is designed for a wide range of usage scenarios such as cloud infrastructure construction/cloud migration, cloud maintenance and operation, and cost optimization, and can easily realize more than several hundred high-quality general-purpose cloud infrastructures by appropriately combining IaaS and PaaS.  

7. Contact us

This article provides useful introductory information free of charge. For consultation and inquiries, please contact "Definer Inc".

8. Regarding Definer

・Definer Inc. provides one-stop solutions from upstream to downstream of IT. ・We are committed to providing integrated support for advanced IT technologies such as AI and cloud IT infrastructure, from consulting to requirement definition/design development/implementation, and maintenance and operation. ・We are committed to providing integrated support for advanced IT technologies such as AI and cloud IT infrastructure, from consulting to requirement definition, design development, implementation, maintenance, and operation. ・PrismScaler is a high-quality, rapid, "auto-configuration," "auto-monitoring," "problem detection," and "configuration visualization" for multi-cloud/IT infrastructure such as AWS, Azure, and GCP.