Build a Go Project
For Language versions and other build-environment specific information visit our reference pages:
The rest of this guide covers configuring Go projects in Travis CI. If you’re new to Travis CI please read our Onboarding and General Build configuration guides first.
Specify a Go version #
You can use any tagged version of Go, a version with x
in place of the minor
or patch level to use the latest for a given major or minor version, or use
master
to get the latest version from source. All go version management is
handled by gimme.
language: go
go:
- 1.x
- "1.10"
- 1.11.x
- master
Go Modules #
Any value set for GO111MODULE
via .travis.yml
or repository settings is left
as-is. If absent, a default value of GO111MODULE=auto
is set.
Go Import Path #
The project source code will be placed in GOPATH/src/{repo-source}
, but if
vanity imports are
necessary (especially for internal
package
imports),
go_import_path:
may be specified at the top level of the config, e.g.:
go_import_path: example.org/pkg/foo
Dependency Management #
The default install
step of travis_install_go_dependencies <go-version>
[gobuild-args]
will behave differently depending on the Go version specified,
as well as the presence of certain environment variables and file paths.
If a Makefile is present by any of the following names, then no further actions
are taken in the install
step:
GNUMakefile
Makefile
BSDmakefile
makefile
In all other cases, the command go get ${gobuild_args} ./...
is run.
godep Support #
There is support included for godep when used
with vendored dependencies such that the GOPATH
will be prefixed with
${TRAVIS_BUILD_DIR}/Godeps/_workspace
and PATH
will be prefixed with
${TRAVIS_BUILD_DIR}/Godeps/_workspace/bin
. Additionally, if the
Godeps/_workspace/src
directory does not exist,godep
will be installed and
a godep restore
will be run.
It is important to note that using the older style Godeps.json
at the top
level is not supported.
All of the godep
integration steps are performed prior to the separate
go get
and makefile steps listed above.
Note that the godep
support is only activated if a custom install
step is
not specified.
Install Private Dependencies #
As go get
uses HTTPS to clone dependencies from GitHub rather than SSH, it
requires a different workaround from our recommended way of handling private
dependencies.
When cloning via HTTPS, git uses curl under the covers, which in turn allows you to specify a .netrc file, where you can store custom authentication credentials for specific domains, github.com for instance.
Go to your GitHub account and create a personal access token.
Make sure to give it the repo
scope, which allows accessing private
repositories.
To reduce the access rights of the token, you can also create a separate user account with access to only the repositories you need for a particular project.
Copy the token and store it in a .netrc in your repository, with the following data:
machine github.com
login <username>
password <token>
Add this to your repository and add the following steps to your .travis.yml:
before_install:
- cp .netrc ~/.netrc
- chmod 600 ~/.netrc
Default Build Script #
Go projects assume that either Make or Go build tool are used by default. In case there is a Makefile in the repository root, the default command Travis CI will use to run your project test suite is
make
In case there is no Makefile, it will be
go test ${gobuild_args} ./...
instead.
These default commands can be overridden as described in the general build
configuration guide. For example, to add the
-v
flag, override the script:
key in .travis.yml
like this:
script: go test -v ./...
The arguments passed to the default go test
command may be overridden by
specifying gobuild_args:
at the top level of the config, e.g.:
gobuild_args: -x -ldflags "-X main.VersionString v1.2.3"
which will result in the script step being:
go test -x -ldflags "-X main.VersionString v1.2.3" ./...
To build by running Scons without arguments, use this:
script: scons
Environment Variable #
The version of Go a job is using is available as:
TRAVIS_GO_VERSION
This may contain .x
at the end, as described above.
Use of this variable in the deployment condition should
take this possibility into consideration.
For example:
go:
- 1.7.x
⋮
deploy:
...
on:
condition: $TRAVIS_GO_VERSION =~ ^1\.7
Build Config Reference #
You can find more information on the build config format for Go in our Travis CI Build Config Reference.