This article goes over how to migrate from Travis CI to GitHub Actions for a Node.js project on GitHub.
Travis CI
Given the .travis.yml
:
# .travis.yml
language: node_js
node_js:
- stable
install:
- npm install
script:
- npm run lint
- npm run build
- npm test
after_success:
- cat coverage/lcov.info | npx coveralls
GitHub Actions
First make the directory .github/workflows/
:
mkdir -p .github/workflows/
Create .github/workflows/build.yml
:
touch .github/workflows/build.yml
Add the Node.js workflow:
# .github/workflows/build.yml
name: build
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14]
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm install
- name: Lint files
run: npm run lint
- name: Build bundle
run: npm run build
- name: Run tests
run: npm test
- name: Coveralls
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
The workflow is inspired by the template.
Workflow
Here’s a breakdown of what each YAML field does.
name
name
is the workflow name. It’s optional and you can name it whatever you like:
name: CI build
on
on
is the event that triggers the workflow. In our example, git push
triggers our workflow.
To add the pull_request
event:
on: [push, pull_request]
Which is the same thing as:
on:
push:
branches: [$default-branch]
pull_request:
branches: [$default-branch]
jobs
jobs
are the jobs that the workflow runs. In our example, we have a single job named build
:
jobs:
build:
# ...
runs-on
runs-on
is the environment that the workflow runs on. To specify an OS version:
runs-on: ubuntu-18.04
strategy.matrix
strategy.matrix
is the build matrix. In our example, we’re running a job for a single node version. To define jobs for multiple node versions:
strategy:
matrix:
node-version: [12.19.0, 14.x, 15]
steps
steps
are the tasks of a job. In our example, we’re using the GitHub Actions:
actions/checkout
checks out the repository and actions/setup-node
sets up the Node.js environment:
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
coverallsapp/github-action
sends the coverage report to Coveralls:
- name: Coveralls
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
steps.run
steps.run
runs the commands. The commands can be npm scripts from package.json
or other bash commands.
steps:
- run: npm run build --if-present
actions/setup-node@v2
installs npm
, npx
, and yarn
. The --if-present
option runs the script only if it exists.
env
env
sets the environment variables for the entire workflow or an individual step. See env
and jobs.<job_id>.steps.env
.
Although Travis CI sets default environment variables, we don’t have to set CI=true
for our job since actions/checkout@v2
does it for us:
jobs:
build:
env:
CI: true