CI / CD for Node.js application with Github Action

Tram Ho

This article will cover the following:

  • Use Docker to launch an app
  • Use the GitHub Actions for continuous integration (CI) of your application
  • Use GitHub Actions for persistent deployment (CD) by pushing the Docker image into the Docker registry (Docker Hub)

Flow is like this

The full code will be in this

Use Docker to launch an app

Setting up docker for an application is pretty straightforward. What you need is a Dockerfile file and add the .dockerignore option

Here is an example

 

It will copy package.json, run npm install and start server. To make sure the file is correct you can run docker build -t abhinavdhasmana/github-action-example-node . from root. If you run docker images , you will see the lastest version image. You can also run the container with the command docker run -d -p 3000:3000 abhinavdhasmana/github-action-example-node . Browse to http://localhost:3000/ and a text will appear.

What is Github Action, and how does it work

‘GitHub Actions’ is an API that can react to any GitHub event, or our own. For example, when there is a push event to the repository, we want the unit test to be run

For Github Action to work, we need to create a .github/workflows folder and we will create workflows in this. Let’s create a push.yml file

This is what we will do in flow

  1. git clone the repo
  2. run npm install
  3. run npm lint
  4. run npm test
  5. build the docker image 6 login to docker hub
  6. Push the image to docker hub

The push.yml file should look like this

 

Explain a bit about the file

Line 1: The event they want to trigger, push

lines 3–6: We are defining a build-and-publish job to run on the latest ubuntu. Each job runs in a different session of the virtual environment. A job can contain one or more steps.

line 8: This is step 1 in our application. Here we will get the source code. You can also write your own script to pull code or reuse an open source code. https://github.com/actions/checkout

lines 9-12: This is step 2 in our workerflow, where we run npm install on our source code. Again, we use an open source action at https://github.com/actions/npm and pass the argument.

lines 13–20: This is a test run, convention test.

lines 21–24: We create a docker image for the source code with the help of docker cli and tag the image abhinavdhasmana / github-action-example-node

lines 25-29: This line is the login to the Docker hub. Here we will use the secrest keys passed as env variables for the biuld version. We can set these env variables in many ways. To set this up via GitHub, go to Settings-> Secrets and generate a new key

lines 30-33: We push the image to the docker hub with the tag we created in line 24.

If we push the code, and the comit changes, GitHub Actions is up and running and all steps in our workflow will start. We should see something like this

And we can go to DockerHub to see the image here

Full code is located at Github

Source: https://blog.bitsrc.io/

Share the news now

Source : Viblo