How to set-up Github Actions for Node.js project with Heroku?

This is the step-by-step process of setting up Github Actions to deploy a Node.js project on Heroku.

Naman Gupta
4 min readJul 23, 2020

--

Introduction

Here, we start by preparing a Node.js project to be deployed onto the Heroku. The Project will be prepared as an Open-Source Project and set up Github Actions such that it downloads the config file into the repository before being deployed onto Heroku.

The strategy we are going to follow is having a repository on Github with the complete code without the config file. We would upload the config file as a private gist on Github. Then we’ll set up a deploy branch, pushing code onto which will trigger the Github Action. The Action would pull the code onto the machine and add the config.js file and deploy it onto Heroku.

Node Project

My Node Project is pretty much a basic GET API application. The application retrieves the data saved in MongoDB and returns it as a response to a GET request. The catch lies in the .gitignore, the package.json and the config.js files.

The .gitignore file is the basic Node file that is provided while building a new repository on Github. Add config.js to the .gitignore file to ignore the config.js file while pushing the code. The final .gitignore may look like this:

# Configuration File
config.js
# Logs
logs
*.log
npm-debug.log*
...

Secondly, we’ll set up the scripts in the package.json file, that would be required to deploy the Node project onto Heroku.

"scripts": {
"start": "node app.js",
"server": "nodemon app.js",
"server_dev": "SET NODE_ENV=dev&& npm run server",
"server_prod": "SET NODE_ENV=production&& npm run server"
}

Finally, we’ll have our config.js file like so:

const nodeEnv = process.env.NODE_ENV;const config = {  
dev: {
<< Config Variables for Development >>
},
production: {
<< Config Variables for Development >>
}
};
export default config[nodeEnv];

Upload this file as a gist on Github and set it as a private gist.

This is all the changes that we need to do to have our Node.js project ready for deployment.

Github Actions

Now, we will start to create our Github Actions for the repo.

First, open the Actions tab in your repository:

Github Actions Tab

Click on set up a workflow yourself:

workflow editor

In the editor paste the following main.yml:

# This is a basic workflow to help you get started with Actions
name: CI
# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Remove gitignore file
run: |
rm .gitignore
ls -a
- name: Pull config.js
run: |
git clone https://gist.github.com/${{ secrets.CONFIG_URI_ID }}.git
mv ${{ secrets.CONFIG_URI_ID }}/config.js .
rm -rf ${{ secrets.CONFIG_URI_ID }}
ls -a
- name: Commit changes
run: |
git config --global user.name << Username >>
git config --global user.email << Email Id >>
git add .
git commit -m "Add Config file"
- uses: akhileshns/heroku-deploy@v3.3.6 # This is the action
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: << App Name >>
heroku_email: << Email Id >>

Commit these changes and move on to the next part.

Github Secrets

Now, we have to set the Secrets that are going to be used while running the Github Actions.

Open the Settings tab of your Repository. And then into the Secrets Section.

Github Secrets

Click on New Secret and add the following Secrets:

  1. CONFIG_URI_ID
  2. HEROKU_API_KEY (Look for resources to know how to get one)

Once, this is done re-run the jobs from the Actions tab and your App would be up and running.

This is the starting point of setting up a Github Actions. Github Actions is still a very versatile, flexible and powerful tool that may save you the hassle of doing repetitive tasks, that could be done in a matter of seconds by Github itself. So, do take a bit of time to explore this powerful tool and have fun with it.

Cheers!!

Resources

  1. Heroku API Token: link
  2. Github Actions for Node.js: link

--

--

Naman Gupta

SDE-2 at Navi Technologies, Web Developer and Self-Driving Cars Enthusiast