Using Travis-CI for your Laravel Nova packages

Published on – 2 minute read

Today Laravel announced that Laravel Nova can now be installed via composer. This works by providing your nova.laravel.com username and password as credentials for composer, as well as adding a custom Laravel Nova composer repository to your composer.json file:

"repositories": [
    {
        "type": "composer",
        "url": "https://nova.laravel.com"
    }
],

This is great news, as this does not only simplify updating Laravel Nova, but it also allows Nova tools/package developers to add continuous integration to their projects!

But there is still a problem: we do not want to provide our Laravel Nova credentials in our open source repository. But how can we solve this?

Travis-CI Encrypted Environment Variables

We can make use of Travis-CIs encrypted environment variables. These variables will store our Laravel Nova username and password and then we can use this information for our CI script. Don't be afraid: these encrypted envrionment variables will not be available for pull requests from other repositories.

The command line tool from Travis lets you create these encrypted env variables that we can use when running our tests. All we need to do is install the travis tool.

gem install travis

Once the tool is installed, we can create our encrypted variables. You need to run the following commands in the root of your Nova Tool repository:

travis encrypt NOVA_USERNAME="[your-nova-username]" --add
travis encrypt NOVA_PASSWORD="[your-nova-password]" --add

This will encrypt the environment variables, as well as adding them to your .travis.yml file.

Now all we need to do is modify our travis file, so that it uses these variables for the nova.laravel.com composer repository configuration:

language: php

php:
- 7.1
- 7.2

env:
  matrix:
  - COMPOSER_FLAGS="--prefer-lowest"
  - COMPOSER_FLAGS=""
  global:
  - secure: your-encrypted-variable-1=
  - secure: your-encrypted-variable-2=

install:
- echo "{\"http-basic\":{\"nova.laravel.com\":{\"username\":\"${NOVA_USERNAME}\",\"password\":\"${NOVA_PASSWORD}\"}}}" > auth.json

before_script:
- travis_retry composer self-update
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source

script:
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
- php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover

Now the next time you push code to your repository, travis will use the encrypted environment variables and install Laravel Nova for you. Happy testing!