Table of contents
- Services used in making a CICD pipeline:
- Prerequisites:
- Step 1: Fork the GitHub Repository
- Step 2: Enable Google Cloud Build API
- Step 3: Set Up Google Cloud Build Trigger
- Step 3: Configure cloudbuild.yaml to build and deploy your code
- Step 4: Enabling Permissions
- Step 5: Test the Trigger
- Step 5: Verify Your Application on Cloud Run
In the world of software development, Continuous Integration and Continuous Deployment (CI/CD) pipelines play a crucial role in ensuring the smooth and efficient delivery of applications. In this blog post, we'll explore the creation of a CI/CD pipeline for a unique project – a Game of Thrones character matching app. Our pipeline will use GitHub for version control, Cloud Source Repositories for hosting our code, Cloud Build for automated builds, Cloud Run for deployment, and Artifact Registry for storing artifacts.
Project Overview:
The objective of our project is to create an application that takes a user-inputted character from Game of Thrones and provides a similar character from the series. This fun and interactive app will leverage machine learning models and data analysis to make character recommendations.
This project has been implemented by CampusX
Services used in making a CICD pipeline:
GitHub: We use GitHub for version control, allowing developers to collaboratively commit and track code changes. This ensures versioning and facilitates easy rollbacks if needed.
Cloud Source Repositories: This triggers your pipeline automatically when code is pushed to GitHub. It acts as the starting point for your CI/CD workflow.
Cloud Build: This service builds your code and runs tests automatically, ensuring code quality and stability before deployment. You can define configurations like build steps, dependencies, and testing frameworks.
Artifact Registry: This stores your built artifacts (Docker images etc.) securely and efficiently, making them readily available for deployment.
Cloud Run: This serverless platform deploys your containerized applications in a scalable and cost-effective way. You can easily manage different versions and scale resources based on traffic demands.
Prerequisites:
A Google Cloud project.
An existing code repository on GitHub, https://github.com/AbhayGRT/game-of-thrones-personality-matcher
Step 1: Fork the GitHub Repository
Before you proceed with the tutorial, you'll need to fork the source repository into your own GitHub account. Follow these steps:
Visit the source repository on GitHub: https://github.com/AbhayGRT/game-of-thrones-personality-matcher.
In the upper-right corner of the repository page, click the "Fork" button.
Select your GitHub account where you want to fork the repository.
Wait for the repository to be forked into your GitHub account.
Now you have a copy of the source repository in your GitHub account, and you can follow the tutorial using your forked repository as the source.
Step 2: Enable Google Cloud Build API
Before you can use Google Cloud Build, make sure the Cloud Build API is enabled for your project. To do this:
Go to the Google Cloud Console.
Select your project (or create a new one).
In the left sidebar, navigate to "APIs & Services" > "Library."
Search for "Cloud Build API" and click on it.
Click the "Enable" button if the API is not already enabled.
Step 3: Set Up Google Cloud Build Trigger
Now, we'll set up a trigger to automatically build artifacts whenever code is pushed to your GitHub repository.
Go to the Google Cloud Console.
Select your project.
In the left sidebar, navigate to "Cloud Build" > "Triggers."
Click the "Create Trigger" button.
Fill in the trigger configuration:
Name: Enter a name for your trigger.
Description: (Optional) Provide a description.
Event: Choose "Push to a branch."
Source: Select "1st gen".
GitHub App: Click the "Connect Repository" button to link your GitHub account.
GitHub repository: Select your existing GitHub repository, such as "your-github-username/app."
Included Branch: Specify the branch you want to trigger the build on.
Configuration: Choose "Use a configuration file (YAML or JSON)."Cloud Build Configuration File: Enter the path to your cloudbuild.yaml file, which contains the build steps and configurations for your project. If it's in the root directory, simply enter .cloudbuild.yaml.
We have the provided cloud build config in our repository.
Click the "Create" button to create the Cloud Build trigger.
Step 3: Configure cloudbuild.yaml to build and deploy your code
To enable the deployment of your containerized application to Google Cloud Run, you need to add the following config to your cloudbuild.yaml file. Replace the project_id variable accordingly.
# Define variables for project ID, image name, and tag.
# Customize these values as needed.
substitutions:
_PROJECT_ID: 'stoked-castle-409104' # Replace with your actual project ID.
_IMAGE_NAME: 'gcr.io/${_PROJECT_ID}/game-of-thrones-personality-matcher'
_TAG_NAME: 'latest'
_REGION: 'us-central1' # Replace with your preferred region.
_SERVICE_NAME: 'game-of-thrones-servicename' # Replace with your preferred Cloud Run service name.
steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', '${_IMAGE_NAME}:${_TAG_NAME}', '.']
# Push the Docker image to Google Container Registry.
- name: 'gcr.io/cloud-builders/docker'
args: ['push', '${_IMAGE_NAME}:${_TAG_NAME}']
# Deploy to Cloud Run.
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: 'gcloud'
args:
- 'run'
- 'deploy'
- '${_SERVICE_NAME}'
- '--image=${_IMAGE_NAME}:${_TAG_NAME}'
- '--platform=managed'
- '--region=${_REGION}'
- '--port=8501' # Specified for the port number that you want to use
- '--allow-unauthenticated' # Optional: Remove this line if you don't want to allow unauthenticated access.
images:
- '${_IMAGE_NAME}:${_TAG_NAME}' # Reference the dynamic image name and tag.
Here is a brief explanation of what this cloudbuild.yaml file achieves
Build Docker Image: Automatically create a Docker image for your application.
Push to GCR: Store the Docker image in the Google Container Registry.
Deploy to Cloud Run: Automatically deploy the image to Google Cloud Run upon code changes.
Step 4: Enabling Permissions
Go to "Cloud Build" > "Settings"
In the Service Account section enable Cloud Run, Service Account and Cloud Build.
Step 5: Test the Trigger
To test your Cloud Build trigger:
Push a code change to the specified branch of your GitHub repository (e.g., https://github.com/your-github-username/app).
Go to the Google Cloud Console.
Select your project.
In the left sidebar, navigate to "Cloud Build" > "History."
You should see a new build triggered by your Cloud Build trigger. Click on the build to view its details.
In the build details, you can see the build steps and logs and when the build is successful you should see a docker image in your artifact repository built from just a single commit to your source repo.
Step 5: Verify Your Application on Cloud Run
Now that your application is built and deployed automatically, you can verify it on Google Cloud Run:
1. Go to the Google Cloud Console.
2. Select your project.
3. In the left sidebar, navigate to "Cloud Run."
4. You should see your Cloud Run service with the name you specified in the cloudbuild.yaml file (e.g., "your-cloud-run-service"). Click on it.
5. You can access your deployed application by clicking the service URL provided on the Cloud Run tab.
Output:
That's it! You've successfully set up Google Cloud Build to automatically build and deploy your containerized application to Google Cloud Run whenever code changes are pushed to your GitHub repository. This automated pipeline streamlines your development workflow, making it easier to release updates and improvements to your application while ensuring consistency and reliability in the deployment process.