How to Deploy a TimeVault Application on Cloud Run with Cloud Build on GCP
Published: 2023-05-23 07:00:32

[](url)**Timevault**

A deadman's switch to encrypt your vulnerability reports or other compromising data to be decryptable at a set time in the future. Uses tlock-js and is powered by drand. Messages encrypted with timevault are also compatible with the go tlock library.

**Cloudrun**
Cloud Run is a managed compute platform that lets you run containers directly on top of Google's scalable infrastructure. You can deploy code written in any programming language on Cloud Run if you can build a container image from it. In fact, building container images is optional.

**Structure of Timevault**

_**Step 1:**_

Create a repository as in GCP Cloud Source Repositories name `timevault-cloudrun`. And clone the repository to your local.

_**Step 2:**_
    Clone the timevault repository
```
 git clone https://github.com/r4jeshwar/timevault.git
 cd  timevault
```
_**Step 3:**_

Mirror the Github (timevault) repository to GCP cloud source repository

Go the GCP cloud source repository service and click `Add repository` and click `connect external repository` next click `continue`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wz6sugz1i1h8658e72p3.png)

Select your project ID and Select `Github` in Git provider, next choose your needed repository and click `connect selected repository`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/begprqseppf19iu8ien3.png)

Finally, Your Github repository will connect to your GCP cloud source repository


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g5saa57iragp4nj1am2p.png)


_**Step 4:**_
Using this Multi-stage Dockerfile we can able to deploy it on Cloudrun

```
FROM node:18-alpine AS build

WORKDIR /app

COPY package*.json ./
COPY ./src ./src

RUN npm i
RUN npm run build

FROM nginx:stable-alpine

COPY --from=build /app/dist/ /usr/share/nginx/html/
```

_**Step 5:**_
Write the cloudbuild.yaml to deploy it on Cloudrun. Here the `cloudbuild.yaml` file

```
 steps:
 # Build the container image
 - name: 'gcr.io/cloud-builders/docker'
   args: ['build', '-t', 'gcr.io/$_PROJECT_ID/$_IMAGE_NAME:$COMMIT_SHA', '.']
 # Push the container image to Container Registry
 - name: 'gcr.io/cloud-builders/docker'
   args: ['push', 'gcr.io/$_PROJECT_ID/$_IMAGE_NAME:$COMMIT_SHA']
 # Deploy container image to Cloud Run
 - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
   entrypoint: gcloud
   args:
   - 'run'
   - 'deploy'
   - '$_IMAGE_NAME'
   - '--platform=managed'
   - '--image'
   - 'gcr.io/$_PROJECT_ID/$_IMAGE_NAME:$COMMIT_SHA'
   - '--region'
   - '$_REGION'
   - '--allow-unauthenticated'
   - '--port'
   - '80'  
 images:
 - 'gcr.io/$_PROJECT_ID/$_IMAGE_NAME:$COMMIT_SHA'

 substitutions:
   _IMAGE_NAME: timevault
   _REGION: <YOUR_REGION>
   _PROJECT_ID: <YOUR_PROJECT_ID>
```

- `YOUR_REGION` is the region of the Cloud Run service you are deploying.
- `YOUR_PROJECT_ID` is your Google Cloud project ID where your image is stored.

_**Step 6:**_

Go to Cloud Build and create a trigger.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7mt8ndfv6oz5x88xze9z.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oatl3fsd96ws02ytljc2.png)

_**Step 7:**_

Trigger the Cloud Build. By below steps:
`Cloud Build` --> `Triggers` --> click `RUN` --> click `RUN TRIGGER`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5rktueuiibrrbjyeqwor.png)

Deployed on Cloud Run. Copy the Cloud Run URL and expose it on web browser

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n4j73tl7oasha9hgkd4a.png)

**Adding custom domain in Cloud run**

When you deploy a service to Cloud run, you are provided with a default  domain to access the service. However, you can use your own domain or subdomain instead of default one.

_**Step 1:**_
Go to Cloud run service, Click **MANAGE CUSTOM DOMAINS**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d550vqfvkgx9u7i9331t.png)

_**Step 2:**_

Go to the domain mapping, Click **ADD MAPPING**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vlkofi6gv4mxeyr5yua1.png)

_**Step 3:**_

In the add mapping form, select the service which you map to and select the verify the new domain, next enter your domain. As shown in the below. Click the **CONTINUE**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zunw5epp2h6r95yln4lj.png)

It will take some time to verify your domain, Click **REFRESH** to check verification is done.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sz9jyhuwi1d08x065aes.png)

After the verification is done, enter the subdomain to map to the service you have selected, then click **CONTINUE**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qpgg09zw6tm5ckbjc4ti.png)

You will get the DNS record for your custom domain that maps to your Cloud run service. You need to add this DNS record to your domain provider


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3hk1x0333odyognm5a0.png)

Finally, when you add the DNS record successfully, you can use your custom domain to access your deployed Cloud run service.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hahzk0o7nzyfnlxd6dew.png)