How to set up Travis CI to use linked (local) packages?

Ian Paschal

I use npm link myDependency within myPackage so that I can work on both at the same time (vs. publishing myDependency after every change and then updating myPackage to test it.)

I would like to be able to use Travis CI with myPackage but, as one could expect (I actually forgot, but it became quite obvious after attempting to build): running npm install on Travis won't manage to install the linked package.

So, what can I do? I saw someone suggest using a Docker container but that feels like a lot of infrastructure and I'm not experienced with Docker. Another option I thought of was adding a pre-install script to clone the dev branch of myDependency repo into the node_modules folder.

First though, I'm sure I'm not the first person who ever worked on two packages alongside each other, so there must be some consensus on how this should be done.

Ian Paschal

I solved this by replicating my development set-up on Travis.

The key to solving this involves giving Travis a way to access GitHub. To do this, log in to GitHub and go to the Personal access tokens page.

You’ll see a form with a field for the name of your token and what permissions you want to grant access to. Fill in a name such as "Travis CI Pull Repo" and select the "repo" section. None of the others are necessary, so no need to give access to them. At the bottom of the page is a green button "Generate token."

Back in your code editor, create a .travis.yml file in the root directory of your project if you don’t have one already. We will be using a RubyGem for the next step, so if you don’t have Ruby gems installed, you’ll need to download it. You can check if you have it installed by running gem -v in the terminal.

If you do, run the following in the terminal to install the Travis RubyGem:

gem install travis

Next, in the terminal, make sure you’re working in your project’s root directory, and use the Travis gem to add the access token to your .travis.yml file:

travis encrypt GH_TOKEN="token-from-github-goes-here" --add

If you were successful, your .travis.yml file should have a bunch of random text an encrypted token saved:

env:
  global:
    secure: "lots-of-seemingly-random-characters"

That’s it! Travis should now be able to pull (and push if that’s what you’re into) to your GitHub repository.

Obviously your .travis.yml file can vary greatly from mine, but at it’s most basic, I set up .travis.yml like this:

language: node_js
node_js:
- '6'
cache:
  directories:
    - node_modules
install:
- npm install
script:
- npm run lint
- npm run test
env:
  global:
    secure: "lots-of-seemingly-random-characters"

To add the cloning and linking of the dependency, add a before_install section with the following commands:

before_install:
- git config credential.helper "store --file=.git/credentials"
- echo "https://${GH_TOKEN}:@github.com" > .git/credentials
- cd ..
- git clone https://github.com/my-name/my-dependency.git my-dependency
- cd my-dependency
- npm install
- npm link
- cd ../my-main-project

What is this actually doing?

  1. We configure Git to use our saved access token.
  2. We go one directory up and clone the repository into a new folder with the same name as the repository.
  3. We go into the repository and install its dependencies. We create a global NPM link.
  4. Finally, we return to the main project (the one we are running Travis on). Note that this name must match the repository’s name on GitHub as that’s the name Travis will use.

In addition, we’ll need to actually use the link created above, so in the install section add the following line:

install:
- npm install
- npm link my-dependency

Make sure you put npm link after npm install as by default npm install will obliterate any links (a very annoying bug for those of us who use npm link).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to set up Travis CI with multiple languages

How to use different R versions to test packages with Travis-CI?

How to set up Travis CI and postgresql using custom db credentials?

Set up CI with Travis for .NET Core

How to use recent Boost versions with Travis CI

How to use environment variables in travis-ci

How do I cache global NPM packages on Travis CI?

How do I deploy nuget packages in Travis CI?

How to set up Python packages

Use ActiveMQ in Travis CI

How do I set up a partition to use for local files [18.04]

How do I set up the sources.list to select kernel packages from a local repository?

How to set the "api_key" value in Travis CI

How to set github token on Travis CI in a secure way?

Travis CI, update CMake using the packages cache

Travis CI for R packages: no deploy key found

How to set up gitlab CI runners?

How should I set up the python packages for this

How to set up a notification for "New in repository" packages?

How to use multiple Travis CI files inside a single repository?

How to Use Devicemapper Docker Filesystem Backends in Travis CI?

How to properly use curl in Travis-CI config file (YAML)?

How to use dotnet tool during Travis-CI build?

How can I make Travis CI use input() in python?

use velocity, meteor, and travis ci

How to cache packages which have been installed via `apt-get` in Travis CI?

setting up continuous deployment via travis ci for docusaurus - what github scopes to use?

Setting up postgresql in Travis CI for django project

Setting up travis ci with phoenix + heroku