Skip to content

Instantly share code, notes, and snippets.

@mikeatlas
Last active February 2, 2024 08:08
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save mikeatlas/3f21d8d64b8c9217c7d3 to your computer and use it in GitHub Desktop.
Save mikeatlas/3f21d8d64b8c9217c7d3 to your computer and use it in GitHub Desktop.
Example Go GitLab CI setup (no root/sudo required)
# 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
@kusold
Copy link

kusold commented May 26, 2016

This should really use https:// since hashes aren't being checked.

@Lorac
Copy link

Lorac commented Aug 10, 2016

Doesn't seems to work anymore. I get this error when starting the build-my-project step. The install-go goes well, but it starts a new container for the next stage.

$ echo $PATH
/usr/local/bundle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
$ which go

ERROR: Build failed: exit code 1

@odunboye
Copy link

the runners change when pipeline gets to the next stages causing failure for build and test (can not find go command), is there any fix to this?

@jcloutz
Copy link

jcloutz commented Jan 13, 2017

@Lorac @odunboye try this, I'm still trying to get deploy to work with it but it at least runs the tests.

https://gist.github.com/jcloutz/a9831d60aefb5e6ca29fd7848a230467

@pcarranza
Copy link

This is what I have for using go 1.8 and go dep. Dep requires to have a somewhat valid go path:

image: golang:1.8

stages:
  - test
  - build

before_script:
  - go get -u github.com/golang/dep/cmd/dep 
  - export GOPATH=$(dirname $CI_PROJECT_DIR)/go
  - mkdir -p $GOPATH/src
  - cd $GOPATH/src
  - ln -s $CI_PROJECT_DIR
  - cd $CI_PROJECT_NAME
  - dep ensure -update

test:
  stage: test
  script:
    - go test

build:
  stage: build
  script:
    - go build

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.

@Till--H
Copy link

Till--H commented Feb 20, 2018

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

@lukasmalkmus
Copy link

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.

@lcnja
Copy link

lcnja commented Jun 2, 2018

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