Deploying to Google Cloud Run with GitLab CI/CD: A Step-by-Step Guide
Oluwafemi Akinde
Lead Software Engineer | Cloud DevOps Enthusiast | Technical Author and Continuous Learner
Google Cloud Run is a powerful platform that allows developers to run stateless HTTP containers without worrying about the underlying infrastructure. With GitLab CI/CD, you can automate your build, test, and deployment process to Cloud Run, making it a perfect match for modern application development.
In this article, I will walk you through the process of setting up a GitLab CI/CD pipeline to deploy your code to Google Cloud Run.
NOTE: If you want to use Github Actions instead of Gitlab CI/CD, see my other article?here.
Let’s continue….
Prerequisites
Before we get started, make sure that you have the following:
Step 1: Create a Google Cloud Run Service
First, we need to create a Google Cloud Run service that will host our application. To do this, follow these steps:
Step 2: Authenticate the Google Cloud SDK
To deploy your code to Cloud Run, you need to authenticate the Google Cloud SDK on your local machine. To do this, follow these steps:
gcloud auth login
2. Follow the prompts to log in to your Google Cloud account.
领英推荐
Step 3: Create a GitLab CI/CD Pipeline
Now that we have our Cloud Run service set up and authenticated the Google Cloud SDK, we can create a GitLab CI/CD pipeline to automate our deployment process.
image: docker:latest
services:
- docker:dind
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
deploy:
image: google/cloud-sdk:latest
script:
- gcloud auth activate-service-account --key-file=google-creds.json
- gcloud config set project $PROJECT_ID
- gcloud builds submit --tag gcr.io/$PROJECT_ID/$CI_PROJECT_NAME:$CI_COMMIT_SHA
- gcloud run deploy --image=gcr.io/$PROJECT_ID/$CI_PROJECT_NAME:$CI_COMMIT_SHA --platform=managed --region=$CLOUD_RUN_REGION --allow-unauthenticated --update-env-vars=VAR1=value1,VAR2=value2 --quiet
only:
- master
3. Replace?$PROJECT_ID?with your Google Cloud project ID and?$CLOUD_RUN_REGION?with your preferred region.
4. Add any environment variables you need to the?--update-env-vars?flag.
5. Commit and push your changes to your GitLab repository.
Step 4: Configure GitLab CI/CD Variables
Finally, we need to configure some variables in GitLab CI/CD to authenticate our Google Cloud account and registry. To do this, follow these steps:
Conclusion
Congratulations! You now have a fully automated GitLab CI/CD pipeline that deploys your code to Google Cloud Run. With this setup, you can focus on writing code and let GitLab and Google Cloud handle the rest.
To see more of my articles, please follow me on Medium https://medium.com/@oluwafemiakinde.