Cloud Run with bitbucket pipeline

Geekette
4 min readApr 29, 2020

Hi, we will deploy today a simple code to cloud run using bitbucket pipelines

What is CloudRun ?

Cloud Run is a fully managed compute platform that automatically scales your stateless containers. Cloud Run is serverless: it abstracts away all infrastructure management, so you can focus on what matters most — building great applications. Run your containers in fully managed Cloud Run or on Anthos, which supports both Google Cloud and on-premises environments. Cloud Run is built upon an open standard, Knative, enabling the portability of your applications. https://cloud.google.com/run

What is Bitbucket Pipeline ?

Bitbucket Pipelines is an integrated CI/CD service, built into Bitbucket. It allows you to automatically build, test and even deploy your code, based on a configuration file in your repository. … The bitbucket-pipelines. yml file holds all the build configurations for your repository. https://confluence.atlassian.com

Enable bitbucket pipeline

Edit bitbucket pipeline file

image: node:10.13
pipelines:
default:
- step:
name: QA
caches:
- node
script:
- yarn
- yarn eslint:lint
branches:
master:
- step:
name: Deployment
image: google/cloud-sdk:latest
caches:
- docker
script:
- echo ${KEY_FILE_AUTH} | base64 --decode --ignore-garbage > /tmp/gcloud-api.json
- gcloud auth activate-service-account --key-file /tmp/gcloud-api.json - gcloud config set project PROJECT_ID - gcloud builds submit --tag eu.gcr.io/PROJECT_ID/SERVICE_NAME- gcloud beta run deploy SERVICE_NAME --image eu.gcr.io/PROJECT_ID/SERVICEN_AME --platform managed --region europe-west1 --allow-unauthenticated --set-env-vars=NODE_ENV='production'options:
docker: true

The first line, we will be working with node image because the code is written in NodeJS, we will then init the pipelines and with Default means that any push on a branch will invoke ESlint testing to see if our code is in “good shape”

inline 12, if we have any commit on the master branch we will call the image: google/cloud-SDK: latest which will give us access to gcp CLI command (gcloud)

echo ${KEY_FILE_AUTH} | base64 --decode --ignore-garbage > /tmp/gcloud-api.json

since we need to authenticate into our project so we can deploy to cloud run, we need to use a service account downloaded from our gcp project ( IAM --> Service Accounts ), because we cannot actually pass the service account as a file in clear (security ) so we put it as a bitbucket environment variable, we decode the file

base64 ServiceAccount.json -w0

we take the output of this command and add it into bitbucket variables

gcloud auth activate-service-account --key-file /tmp/gcloud-api.json

with this command, we login using ServiceAccount.json that we decoded before in /tmp directory

gcloud config set project PROJECT_ID
gcloud builds submit --tag eu.gcr.io/PROJECT_ID/SERVIC_ENAME

we set which project we are working on it, and then we just build our docker image and push it to the google container registry

PS: Dockerfile will depend on your application for me it is NodeJS so it is something like this

FROM node:10.13 AS next-buildLABEL maintainer=" geekette <mymail@org.fr>" \
app="nodejs"
EXPOSE 8080WORKDIR /usr/src/app# Copy application dependency manifests to the container image.COPY package.json .# Install production dependencies.RUN yarn# Copy local code to the container image.COPY . .RUN yarn build#MultistageFROM node:10.13WORKDIR /usr/src/appCOPY --from=next-build /usr/src/app/ .#Start the appCMD [ "yarn", "start" ]

Cloud run needs to run on 8080 but if your application start on another port i will show you next how to force port

gcloud beta run deploy  SERVICE_NAME --image eu.gcr.io/PROJECT_ID/SERVICE_NAME --platform managed --region europe-west1 --allow-unauthenticated --set-env-vars=NODE_ENV='production'

we will deploy the image that we build and push it to google registry, the platform will be fully managed, we specify the region and for environment variables NODE_ENV you can pass it with — set-env-vars flag, you can find in the URL below all the flags and explication https://cloud.google.com/sdk/gcloud/reference/beta/run/deploy

PS: if you get this error

Check if your app is running on 8080 port or force the port in the gcloud beta run deploy command with — port flag, if it is not the case sometimes a crash in the app due to code error can invoke this message

That is all.

Peace from Tunisia

--

--

Geekette

Manal lamine just a simple human ( you can call me geekette )