Continuous Integration and Deployment (CI/CD Pipeline) with Github Package and Github Actions

Dale Nguyen - Oct 3 '19 - - Dev Community

From the previous post, I showed you how to create your first Github package. I hope that you had some fun. And in this article, you will learn how to add CI/CD pipeline to the process.

That means every time you push or merge to master, Github Actions will trigger the pipeline, run your test cases, and deploy your package to Github Package Registry automatically. From now, you can manage everything in your repo — is it interesting? :)

Before getting started, you need to add your personal access token to the environment.

Personal Access Token

The process is pretty easy. Once you have your package ready, what you need to do is to create a ci.yml file in your repo. Remember to change my username in the script.

// .github/workflows/ci.yml
name: Publish to GitHub Package Registry
on: [push]
jobs:
  build:
runs-on: ubuntu-latest
steps:
    - uses: actions/checkout@v1
    - name: Use Node.js 8.x
      uses: actions/setup-node@v1
      with:
        registry-url: https://npm.pkg.github.com/
        node-version: 8.x
        scope: '@dalenguyen'
    - name: npm install, build, and test
      run: |
        npm install
        npm run build --if-present
        npm test      
    - name: publish
      env:
        NODE_AUTH_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
      run: |
        npm run deploy

Enter fullscreen mode Exit fullscreen mode

If you want to trigger the build when merging to master, you can use this:

on:
  pull_request:
    branches:
      - master
Enter fullscreen mode Exit fullscreen mode

After you push or merge to master, you can check the build under the Actions tab

Actions build

After everything passed, my package was replaced by the updated version.

The script was contributed by honsq90 through #Hacktoberfest event.

. . . . . . . . . . . . . . . .