This repository is an example on how to test a Golang project using Buildkite.
Interested in using Docker instead? (which is way mega simpler than this) Check out https://github.com/buildkite/golang-docker-example
We've wrapped up the $GOPATH
wrangling required to get Golang projects to run
into a pre-command
hook which you can see here:
https://github.com/buildkite/golang-example/blob/master/.buildkite/hooks/pre-command
To use in your own build pipelines:
-
Copy the
pre-command
hook into your project:cd /your/golang/repo mkdir -p .buildkite/hooks curl -o .buildkite/hooks/pre-command https://raw.githubusercontent.com/buildkite/golang-example/master/.buildkite/hooks/pre-command chmod +x .buildkite/hooks/pre-command
-
Add
BUILDKITE_GOLANG_IMPORT_PATH
to your build steps. If your import path in Golang looks like this:import ( "github.com/keithpitt/project/sub-package" )
Then your
BUILDKITE_GOLANG_IMPORT_PATH
would begithub.com/keithpitt/project
(we don't include thesub-package
part of the import). This path should also match the directory structure within the$GOPATH
on your own development machine.You can add the
$BUILDKITE_BUILD_CHECKOUT_PATH
to your.pipeline.yml
file like this:steps: - command: "./scripts/test.sh" env: BUILDKITE_GOLANG_IMPORT_PATH: "github.com/buildkite/golang-example"
Testing Golang projects can be tricky due to how Golang handles it's $GOPATH
.
What our pre-command
hook does is create an entirely new $GOPATH
tree
within the current build directory (under tmp/go
), and symlink the current
build directory to the desired go import path location. So the new build
directory would look something like this:
$BUILDKITE_BUILD_CHECKOUT_PATH/tmp/go/src/github.com/buildkite/golang-example
The hook then changes the working directory to this new folder, so all of your build commands happen within the directory.
See Licence.md (MIT)