Continous Integration with Cloud Build in GCP

We will create a container with the "Hello World" App in Flask, store the container in GCP Container Registry, and configure Continous Deployment through GCP Cloud Build. Creating a Source Repository in GCP First, we must go to Source Repos

AI-assisted translation, approved by Mateo.

We will create a container with the "Hello World" App in Flask, store the container in GCP Container Registry, and configure Continous Deployment through GCP Cloud Build.

Creating a Source Repository in GCP

  • First, we must go to Source Repositories in GCP:
  • Then, click on "+Add a Repository"
  • Select "Create New Repository"
  • Click Continue.
  • Now we must assign a name and select the project with which it will be associated.
  • Our Source Repository will now be created. However, as the screenshot indicates, it will be empty, as shown in the screenshot below.

Cloning the repository

To interact with our repository, we must open Cloud Shell or use the GCloud CLI. In this case, we will use the gcloud CLI. To sign in with our project, you can follow this walkthrough: Install the gcloud CLI | Google Cloud CLI Documentation

Download Git CLI Tools

For this walkthrough section, we must have the GIT tools installed on our machine https://git-scm.com/downloads
  • First, we must create a folder to associate with our Git repository.
  • To clone our Source Repositories repository, we must use the following command:
Bash
cd C:/
mkdir "C:/<folder-destino-Source-Repo>"
cd "C:/<folder-destino-Source-Repo>"
gcloud source repos clone <nombre-del-repo>
  • In this case, the repo is called "devops-repo". When we use this command, our GIT repository will be associated with this folder.
Bash
cd ..
mkdir "C:/<folder-destino-FlaskApp-Github-Repo>"
cd "C:/<folder-destino-FlaskApp-Github-Repo>"
git clone https://github.com/mdiloreto/flask-app.git
  • Once both repos have been cloned through git in their respective folders, we must copy the information from our Flask-App on GitHub to our source repository folder.
  • Once copied, we must perform a GIT Push to our GCP Source Repository.
  • For this, we will use the following commands:
Bash
cd "C:/<folder-destino-Source-Repo>"
git add .
git commit -m "Initial Flask App commit"
git push origin master
  • By performing this operation, we will be pushing our information (synchronizing our "local" with the remote) to the GCP repository.
  • We can confirm in the following image that the operation was successful.

Building the image

  • Now, we will perform our first image Build.
  • We will perform this build WITHOUT Continous Integration.
  • We will build it using the "gcloud builds" command from the gcloud CLI tool.
  • First, we must set our $PROJECT_ID variable.
PowerShell
$PROJECT_ID="<id de tu projecto>"
gcloud builds submit --tag gcr.io/$PROJECT_ID/devops-image:v0.1
  • As we can see in the following image, our container image was successfully generated in our Container Registry.
  • In the Cloud Build history, we will be able to see execution details.

Mounting the image on a VM instance

Now we will RUN this image on a GCP VM Instance.

  • We will go to the Compute Engine section in our GCP console.
  • We will click on "Create Instance".
  • We will assign it a name.
  • We will assign it the "E2" SKU
  • And we will associate a container with it.
  • In the "Container" section, we must click on "Deploy Container".
  • In this case, we must associate the URL of the container created in the previous step:
Plain text
gcr.io/$PROJECT_ID/devops-image:v0.1
  • We confirm that the configuration was applied:
  • We allow HTTP traffic. This is recommended only in testing environments for Testing purposes. It is not recommended to enable this traffic in Production unless it is necessary and evaluated using a risk matrix!
  • We verify the creation of the VM:
  • Upon opening the VM, we verify that our container is Running!

Continuous Integration with Cloud Build

Now comes the most interesting part. We will create a Continuous Integration pipeline in Cloud Build.

Cloud Build is a comprehensive platform for CI/CD processes that allows building and deploying. We can automate builds and Tests, integrate with source-code management tools such as GitHub, and use Docker and vulnerability scanning. In addition, it is a Serverless platform that scales automatically and supports multiple programming languages.

In this case, we will create a simple Trigger that will allow us to generate Continuous Integration from our source code in Source Repositories and our Container Registry.

  • First, we must go to Cloud Build in our GCP console.
  • Then, we must go to Triggers and click on "Create Trigger".
  • We will select our Source Repository in the Repository section.
  • For this case, it is very important to select .* as the branch.
    • .* is a regular expression (regex) pattern that matches any character (.) an unlimited number of times (*¨). When we use ".*" when selecting Branches, we are indicating that the Trigger will be activated for any Branch in the repo.
  • It is also very important in this case to use the "Dockerfile" configuration type.
  • For this case, it lets us build the image. However, in more complex pipelines, we will surely prefer to use cloud build yaml files, which will give us great flexibility to specify our pipeline's execution details.
  • Complete the Dockerfile name and the image name.
  • At the end, we use $COMMIT_SHA to generate an image with the TAG of the code build's commit number. This is a good practice for providing traceability from our images to code commits.
  • We will see that the Trigger was created.

Testing CI in Cloud Build

Now we are going to test the Continuous Integration of our code.

  • First, we will open our GIT folder associated with our Source Repo using a Code Editor (in this case VSCODE).
  • Then we will modify our "Main.py" file. In this case, I replace "Hola Madsblog!" with "Hello Build Trigger." in the title of our Flask Web page.
  • We will push the modified code:
Bash
cd "C:/<folder-destino-Source-Repo>"
git add .
git commit -m "Testing Build Trigger"
git push origin master
  • We will be able to verify in Cloud Build History how our Build is Triggered by the code push to our repo.
  • When the Job finishes, we can verify the creation of our new image.
  • A new version associated with the "Commit HASH" of our push is generated.

It is important to note

The configured VM Instance does not have Continous Deployment configured. Therefore, when a new image is generated, our VM will not update the version.

Since our VM Instance does not perform Continous Deployment automatically, to update the version of the container running on our VM Instance, we must run the following command:

Bash
gcloud compute instances update-container --container-image=gcr.io/qwiklabs-gcp-00-56daee6e0ad4/devops-image:e226474aca353831581dd857a4b8c494108099b3 instance-1
  • Our instance will restart.
  • And we will be able to verify the newly deployed image.

Thanks for reading. The learning continues in the next article.