Complex Build Commands

Complex Build Commands (1)

Sometimes if you have a complex build environment that is hard to configure in the .travis.yml, consider moving the steps into a separate shell script. The script can be a part of your repository and can easily be called from the .travis.yml.

The Use Case

Consider a scenario where you’re using Travis and you want to run more complex test scenarios, but only for builds that aren’t coming from pull requests. A shell script might look like this:

#!/bin/bash
set -ev
bundle exec rake:units
if [ "${TRAVIS_PULL_REQUEST}" = "false" ]; then
  bundle exec rake test:integration
fi

Note when you set the -ev at the top. The -e flag causes the script to exit as soon as one command returns a non-zero exit code. This can be handy if you want whatever script you have to exit early. It also helps in complex installation scripts where one failed command wouldn’t otherwise cause the installation to fail.

Unit Testing

Let’s say we have a Go file that just prints out Hello World, the file would look like this:

package main

import "fmt"

func Hello(hame string) string {
    return "Hello, " + name + "!"
}

func main() {
    fmt.Println(Hello("Chris"))
}

How do we test it? We’ll create a hello_test.go file in the same directory:

package main

import "testing"

func TestHello(t *testing.T) {
    got := Hello("Montana")
    want := "Hello, Montana!"

    if got != want {
        t.Errorf("got %q want %q", got, want)
    }
}

The travis.yml file should contain the minimum of a Go .travis.yml to test these files:

language: go
script: go test -v

Conclusion

You’ve now got an idea of Complex Build Commands now! I hope this blog post made complex build commands a little less complex.

As always, if you have any questions about Complex Build Commands, please email me at montana@travis-ci.org and I will assist you.

Happy building!