Introduction

Cloud Build is a continuous integration and continuous delivery (CI/CD) service provided by Google Cloud Platform. It allows you to build, test, and deploy applications quickly and at scale. Cloud Build can import source code from various repositories, execute build scripts, and produce artifacts that can be deployed to various environments.

Key Concepts

  1. Build Steps: Individual tasks that are executed during the build process. Each step runs in its own container.
  2. Build Triggers: Automated triggers that start a build when certain conditions are met, such as a code commit.
  3. Build Configurations: YAML or JSON files that define the steps and settings for a build.
  4. Artifacts: The output of a build process, such as compiled binaries, Docker images, or other deployable units.

Setting Up Cloud Build

Prerequisites

  1. GCP Account: Ensure you have a Google Cloud Platform account.
  2. GCP Project: Create or select a GCP project.
  3. Enable Cloud Build API: Enable the Cloud Build API for your project.

Enabling Cloud Build API

  1. Go to the Cloud Build API page.
  2. Click "Enable" to activate the API for your project.

Granting Permissions

Ensure that the Cloud Build service account has the necessary permissions to access your source code and other resources.

gcloud projects add-iam-policy-binding [PROJECT_ID] \
    --member=serviceAccount:[PROJECT_NUMBER]@cloudbuild.gserviceaccount.com \
    --role=roles/owner

Replace [PROJECT_ID] and [PROJECT_NUMBER] with your project's ID and number.

Creating a Build Configuration

A build configuration file defines the steps and settings for your build. This file can be written in YAML or JSON. Below is an example of a simple cloudbuild.yaml file:

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/my-app', '.']
images:
- 'gcr.io/$PROJECT_ID/my-app'

Explanation

  • steps: Defines the build steps. Each step specifies a Docker image and the arguments to pass to it.
    • name: The Docker image to use for this step.
    • args: The arguments to pass to the Docker image.
  • images: Specifies the images to be pushed to the Container Registry after the build.

Running a Build

You can trigger a build manually using the gcloud command-line tool:

gcloud builds submit --config cloudbuild.yaml .

Explanation

  • submit: Submits the build to Cloud Build.
  • --config: Specifies the build configuration file.
  • .: Indicates the current directory as the source code location.

Using Build Triggers

Build triggers allow you to automate builds based on events such as code commits. To create a build trigger:

  1. Go to the Cloud Build Triggers page.
  2. Click "Create Trigger".
  3. Configure the trigger settings, such as the source repository, branch, and build configuration file.

Practical Example

Let's create a simple Node.js application and set up a Cloud Build configuration to build and push a Docker image.

Step 1: Create a Node.js Application

Create a new directory and initialize a Node.js project:

mkdir my-node-app
cd my-node-app
npm init -y

Create a simple index.js file:

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Step 2: Create a Dockerfile

Create a Dockerfile to containerize the application:

FROM node:14

WORKDIR /usr/src/app

COPY package*.json ./
RUN npm install

COPY . .

EXPOSE 3000
CMD [ "node", "index.js" ]

Step 3: Create a Build Configuration

Create a cloudbuild.yaml file:

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/my-node-app', '.']
images:
- 'gcr.io/$PROJECT_ID/my-node-app'

Step 4: Submit the Build

Run the following command to submit the build:

gcloud builds submit --config cloudbuild.yaml .

Exercises

Exercise 1: Create a Build Configuration

Create a cloudbuild.yaml file for a simple Python application that prints "Hello, World!".

Solution

  1. Create a main.py file:
print("Hello, World!")
  1. Create a Dockerfile:
FROM python:3.8-slim

WORKDIR /app

COPY . .

CMD ["python", "main.py"]
  1. Create a cloudbuild.yaml file:
steps:
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/hello-world', '.']
images:
- 'gcr.io/$PROJECT_ID/hello-world'
  1. Submit the build:
gcloud builds submit --config cloudbuild.yaml .

Conclusion

In this section, you learned about Cloud Build, its key concepts, and how to set up and run builds. You also created a practical example to build and push a Docker image for a Node.js application. In the next module, we will explore Cloud Source Repositories and how to integrate them with Cloud Build for a seamless CI/CD pipeline.

© Copyright 2024. All rights reserved