Using Lando with a CI Service

In this example we will spin up a Lando LEMP app and add a .travis.yml config file to demonstrate how to use Lando in conjunction with a Continuous Integration (CI) service. In practice this could be Travis CI, Circle CI, or any CI provider but this example will use Travis CI.

This will be a barebones example that does basic php linting.

Start with a Lando app

We will assume the user already has a Lando app rolling. For this example we will use the following:

# Start with a basic lemp stack
name: lando-and-ci
recipe: lemp

# Automatically run composer install on builds
services:
  appserver:
    run:
      - cd $LANDO_MOUNT && composer install

# Lets make a high level wrapper that runs various test commands
# Ideally you'd put more than just one thing in here
tooling:
  test:
    service: appserver
    cmd:
      - composer
      - test

Note that we've configure our lando app in two special ways.

  1. It will run composer install when it is being built
  2. It defines a wrapper lando test command that can aggregate lower levels test commands eg composer test

Define Some Tests Normally

In this example we are doing some basic phplinting. We can get the phplint tools via composer.

composer require overtrue/phplint --dev

Now you can also add a scripts key to you composer.json file like so:

# Start with a basic lemp stack
name: lando-and-ci
recipe: lemp

# Automatically run composer install on builds
services:
  appserver:
    run:
      - cd $LANDO_MOUNT && composer install

# Lets make a high level wrapper that runs various test commands
# Ideally you'd put more than just one thing in here
tooling:
  test:
    service: appserver
    cmd:
      - composer
      - test

This will allow you to lint your code by running composer test. Note that this requires you use a .phplint.yml. Here is an example .phplint.yml

path: ./
jobs: 10
extensions:
  - php
exclude:
  - vendor

And the full composer.json.

{
  "name": "lando/lando-and-ci",
  "description": "This is a basic example on how to run lando in a ci environment.",
  "license": "MIT",
  "require-dev": {
    "overtrue/phplint": "^0.2.4"
  },
  "scripts": {
    "test": [
      "./vendor/bin/phplint --configuration=./.phplint.yml --no-cache ./"
    ]
  }
}

You should now be able to run your tests

# `lando test` will only work if you've explicitly defined that in your `.lando.yml`
# As in this example
lando test || lando composer test

Lando-ify your .travis.yml

We will assume the user is knowledgable and comfortable about setting up a project on Travis. On a high level your travis file will want to do three things

  1. Install the linux version of lando
  2. Start your lando app
  3. Run your test command

Here is a .travis.yml file that accomplishes the above for our current example.

# Because lando is managing our things the `language` and `php` sections below here
# could presumably be anything eg `language: nodejs` and `node_js: '6'`
language: php
php:
  - '7.0'

# This part is required!!!
services:
  - docker

# Install all the lando magic and do a sanity check to verify its working
before_install:
  - sudo apt-get -y update
  - sudo apt-get -y install cgroup-bin curl
  - curl -fsSL -o /tmp/lando-latest.deb http://installer.kalabox.io/lando-latest-dev.deb
  - sudo dpkg -i /tmp/lando-latest.deb
  - lando version

# Because we've set appropriate build steps and test commands directly in `.lando.yml`
# This part becomes super easy
script:

  # Start lando in verbose mode
  # This will also perform our composer install task
  - lando start -- -v

  # Run relevant tests, also in verbose mode
  - lando test -- -v

This Lando download is bleeding edge

If you need a stable Lando release, it's best to peg your version to an official release.

That's great! More importantly though now each time we file a PR against the GitHub repo it will:

  • Spin up a Travis CI environment
  • Install Lando on Travis
  • Install composer dependencies
  • Run the phplint code check

If the CI checks pass you can merge with confidence!

Full Example

You can check out the full code for this example over here.

results matching ""

    No results matching ""