This is an example application written in Node.js for introducing users to basic features of Travis CI. It's based off of the self-driven demo, travis-ci/travis-intro-node.
We're going to learn how to create a project on Travis CI, add testing and deployment configurations, and then try out some new Travis CI features.
You'll need a working Node.js and npm setup to run local tests. You'll also want to clone the project.
You can start the app with:
$ node -e 'app = require("./lib/app"); app.start()'
...and then stop the app and run tests with:
$ npm install
$ ./node_modules/mocha/bin/mocha
Head over to travis-ci.com. If you don't yet have a Travis CI account, you would be asked to authorize Travis CI to access user data.
You can allow Travis CI to access all your repositories, or select few. For the purpose of this demo, we'll allow Travis CI to access only this repository. Click on the radio button "Only Select Repositories", and search for this repo. Once you find it, click on "Approve and Install".
The .travis.yml
is Travis CI's configuration file. It lives in
the root of your project directory structure, and it's what Travis CI uses to generate your automated build script. Travis CI builds in reasonable defaults, so you can start building this project with very little setup:
Add a Basic .travis.yml
:
- Create a
.travis.yml
file in the root of your project - Add the following to the new file:
language: node_js
node_js: '8'
Commit the .travis.yml
: It is time to trigger our first build! To do so, we need to push a new commit:
$ git commit -m "add travis ci"
$ git push origin master
yay! The build passes! 🎉
Test More Runtimes: Now that we have a basic travis.yml
configured, we can do some more testing. The Build Matrix feature lets quickly add testing against multiple language runtimes, other dependencies, or environment variables. To add one for multiple node runtimes, update the .travis.yml
to look like the following:
language: node_js
node_js:
- '8'
- '9'
- '10'
This is a great thing to do via a pull request, so we'll do the following:
$ git checkout -b add-more-nodejs
$ git add .travis.yml
$ git commit -m "add testing for node 9 and 10"
$ git push origin add-more-nodejs
Navigate to the repository, and open up a pull request. You'll see the Check Runs (CI Tests) happen, and once everything passes, it's time to merge the PR!
Now that we have a build that we're confident of, we're going to deploy it as a GitHub Release. Read up in the notes in this project for more info about GitHub releases. For the sakes of this demo, we'll start with a public_repo
scoped GitHub API token, and add it as an environment variable to Travis CI like so:
- Go to Travis CI → this project → 'Settings' → Environment Variables
- Add a new Environment Variable and name it
GITHUB_OAUTH_TOKEN
- Paste the token into the Value field
- Leave the "Display value in build log" in the "OFF" position.
- Click on the "Add" button
Or,
Using the cli, run:
$ travis encrypt <repo_token>
... and add the output to your .travis.yml
.
Next, we'll need to update our .travis.yml
to add a deploy step:
language: node_js
node_js:
- '8'
- '9'
- '10'
deploy:
provider: releases
api_key: $GITHUB_OAUTH_TOKEN
file: lib/app.js
on:
tags: true
and, we'll need to tag this for release:
$ git commit -m "Deploy to GitHub Releases"
$ git tag v0.0.1
$ git push origin --tags
Now that we have a working GitHub release, what if we expanded our testing configuration to cover some more environments?
Let's update our .travis.yml
to make use of the new Windows build environment, and use Build Stages to make this a bit more organized:
language: node_js
os:
- windows
- linux
node_js:
- '8'
- '9'
- '10'
jobs:
include:
- stage: "Deploy to GitHub Releases"
os: linux
node_js: '9'
deploy:
provider: releases
api_key: $GITHUB_OAUTH_TOKEN
file: lib/app.js
on:
tags: true
Let's create this as a pull request, so we can see what happens. Once it all looks good, we'd tag it for release like before.
Now that you're all setup on Travis CI, there's tons more you can do. Check out some of these resources for ideas on where to go next, and where you can find us on the interwebz: