A simple CI flow to check and build a Node app using GitHub Actions.

Commands

Here are the typical commands which are configured and run for a Node app.

$ npm install
$ npm run lint
$ npm run build
$ npm test

These should be run locally first. Then add them to an automated pipeline so they can run in the cloud.

You can of course leave out a step if it does not make sense for your app, but here I assume they are all needed and in this order.

If you prefer using yarn you can use it here and in the next section.

Samples

These samples also use the versions of Node and Yarn which have already been set up for you in the GH Actions environment. These will probably be latest or close to latest versions.

If you want more control like a specific version of Node or a matrix of Node versionns, see the NPM recipes section which uses the setup-node action.

Standard NPM sample

Add to your repo locally or using GitHub UI. Use any filename but the directory is important as .github/workflows.

  • main.yml
      name: Node CI
    
      on: push
    
      jobs:
        build:
          runs-on: ubuntu-latest
    
          steps:
            - name: Checkout 🛎️
              uses: actions/checkout@master
    
            - name: Install 🔧
              run: npm install
    
            - name: Lint 🧐
              run: npm run lint
    
            - name: Build 🏗️
              run: npm run build
    
            - name: Test 🚨
              run: npm test
    

Standad Yarn sample

  • main.yml
      jobs:
        build:
          runs-on: ubuntu-latest
    
          steps:
            - name: Checkout 🛎️
              uses: actions/checkout@master
    
            - name: Install 🔧
              run: yarn install
    
            - name: Lint 🧐
              run: yarn lint
    
            - name: Build 🏗️
              run: yarn build
    
            - name: Test 🚨
              run: yarn test
    

The assumption here is that the tests will be run against compiled JS files, so test is after build. But you could reverse those for some projects.

NPX sample

If you want to run a CLI package without installing it, you can use npx like this:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout 🛎️
        uses: actions/checkout@master

      - name: Lint 🧐
        run: npx eslint .

      - name: Check for updates 🧐
        run: npx npm-check-updates

Actions used

Which actions are used in the workflow above

This workflow uses actions/checkout, a standard action for cloning the repo in a workflow run.

Note that no Node or Yarn action was used here - the default environment will set up Node.js and Yarn for you. Using an extension is useful if you want to lock down a certain Node.js version or run multiple versions in parallel example. See the NPM recipes for using a matrix.