If you’d like to add commands to the dependencies phase of your go build, the easiest way to do this is to add them in the dependencies: post:
section of your circle.yml
. However, many users need to override the inferred go get
and go build
steps.
Overriding Go inference in the dependencies phase is known to break builds. If you’re familiar with CircleCI, you should know that your repo is cloned into ~/$CIRCLE_PROJECT_REPONAME
, which is not in the GOPATH
. This would normally mean that some go
commands executed from this directory will fail, since they must be executed from the GOPATH
. Our inference does some magic to make things work, but overriding inference will also override the magic.
In case you’re curious about the "magic, we do the following:
- Create a
$HOME/.go_project/src/github.com/$CIRCLE_PROJECT_USERNAME
directory. - Create a symlink in the newly created directory to the default checkout directory (
$HOME/$CIRCLE_REPO_NAME
). - Add the newly created symlink to the
GOPATH
. - Change the build_dir to the path of the newly created symlink so that subsequent commands are executed from this directory.
The good news is that you can (almost) replicate this magic yourself with the power of your circle.yml
.
The end result should look something like this:
dependencies:
override:
- mkdir -p $HOME/.go_project/src/github.com/$CIRCLE_PROJECT_USERNAME
- ln -fs $HOME/$CIRCLE_PROJECT_REPONAME $HOME/.go_project/src/github.com/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME
- |
echo 'export GOPATH=$GOPATH:$HOME/.go_project' >> ~/.circlerc
Note: Changing the build_dir
(or any circle.yml config) is not possible in the middle of a build, so subsequent commands that must be executed from the GOPATH
must be prefixed with cd ~/$HOME/.go_project/src/github.com/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME &&
. For example, go get
would look like this:
- cd $HOME/.go_project/src/github.com/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME && go get -t -d -v ./...
Happy Testing!