Introduction to Deploying Node.js on Google Cloud Platform
Deploying applications efficiently and reliably is a critical aspect of modern software development. Leveraging cloud services such as Google Cloud Platform (GCP) can significantly streamline this process. This is especially true when combined with continuous integration and continuous deployment (CI/CD) practices. In this guide, we’ll show you how to configure Node js CICD with GCP. We’ll utilize Bitbucket Pipelines for CI/CD. Docker will be used for containerization.
Prerequisites for Node.js Deployment on GCP
Before we dive into the deployment steps of Node js CICD, ensure you have the following prerequisites:
- GCP Account: You need a Google Cloud Platform account. If you don’t have one, sign up at GCP.
- Bitbucket Account: Create a Bitbucket account if you don’t have one at Bitbucket.
- Node.js Application: Have a Node.js application ready for deployment.
- Docker: Install Docker on your local machine. Follow the instructions on the Docker website.
- Google Cloud SDK: Install the Google Cloud SDK by following the instructions at Google Cloud SDK.
Creating the bitbucket-pipelines.yml
File for Node.js Deployment
The bitbucket-pipelines.yml
file defines the CI/CD pipeline in Bitbucket. Here’s a sample configuration for building and deploying a Docker image of your Node.js application for Node js CICD:
image: google/cloud-sdk:latest
pipelines:
default:
- step:
script:
- echo "Starting CI/CD pipeline..."
- docker build -t gcr.io/$GCR_PROJECT_ID/my-nodejs-app .
- echo $GOOGLE_APPLICATION_CREDENTIALS | base64 --decode > $BITBUCKET_CLONE_DIR/credentials.json
- gcloud auth activate-service-account --key-file=$BITBUCKET_CLONE_DIR/credentials.json
- gcloud config set project $GCR_PROJECT_ID
- gcloud builds submit --tag gcr.io/$GCR_PROJECT_ID/my-nodejs-app
- gcloud run deploy $CLOUD_RUN_SERVICE_NAME --image gcr.io/$GCR_PROJECT_ID/my-nodejs-app --region $CLOUD_RUN_REGION --platform managed --allow-unauthenticated
This configuration uses the Google Cloud SDK Docker image to build the Docker image of your Node.js application, submit it to Google Container Registry, and deploy it to Cloud Run.
Adding Environment Variables to Bitbucket for GCP Deployment
Next, you need to add environment variables to your Bitbucket repository settings. These variables will store sensitive information and configurations for Node js CICD deployment:

- Navigate to your Bitbucket repository.
- Go to Repository settings > Pipelines > Environment variables.
- Add the following environment variables:
CLOUD_RUN_REGION
: Your Cloud Run region (e.g.,us-central1
).GCR_PROJECT_ID
: Your Google Cloud Project ID.GOOGLE_APPLICATION_CREDENTIALS
: Your service account credentials (in base64 encoded format).CLOUD_RUN_SERVICE_NAME
: Your desired Cloud Run service name.
Creating Service Accounts and Generating Keys in GCP
Service accounts are crucial for securely automating interactions with Google Cloud services. For this task, we need two service accounts. One is for the Bitbucket web hook, and the other is for the Cloud Run service. Here’s how to create these service accounts and configure their roles:


- Go to the GCP Console:
- Open the GCP Console.
- Navigate to Service Accounts:
- Go to IAM & Admin > Service accounts.
- Create Service Account for Bitbucket Web Hook:
- Click Create Service Account.
- Provide a name and description, e.g.,
bitbucket-webhook-service-account
. - Click Create.
- Assign the following roles:
- Storage Admin
- Cloud Run Admin
- Artifact Registry Writer
- Click Done.
- Create Service Account for Cloud Run Service:
- Click Create Service Account.
- Provide a name and description, e.g.,
cloud-run-service-account
. - Click Create.
- Assign the following roles:
- Cloud Run Admin
- Cloud SQL Client
- Click Done.
- Generate Keys:
- For each service account, select the account from the list.
- Click Keys > Add Key > Create New Key.
- Choose JSON and download the key file.
- Encode the Key File to Base64:
- Use the following command to encode the key file to base64:bashCopy code
base64 <key-file.json>
- Use the following command to encode the key file to base64:bashCopy code
- Add Encoded String to Environment Variables in Bitbucket:
- Copy the base64 encoded string.
- Add it to the
GOOGLE_APPLICATION_CREDENTIALS
environment variable in Bitbucket.

Follow these steps to create the necessary service accounts with the appropriate roles. Generate keys to securely automate the deployment process or Node js CICD pipeline.
Building a Docker Image for Node.js in Bitbucket
Using a multi-stage Dockerfile can help optimize your Docker images by reducing their size and improving build efficiency. This approach is particularly useful for Node.js applications. Here’s how to create a multi-stage Dockerfile for your Node.js project:
Multi-Stage Dockerfile
Create a Dockerfile
in your Node.js project directory:
# Stage 1: Build
FROM node:14 AS build
# Create and change to the app directory
WORKDIR /usr/src/app
# Copy application dependency manifests to the container image.
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy local code to the container image.
COPY . .
# Build the application (if needed)
# RUN npm run build
# Stage 2: Run
FROM node:14
# Create and change to the app directory
WORKDIR /usr/src/app
# Copy only the necessary files from the build stage
COPY --from=build /usr/src/app .
# Install only production dependencies
RUN npm install --only=production
# Run the web service on container startup.
CMD [ "node", "index.js" ]
This Dockerfile
sets up a build stage to install dependencies. It builds your application. Then, it creates a final image with only the necessary files and production dependencies.
For more details on creating multi-stage Dockerfiles, refer to this article that provides an in-depth guide.
Creating an SQL Service Instance and Database in GCP

For applications that require a database, you can use Google Cloud SQL. Here’s how to create an SQL service instance and database:
- Go to the GCP Console: GCP Console.
- Navigate to SQL.
- Click Create instance.
- Choose the desired database engine (e.g., MySQL).

- Configure the instance (ID, password, region, etc.) and click Create.

- Once the instance is created, click on it to manage it.
- Go to Databases and click Create database. Enter a name for your database.
Deploying Cloud Run Service from Docker Image
First, build your Docker image and push it to Google Container Registry. Then, you can deploy it to Cloud Run using the Google Cloud Console. Follow these steps:

- Navigate to Cloud Run:
- Go to the Google Cloud Console.
- Select your project.
- Navigate to Cloud Run from the left-hand menu under Compute.
- Click on “Service” in the “DEPLOY CONTAINER” Dropdown:
- Click Service to start creating a new Cloud Run service.
- Configure the Service:
- Service configuration:
- Name: Enter a name for your service, e.g.,
my-nodejs-service
. - Region: Choose the region where you want to deploy your service.
- Name: Enter a name for your service, e.g.,
- Service configuration:

- Container image:
- Location: Select Google Container Registry (GCR).
- Region: Choose the region where your Docker image is stored.
- Image: Enter the path to your Docker image in GCR, e.g.,
gcr.io/your-project-id/my-nodejs-app
.
- Advanced Settings:
- Connections:
- Add connections: Click Add connection to add a connection to a Cloud SQL instance.
- Cloud SQL instance: Select your Cloud SQL instance.
- Authenticator: Choose the authentication method (e.g., Service account).
- Database name: Enter the name of your database.
- Connection name: Use the socket path for connecting to the database.
- Connections:

- Revision autoscaling:
- Maximum requests per container: Configure the maximum number of requests that each container instance can handle.
- Container concurrency: Set the maximum number of concurrent requests that each container instance can process.
- CPU allocation: Adjust the CPU allocation based on your application’s resource needs.
- Memory allocation: Specify the amount of memory allocated to each container instance.
- Deploy: Click Deploy to deploy your service with the specified configuration.
- Access URL: Once deployed, you will receive an access URL for your Cloud Run service. Use this URL to access your deployed Node.js application.
Explanation of Revision Autoscaling
Cloud Run offers autoscaling capabilities to handle varying traffic loads efficiently. Here’s a brief explanation of the key autoscaling settings:
- Maximum requests per container: This specifies the maximum number of requests a single container instance can process. After reaching the limit, it is shut down and replaced.
- Container concurrency: Defines the maximum number of concurrent requests that each container instance can handle.
- CPU allocation: Adjusts the amount of CPU resources allocated to each container instance based on your application’s CPU requirements.
- Memory allocation: Specifies the amount of memory allocated to each container instance to ensure optimal performance.

By configuring these settings, you can ensure that your Node.js application deployed on Cloud Run scales seamlessly to handle increased traffic while optimizing resource usage.
Conclusion: Successfully Deploying Node.js on GCP
In this guide, we covered the entire process of deploying a Node.js application on Google Cloud Platform using Bitbucket Pipelines for CI/CD and Docker for containerization. We started by setting up the environment. We created necessary configuration files. Then we built and deployed Docker images. Finally, we connected the application to a MySQL database.
By following these steps, you can leverage the power of GCP and CI/CD. This will streamline your development and deployment processes. It ensures reliable and scalable application delivery. If you have any questions or feedback, feel free to leave a comment below. Happy coding!
Share the article with your friends
Thanks for every other wonderful article. Where else may just anybody get that type of info in such an ideal approach of writing? I’ve a presentation subsequent week, and I’m on the look for such information.
I am impressed with this site, real I am a fan.
What’s Taking place i am new to this, I stumbled upon this I have found It positively helpful and it has helped me out loads. I’m hoping to give a contribution & help other users like its helped me. Great job.
I do agree with all of the ideas you have offered for your post. They are very convincing and can definitely work. Nonetheless, the posts are very quick for newbies. May you please extend them a little from subsequent time? Thanks for the post.
I like this post, enjoyed this one appreciate it for putting up.
I like this post, enjoyed this one regards for posting. “We seldom attribute common sense except to those who agree with us.” by La Rochefoucauld.
Some really interesting information, well written and loosely user friendly.
Valuable info. Lucky me I found your website by accident, and I am shocked why this accident didn’t happened earlier! I bookmarked it.
Fantastic beat ! I wish to apprentice while you amend your website, how can i subscribe for a blog web site? The account aided me a acceptable deal. I had been tiny bit acquainted of this your broadcast offered bright clear idea
Pretty nice post. I just stumbled upon your weblog and wanted to say that I’ve truly enjoyed surfing around your blog posts. After all I’ll be subscribing to your rss feed and I hope you write again soon!
Definitely believe that which you said. Your favorite justification appeared to be on the web the simplest thing to be aware of. I say to you, I definitely get irked while people consider worries that they just do not know about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects , people can take a signal. Will probably be back to get more. Thanks
I do agree with all of the ideas you have presented in your post. They are very convincing and will definitely work. Still, the posts are too short for novices. Could you please extend them a bit from next time? Thanks for the post.
Hello there, I found your website via Google while searching for a related topic, your web site came up, it looks good. I have bookmarked it in my google bookmarks.
Excellent site you have here but I was curious if you knew of any forums that cover the same topics discussed in this article? I’d really love to be a part of group where I can get suggestions from other knowledgeable individuals that share the same interest. If you have any suggestions, please let me know. Thank you!
I am glad to be one of several visitors on this outstanding internet site (:, thanks for posting.
I believe this internet site has some very good info for everyone. “Only the little people pay taxes.” by Leona Helmsly.
I am really enjoying the theme/design of your site. Do you ever run into any web browser compatibility problems? A couple of my blog audience have complained about my site not working correctly in Explorer but looks great in Chrome. Do you have any ideas to help fix this problem?
Hey There. I found your blog using msn. This is an extremely well written article. I will make sure to bookmark it and return to read more of your useful information. Thanks for the post. I will certainly return.
Needed to write you this little word to say thank you the moment again for those gorgeous methods you have shared on this website. This has been simply generous of you to give extensively all a number of people could have made available as an e book in order to make some profit for their own end, most importantly considering the fact that you might well have tried it in the event you considered necessary. The strategies likewise worked as the easy way to fully grasp that the rest have the same fervor much like mine to find out more with respect to this problem. I’m sure there are a lot more pleasant occasions ahead for people who see your website.
Thanks, I have just been looking for info about this subject for ages and yours is the best I have found out till now. However, what in regards to the conclusion? Are you certain concerning the supply?
I’m still learning from you, but I’m trying to reach my goals. I absolutely liked reading all that is posted on your blog.Keep the information coming. I loved it!
I view something truly interesting about your weblog so I saved to my bookmarks.
I really appreciate this post. I?¦ve been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thanks again
I’ve been absent for some time, but now I remember why I used to love this web site. Thank you, I will try and check back more often. How frequently you update your website?
I am impressed with this web site, very I am a big fan .
What i don’t realize is in truth how you’re now not actually much more neatly-appreciated than you might be right now. You are so intelligent. You understand therefore considerably in terms of this topic, produced me individually believe it from so many varied angles. Its like women and men don’t seem to be involved except it’s something to accomplish with Girl gaga! Your own stuffs outstanding. All the time care for it up!
I’m curious to find out what blog platform you are utilizing? I’m having some minor security problems with my latest blog and I’d like to find something more risk-free. Do you have any suggestions?
An fascinating discussion is price comment. I believe that you need to write extra on this matter, it may not be a taboo subject however generally people are not sufficient to talk on such topics. To the next. Cheers
Have you ever considered creating an e-book or guest authoring on other websites? I have a blog based on the same topics you discuss and would love to have you share some stories/information. I know my subscribers would enjoy your work. If you are even remotely interested, feel free to send me an e-mail.
Lovely just what I was searching for.Thanks to the author for taking his time on this one.
Its like you read my mind! You appear to know so much about this, like you wrote the book in it or something. I think that you can do with a few pics to drive the message home a bit, but instead of that, this is magnificent blog. A great read. I will certainly be back.
great post.Ne’er knew this, thanks for letting me know.
I love it when people come together and share opinions, great blog, keep it up.
Whats up! I simply wish to give a huge thumbs up for the good information you have right here on this post. I will probably be coming again to your weblog for extra soon.
hello!,I love your writing so much! share we communicate more approximately your post on AOL? I need a specialist in this house to solve my problem. Maybe that is you! Taking a look ahead to look you.
I have been surfing online greater than three hours these days, but I by no means found any attention-grabbing article like yours. It’s pretty worth enough for me. Personally, if all site owners and bloggers made just right content as you did, the net can be much more helpful than ever before.
Lovely just what I was looking for.Thanks to the author for taking his clock time on this one.
After I originally commented I clicked the -Notify me when new feedback are added- checkbox and now every time a comment is added I get 4 emails with the identical comment. Is there any means you can take away me from that service? Thanks!
I conceive this site has very good indited articles posts.
I’m not that much of a internet reader to be honest but your sites really nice, keep it up! I’ll go ahead and bookmark your site to come back down the road. All the best
I’ve recently started a blog, the information you provide on this website has helped me greatly. Thank you for all of your time & work. “The only winner in the War of 1812 was Tchaikovsky” by Solomon Short.
I believe you have remarked some very interesting points, thankyou for the post.
You can definitely see your skills in the work you write. The world hopes for more passionate writers like you who aren’t afraid to say how they believe. Always follow your heart.
Hi , I do believe this is an excellent blog. I stumbled upon it on Yahoo , i will come back once again. Money and freedom is the best way to change, may you be rich and help other people.
I actually wanted to post a brief remark in order to thank you for those stunning instructions you are giving out here. My long internet look up has now been compensated with reliable concept to write about with my colleagues. I would state that that we visitors are undoubtedly lucky to live in a superb place with very many wonderful individuals with insightful points. I feel extremely privileged to have seen your webpages and look forward to really more cool moments reading here. Thanks a lot again for a lot of things.
I couldn’t resist commenting
great post, very informative. I wonder why the other experts of this sector do not notice this. You should continue your writing. I’m confident, you have a huge readers’ base already!
I will right away take hold of your rss feed as I can not to find your e-mail subscription hyperlink or newsletter service. Do you have any? Please let me know so that I may subscribe. Thanks.
Thank you for some other magnificent post. Where else could anyone get that type of info in such a perfect way of writing? I have a presentation next week, and I’m on the look for such info.
Regards for helping out, excellent information.
Good article and straight to the point. I am not sure if this is in fact the best place to ask but do you people have any ideea where to hire some professional writers? Thanks 🙂
It’s really a nice and helpful piece of information. I’m satisfied that you shared this helpful information with us. Please stay us up to date like this. Thank you for sharing.
I like this web blog very much, Its a really nice place to read and find info .
You are my inhalation, I have few blogs and often run out from to brand : (.
I have recently started a website, the information you provide on this web site has helped me greatly. Thanks for all of your time & work.
Very nice post and right to the point. I am not sure if this is actually the best place to ask but do you people have any thoughts on where to get some professional writers? Thank you 🙂
Good web site! I really love how it is simple on my eyes and the data are well written. I’m wondering how I might be notified whenever a new post has been made. I have subscribed to your RSS feed which must do the trick! Have a great day!
Great awesome issues here. I am very glad to look your article. Thank you so much and i am looking forward to touch you. Will you please drop me a mail?
he blog was how do i say it… relevant, finally something that helped me. Thanks
I am always searching online for ideas that can facilitate me. Thx!
Wow! Thank you! I continuously needed to write on my website something like that. Can I take a fragment of your post to my site?
With havin so much content and articles do you ever run into any issues of plagorism or copyright infringement? My site has a lot of unique content I’ve either created myself or outsourced but it appears a lot of it is popping it up all over the web without my authorization. Do you know any methods to help reduce content from being stolen? I’d truly appreciate it.
I will immediately grab your rss as I can’t to find your e-mail subscription hyperlink or newsletter service. Do you’ve any? Please let me understand so that I may subscribe. Thanks.
I really like your writing style, great info, appreciate it for posting :D. “If a cluttered desk is the sign of a cluttered mind, what is the significance of a clean desk” by Laurence J. Peter.
Once I originally commented I clicked the -Notify me when new feedback are added- checkbox and now each time a remark is added I get four emails with the identical comment. Is there any means you may take away me from that service? Thanks!
Super-Duper blog! I am loving it!! Will be back later to read some more. I am taking your feeds also.
Throughout this grand design of things you actually get a B+ with regard to effort and hard work. Where you actually lost us was first on the particulars. You know, they say, details make or break the argument.. And that couldn’t be more accurate right here. Having said that, let me tell you precisely what did do the job. The article (parts of it) is certainly quite engaging and this is possibly the reason why I am taking the effort to comment. I do not really make it a regular habit of doing that. 2nd, even though I can see a jumps in reason you come up with, I am not necessarily confident of how you seem to unite your details which inturn make the actual final result. For the moment I shall yield to your position but trust in the near future you link the dots better.
It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about! Thanks
Some really good info , Gladiola I noticed this. “Our pleasures were simple-they included survival.” by Dwight D Eisenhower.