Deploying CI/CD for Google Cloud Serverless Cloud Functions
Introduction
As the demand for scalable and efficient cloud solutions grows, serverless computing has emerged as a popular option among developers. Google Cloud Functions is one such serverless computing platform that allows developers to build and deploy event-driven applications and microservices without worrying about infrastructure management. Additionally, Google Cloud Build offers a continuous integration and delivery platform that automates the entire development workflow, from building and testing to deploying code changes.
In this blog post, we’ll explore how to deploy CI/CD for Google Cloud Serverless Cloud Functions by integrating Google Cloud Functions with Google Cloud Build. This integration allows developers to streamline the process of building, testing, and deploying their Cloud Functions code, enabling them to release changes more quickly and with greater confidence. So, whether you’re a seasoned developer or just getting started with serverless computing, this post is for you!
What is Google Cloud Function ?
Google Cloud Functions is a service that lets you run code in response to events such as a new HTTP request, an upload to a storage bucket, an invocation from a Google service, and more.
Cloud Functions lets you run code in response to one of these events without having to provision any servers or manage any infrastructure. You can write code in languages such as Python, Go, Java,.NET and Ruby
You can use Cloud Functions to event-driven computing, where the function is triggered in response to an event. For example, you can use Cloud Functions to respond to new images uploaded to a Google Cloud Storage bucket or process a payment from a web hook that your e-commerce store receives from a payment processor.
What is Cloud Build?
Cloud Build is a service that executes your builds on Google Cloud Platform infrastructure.
Cloud Build can import source code from Google Cloud Storage, Cloud Source Repositories, GitHub, or Bitbucket, execute a build to your specifications, and produce artifacts.
Cloud Build executes your build as a series of build steps, where each build step is run in a Docker container by a builder image.
Why Cloud build + Cloud functions
Google Cloud Functions is a serverless computing platform that allows developers to build and deploy event-driven applications and microservices. It provides a cost-effective way to run code without having to manage servers, and automatically scales based on incoming requests.
On the other hand, Google Cloud Build is a continuous integration and delivery platform that automates the process of building, testing, and deploying code changes. By integrating Cloud Build with Cloud Functions, developers can automate the entire process of building, testing, and deploying their code changes to Cloud Functions, enabling them to release changes more quickly and with greater confidence.
This integration helps developers to speed up the development process and improve the quality of their code by automating repetitive tasks and allowing them to focus on writing and testing code. With the help of Cloud Build, developers can easily configure and automate their build, test, and deployment pipelines, and with Cloud Functions, they can build and deploy serverless applications that scale automatically based on incoming requests.
Therefore, the combination of Cloud Build and Cloud Functions provides a powerful toolset for building, testing, and deploying serverless applications on the Google Cloud Platform.
Implementing CI/CD with Cloud function
Write the Cloud Function code and push it to the Git repository.
Set up Cloud Build by creating a cloudbuild.yaml file in the root directory of your Git repository. This file defines the build steps and triggers for Cloud Build.
Define the build steps in the cloudbuild.yaml file. For example, you may want to run tests, build the function, and deploy it to a development environment.
Configure triggers for Cloud Build. You can trigger a build when new code is pushed to the Git repository or on a schedule.
Set up a development environment for your Cloud Function. This could be a Cloud Function deployed to a development project or a virtual machine.
Set up the production environment for your Cloud Function.
Configure Cloud Build to deploy the Cloud Function to the development environment when the build is successful.
Test the Cloud Function in the development environment and make any necessary changes.
Configure Cloud Build to deploy the Cloud Function to devlopment envierment
Overview
Here’s an overview of how the integration works:
- Clone the source code containing cloud build configuration files.
- Configure cloudbuild.yaml file with gcloud builder and define the build steps.
- Compress the file containing main.py and requirements.txt as cloud-function-source.zip and submit it to Cloud Build using gcloud builds submit command.
- Create a trigger in Cloud Build to invoke the build process whenever changes are made to the repository in GitHub.
- Modify the main.py file in the GitHub repository to update the Hello World message.
- As soon as changes are made in the repository, a new build will automatically trigger in Cloud Build and the new function will be deployed.
- Test the deployed function to verify the changes made to the code.
Demo on the CI/CD Using Cloud Function & Cloud build
Step 1 : Clone the source code containing cloud build configuration files
steps:
- name: 'gcr.io/cloud-builders/gcloud'
args:
- functions
- deploy
- hello_world
- --source=.
- --trigger-http
- --runtime=python37
- --allow-unauthenticated
Cloud build is a serverless CI/CD platform on google cloud it has a build configuration which consists of build steps and each step is run into a docker container
Cloud builders are nothing but a bunch of Docker container images, so we are going to use the gCloud image here.
Below are the sample builders from the documentation.
Step 2 : Compress the file which contains main.py and the dependencies file requirements.txt and give it the name cloud-function-source.zip and run the following gcloud commands
gcloud builds submit - config cloudbuild.yaml cloud-function-source.zip
gcloud config set project PROJECT_ID
Now lets jump into the demo
So now we will trigger the deployed function as soon as there are changes made to the repository in this case, which is GitHub. Go to the triggers section in CloudBuild and create a new trigger. Assume you don’t already have a repository; you’ll need to create one (on GitHub), then connect your GitHub account. (Because your GitHub needs permission to send a hook whenever a new commit is made to the branch)
Once the above step is done, you will have the trigger created. Now, go to the triggers under the “cloud build” section and configure them in such a way that if I make any change in any branch, this trigger will be invoked. What this does is find the build configuration file, cloudbuild.yaml.
Now, I will go to my GitHub account and update the main.py file and edit the Hello World message.
def hello_world(request):
"""Responds to any HTTP request.
Args:
request (flask.Request): HTTP request object.
Returns:
The response text or any set of values that can be turned into a
Response object using
`make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
"""
request_json = request.get_json()
if request.args and 'message' in request.args:
return request.args.get('message')
elif request_json and 'message' in request_json:
return request_json['message']
else:
return f'Hello World!!!'
Once I Committed the code a new build will trigger automatically along with that you also get a commit id which will redirect to the GitHub page where the commit was made
Testing the deployed function
we can see the modified changes of main.py in the output section in the console
Final Verdict :
Cloud Functions on Google Cloud Platform (GCP) is a powerful and flexible serverless computing platform that allows developers to easily create and deploy code without having to worry about the underlying infrastructure. In this blog, we have explored various aspects of Google Cloud Functions, including its features, use cases, and benefits.
One of the main benefits of Cloud Functions is its pay-per-use pricing model, which allows developers to pay only for the resources they consume, with no upfront costs or long-term commitments. This makes it a cost-effective option for both small and large-scale applications.
In addition, Google Cloud Functions offers robust security features, including isolation of customer data and resources, automatic security updates, and built-in encryption. This makes it a secure platform for building and deploying critical applications.
Overall, Google Cloud Functions provides a simple and efficient way for developers to build and deploy serverless applications. With its ease of use, flexibility, and cost-effectiveness, it is a compelling option for any developer looking to build modern, event-driven applications on the cloud.
References: https://cloud.google.com/functions/docs/testing/test-cicd
Drop your comments, feedback, or suggestions below — or connect with me directly on Linked @https://www.linkedin.com/in/siddharth-satpute/
Happy learning !!!