When thinking about code storage, I was dead set on using Gitlab because of all the hype around Gitlab's do everything aura. As I began to engage with Gitlab and understand the available tools, I was less enamored with its abilities.
Github is the dominant tool in the code repository space, but after being warned of the Github hack back in 2020, Bitbucket seemed a more viable alternative. The CI/CD pipeline of Bitbucket is easier to use as well. Getting the code from Bitbucket into Cloud Run led to an unnecessarily long discovery period because it's actually relatively easy. Much of the documentation around the topic is more complicated than it has to be so I won't go through the pain I endured to get this process to work. Here, I will outline a straightforward method to set up a CI/CD pipeline between Bitbucket and the Google Cloud Platform (GCP).
First things first, you either need a project already created in GCP, or you will need to create a cloud project from the Google Cloud console.
The following steps assume that your code will be running inside a docker container. The CI/CD pipeline will extract code from Bitbucket, compile the code using Google's Cloud Build, then deploy the code in a container to Google's Cloud Run.
The Google Cloud Run tool is built on top of a hosted open-source application called K-Native, which runs on top of a service mesh hub by Istio, which runs on top of Kubernetes.
After project creation, go to Google Cloud Build service and enable the service. To enable the service, go to - Google Cloud Build > Settings. Enable the Cloud Run and Service Accounts service.
What does Cloud Build do? Cloud Build compiles Java, Go, Node.js code. Can deploy the compiled code across multiple environments such as VMs, serverless, Kubernetes, or Firebase. Performs a security scan and packages the code into a container or non-container artifact.
Once both services are enabled, click the "Connect Repository" button at the top of the screen of the Cloud Build service page. Go through steps to connect, authorize and connect the desired BitBucket repository.
After the repository is connected, click "Create Trigger" and follow the steps (some fields are prepopulated). Choose Dockerfile for the Build Configuration. (A configuration Dockerfile must be part of the source.)
After trigger creation, run the Trigger to see if it works. Running the Trigger should push a container to the Google Container Registry.
Click "History" to see a log of all Trigger runs—examining the Trigger from the log allows for viewing a Trigger's execution.
Now, if Cloud Run is not enabled, it must be enabled to continue to the next step.
From the Cloud Run service page, click "Create Service." When creating the service, select the image from the container registry created in the Trigger step above.
In setting up a Cloud Run service, choices are presented to require or not require authentication. If authentication is required, a key will have to be created from a service account and provided to the requestor.
Click the "Create" button.
After creation, there should be an endpoint URL generated. Click the generated URL to see if the application works. If the application works, then the CI/CD pipeline is almost setup. There is one more step to have the process fully automated.
At the top of the Cloud Run screen, there should be an option to complete the CI/CD pipeline. Click the button and ensure the prepopulated values are accurate. Once submitted, the CI/CD pipeline setup is complete. GCP creates an additional Trigger automatically deploying the code after the build process.
From this point, whenever changes are pushed to Bitbucket (depending on which branch your Trigger is watching) code will be built and deployed automatically to the URL defined by Cloud Run.