GCP DevOps Project

Sprint 04

Sprint 04 review

Welcome back! In this Sprint 04 review, we’ll recap our objectives and confirm that each has been met, from configuring Cloud Build to publishing container images.

Sprint 04 Goals

Goal IDObjectiveDeliverable
1Understand Google Cloud Buildcloudbuild.yaml configuration files
2Connect Cloud Build to GitHubBuild trigger on GitHub push
3Automate Docker image buildsDockerfile in application repo
4Store Docker images in Artifact RegistryImages pushed to GCP registry

Implementation Steps

1. Configure Google Cloud Build

We defined build steps in a cloudbuild.yaml file to install dependencies, run tests, and build the Docker image:

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

Note

Ensure the Cloud Build service account has roles/artifactregistry.writer and roles/storage.admin for pushing images.

We created a trigger so that any push to the main branch starts a build:

gcloud beta builds triggers create github \
  --name="on-main-commit" \
  --repo-name="my-repo" \
  --repo-owner="my-org" \
  --branch-pattern="^main$" \
  --build-config="cloudbuild.yaml"

3. Automate Docker Image Builds

Our Dockerfile defines how the application is containerized:

FROM node:18-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --production
COPY . .
CMD ["node", "server.js"]
EXPOSE 8080

This file ensures dependencies install and the app runs on port 8080.

4. Publish to Artifact Registry

After building, Cloud Build pushes to Google Cloud Artifact Registry:

gcloud artifacts repositories create my-repo \
  --repository-format=docker \
  --location=us-central1

# Already covered by cloudbuild.yaml, but manual push example:
docker tag my-app gcr.io/$PROJECT_ID/my-app:$COMMIT_SHA
docker push gcr.io/$PROJECT_ID/my-app:$COMMIT_SHA

Warning

Verify that your Artifact Registry repository is in the same region as your Cloud Build trigger to avoid latency issues.

Results and Next Steps

  • All build steps in cloudbuild.yaml execute successfully.
  • GitHub pushes to main automatically trigger Cloud Build.
  • Docker images build without manual intervention.
  • Images are stored in Artifact Registry and available for deployment.

With Sprint 04 complete, our CI/CD pipeline is fully automated on Google Cloud Platform.

Thank you, and see you in the next lesson!


References

Watch Video

Watch video content

Previous
Quick walkthrough