The Great Melting Pot of Devops
We live in a great time of automation when it comes to scaling and deploying services. However with all of this choice, too many cooks are ruining a simple recipe for deploy and scaling. Therefore today we will be going over my favorite way to deploy services using completely FREE technologies.
Pre-requisites
- NodeJS api that you would like to deploy
- Gitlab accout with a source controlled nodejs api
- Heroku account
What’s in our stack?
Heroku and Docker
Let’s start with our hosting service, Heroku. I find heroku easy to user and it leads the way in deploying applications with its buildpack system. The buildpack system observes the makeup of your project and then determines how to deploy it. Making things simple for the developer. This accompanied with a ton of autoscaling and pricing controls, Heroku is amazing for projects you just try to get up and running. You can create an app through Heroku’s CLI or through it’s menu when logged into your account. Today though, we will be utilizing technology outside of Heroku’s buildpack, it’s container service. Not just any container service, but Docker. Docker is used to bundle dependencies/configurations needed to run an application and allow it to run on any platform. To containerize our node application, we will need to put a DockerFile at the root of our NodeJS project. It should look like this:
FROM node:8 #app directory WORKDIR /usr/src/app #copying package dependencies COPY package*.json ./ RUN npm install --only=production COPY . . EXPOSE 80 CMD ["npm", "start"]
build: docker: web: Dockerfile worker: ./Dockerfile
Gitlab
Gitlabs commitment to easing devops work has been top notch and I use them often for home projects because of it. So how do we add automation to this project? Well we are going to need do two things first, add a file and add a secret variable to our repo. Follow this link and get your Heroku API key. Next we are going to log into our gitlab and enter the repository with our API. Next click the Settings->CI/CD menu, then expand the variables menu:
- Enter HEROKU_API_KEY as the name of the variable
- In the value section, paste the heroku api key you have.
- Save
Now the last part, automating the build and deploy with a gitlab script. Create a .gitlab-ci.yml file in the root of your project. Fill the contents with this:
deploy_to_heroku: image: ruby:2.3 stage: deploy script: - gem install dpl - dpl --provider=heroku --app=leads-staging --api-key=$HEROKU_API_KEY --strategy=git only: - master
Note that variable “$HEROKU_API_KEY“? Essentially the build will download the tools needed and then push it to your heroku space (provided by your key). Naturally, I made it to only deploy when in the master branch. When you do pushes/merges to the master branch, you can view the magic in gitlab’s CI/CD->pipelines menu.
Wrapping it up
The step into devops can be overwhelming with the amount of tools out there. However, I hope this article gives you a template on how you can do things. This same stack and method of deployment works for Spring boot, Play, etc. So don’t be afraid to change things up a bit and get building out there.
