GitHub Actions – How To Create CI/CD Pipeline

Reading Time: 4 minutes
Git-Hub Actions

Hello Readers! In this blog, we are going to learn how we can Create CI/CD Pipeline for nodejs Applications using GitHub Actions.

Node.js Application

Node.js is an open-source, cross-platform runtime environment for developing server-side and network-side applications. These applications are written in JavaScript and can run in the Node.js runtime on OS X, Microsoft Windows, and Linux.

It also provides a rich library of various JavaScript modules, which greatly simplifies the development of web applications using it.

GitHub Actions

GitHub Actions is a tool/service that can be use to test, build and deploy your code by generating a CI/CD pipeline. It helps to automate your workflow just like other tools/services like Jenkins, Gitlab CI etc. GitHub Actions uses YAML as a language to write tasks/steps to perform on certain events.

This can be use for many things like deploying web services, building container apps, publishing packages to the registry, or automating the on boarding of new users to your open source projects.

The biggest feature of GitHub Actions for me is Matrix Builds, this means you can run your workflow concurrently test on multiple operating systems and versions of your runtime environment.

Create a new pipeline using GitHub Actions

We will see how to create a Hello World workflow on GitHub Actions

1. Choose a repository

Create a new GitHub repository. Select a new or an existing one. The code in the repository is irrelevant. In this demo, we don’t need code files in the repository.

How To Create CI/CD Pipeline Using GitHub Actions

2. Create a workflow in GitHub Actions

Click the “Actions” tab on your repository. Note that you can view the tab on any repository but you can only edit workflows if you have edit access to the repository.

How To Create CI/CD Pipeline Using GitHub Actions

If you do not already have a workflow (the configuration file used for the pipeline), you will be prompted to create one. Instead of choosing a template, choose the option to skip (skip this and set the workflow yourself). You can always go back and add another workflow later. B. Use Node.js or Python templates. You’ll see an editor view for editing a new file named .github / Workflows /main.yml. You can leave the path as it is.

How To Create CI/CD Pipeline Using GitHub Actions

This is the code given below for the YML file.

name: CI

on: push

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Run a one-line script
        run: echo "Hello, world!"

Lets explains what each of the lines does

name: CI – For specifying a name for the workflow

on: [push] – The on command is used to specify an event that will trigger the workflow, this event can be pushpull_request, etc.

jobs: – The job we want to run, in this case, we are setting up a build job.

runs-on: ubuntu-latest -The runs-on is specifying the OS you want your workflow to run on and we are using the latest version of ubuntu

Steps: Steps just indicate the various steps you want to run on that job.

The other steps just show how to run one or more commands in the shell. the default shell is bash.

3. View logs

We’ve added a new commit with that file, so your workflow’s “push” condition will be triggered and it will run against the current code immediately.

View the Actions tab and find the logs for a run for this workflow – in this case only one run.

The workflow should log that it is successfully checked out your repo and then ran one step to print a greeting. Then it will finish with a green check-mark.

How To Create CI/CD Pipeline Using GitHub Actions

The Final results would look like this after successful building of pipeline

How To Create CI/CD Pipeline Using GitHub Actions

GitHub Actions – How to run a Node.js app pipeline

Now we will create a basic node.js app pipeline using the following steps:-

1. Setup the files

If you already have a Node.js project then no need to go through with this step, otherwise you should copy this template link give click here or create a new file naming “package.json” and write the below code in it:-

{
  "name": "First CI app",
  "dependencies": {
    "lodash": "*"
  },
  "scripts": {
    "lint": "echo 'Linting!'",
    "build": "echo 'Running a build!'",
    "test": "echo 'Running unit tests!'",
  }
}

2. Setup workflow

We can edit the existing simple workflow file or add a new one under a different filename.

name: Node CI

on: push

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout 
        uses: actions/checkout@v2
        with:
          persist-credentials: false

      - name: Install 
        run: npm install

      - name: Lint 
        run: npm run lint

      - name: Build 
        run: npm run build

      - name: Test 
        run: npm test

It would look like this:-

3. View the logs in GitHub Action

Go to your Actions tab and click to drill down to your build logs.

After successful completion the final result would look like :-

Conclusion

We had learnt about the Git-Hub Action and how we can create a C/ICD Pipeline using it, in future we can use git-hub action for creating more pipelines.

References

Git-Hub Action

knoldus

Written by 

Deeksha Tripathi is a Software Consultant at Knoldus Inc Software. She has a keen interest toward learning new technologies. Her practice area is DevOps. When not working, she will be busy in listening music , and spending time with her family .