Last active
February 2, 2024 08:08
-
-
Save mikeatlas/3f21d8d64b8c9217c7d3 to your computer and use it in GitHub Desktop.
Example Go GitLab CI setup (no root/sudo required)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See docs/examples | |
# http://doc.gitlab.com/ce/ci/quick_start/README.html | |
# http://doc.gitlab.com/ce/ci/yaml/README.html | |
# GitLab CI template for Go tests. Note this installs | |
# a new working copy of Go in a non-standard path such | |
# that sudo/root is not needed for the install stage. | |
# note that this particular install-environment stage | |
# is overly verbose in order to debug anything tricky | |
# or weird in your environment - feel free to trim it | |
# down as needed | |
stages: | |
- install-environment | |
- build | |
- test | |
install-go: | |
stage: install-environment | |
script: | |
- export CURRENT_BUILD_PATH=$(pwd) | |
- echo $PATH | |
- rm -rf $HOME/golang | |
- rm -rf $HOME/gopath | |
- mkdir -p $HOME/golang # for GOROOT (contains the Go binary & core packages) | |
- mkdir -p $HOME/gopath # for GOPATH (contains code and external packages) | |
- curl http://storage.googleapis.com/golang/go1.5.2.linux-amd64.tar.gz 2>/dev/null > go1.5.2.linux-amd64.tar.gz | |
- tar -C $HOME/golang -xzf go1.5.2.linux-amd64.tar.gz | |
- export GOROOT=$HOME/golang/go | |
- export GOPATH=$HOME/gopath | |
- export PATH=$PATH:$GOROOT/bin | |
- export PATH=$PATH:$GOPATH/bin | |
- (if [[ "$(go version)" == *"go version go1.5"* ]]; then echo "✓ Go binary installed!"; else echo "Go binary not installed"; exit -1; fi); | |
- go version | |
- echo $PATH | |
- go env | |
- which go | |
build-my-project: | |
stage: build | |
script: | |
- cd $CURRENT_BUILD_PATH | |
- go build | |
test-my-project: | |
stage: test | |
script: | |
- cd $CURRENT_BUILD_PATH | |
- go test |
Building on the answer from @pcarranza, I simplified the script a little. The fiddling around with the go path did not work for me. Also dep
does not download the vendor
directory any longer when the update
command is passed in, so I removed this as well:
image: golang:1.10
stages:
- test
- build
before_script:
- go get -u github.com/golang/dep/cmd/dep
- mkdir -p $GOPATH/src
- cd $GOPATH/src
- ln -s $CI_PROJECT_DIR
- cd $CI_PROJECT_NAME
- dep ensure
test:
stage: test
script:
- go test
build:
stage: build
script:
- go build
I'm using a similar setup, but I would really love to use caching to save the vendor folder between jobs. Has anybody achieved this? I run multiple jobs in my test stage (test, bench, coverage, etc.) and try to run a cache warmup stage at first.
How to use dep cache for gitlab ci.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is what I have for using go 1.8 and go dep. Dep requires to have a somewhat valid go path:
If the issue is installing go, I would recommend you to build an image in a different pipeline and push it to a registry and then just use it. Else, it feels like your ci build is getting much more complicated than necessary.