Running Travis CI with Laravel
Today I was working on setting up Travis CI for Cronhub builds. It took me around two hours to make it work. I was expecting the integration should be a breeze, but Travis CI configuration is not trivial. It has many moving pieces, and everything should be in place.
Since I finally got it working, I thought I’d share my travis.yml
and .env.travis
files to save you time.
travis.yml file
Make sure to use the bionic
or xenial
Ubuntu dist in your travis.yml file and add mysql
externally under the services. This will tell your build to install the latest stable version of MySQL. Otherwise, your migrations are going to fail due to an older version of MySQL.
dist: bioniclanguage: phpphp:
- 7.2
- 7.3services:
- mysqlcache:
directories:
- node_modules
- vendorbefore_script: - cp .env.travis .env - sudo mysql -e 'create database homestead;' - composer self-update - composer install --prefer-source --no-interaction --dev - php artisan key:generate - php artisan migratescript: - vendor/bin/phpunit
.env.travis file
Nothing special here just make sure you set DB_CONNECTION to testing
.
APP_NAME=Cronhub
APP_ENV=testing
APP_KEY=Sometring
APP_DEBUG=true
APP_URL=http://localhost/DB_CONNECTION=testing
DB_TEST_USERNAME=root
DB_TEST_PASSWORD=BCRYPT_ROUNDS=4
CACHE_DRIVER=array
MAIL_DRIVER=array
QUEUE_CONNECTION=sync
SESSION_DRIVER=arrayBUGSNAG_API_KEY=randkey
In your database.php
file you need to add a new connection as follows:
...'testing' => [ 'driver' => 'mysql', 'host' => env('DB_TEST_HOST', 'localhost'), 'database' => env('DB_TEST_DATABASE', 'homestead'), 'username' => env('DB_TEST_USERNAME', 'homestead'), 'password' => env('DB_TEST_PASSWORD', 'secret'), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false,],...
Once you do everything here, your build should start getting green. I hope this is useful and let me know if you have questions.