Skip to content

Instantly share code, notes, and snippets.

@npenkov
Created December 29, 2017 12:57
Show Gist options
  • Save npenkov/02ef7dee9137ee3431375a27f5b6961c to your computer and use it in GitHub Desktop.
Save npenkov/02ef7dee9137ee3431375a27f5b6961c to your computer and use it in GitHub Desktop.
gitlab-ci for Go projects using go dep.
# Replace "projectname" and "username"
image: golang:1.9
variables:
BIN_NAME: projectname
ARTIFACTS_DIR: artifacts
GO_PROJECT: gitlab.com/username/projectname
stages:
- build
- test
before_script:
- mkdir -p ${GOPATH}/src/${GO_PROJECT}
- mkdir -p ${CI_PROJECT_DIR}/${ARTIFACTS_DIR}
- go get -u github.com/golang/dep/cmd/dep
- cp -r ${CI_PROJECT_DIR}/* ${GOPATH}/src/${GO_PROJECT}/
- cd ${GOPATH}/src/${GO_PROJECT}
build-projectname:
stage: build
script:
- dep ensure
- go build -o ${CI_PROJECT_DIR}/${ARTIFACTS_DIR}/${BIN_NAME}
artifacts:
paths:
- ${ARTIFACTS_DIR}
test-projectname:
stage: test
script:
- dep ensure
- go test -v -cover ./...
@noituri
Copy link

noituri commented Nov 24, 2018

Thanks

@goosecoid
Copy link

goosecoid commented Dec 3, 2018

Thanks, works like a charm
I use glide instead of go dep and made some other project specfific tweaks...

So here's mine:

# awesome api CI config
image: golang:1.11

variables:
  BIN_NAME: awesome-api
  ARTIFACTS_DIR: artifacts
  GO_PROJECT: gitlab.com/blabla/awesome-api

stages:
  - build
  - test
  
before_script:
  - mkdir -p ${GOPATH}/src/${GO_PROJECT}
  - mkdir -p ${CI_PROJECT_DIR}/${ARTIFACTS_DIR}
  - go get -u github.com/Masterminds/glide
  - cp -r ${CI_PROJECT_DIR}/* ${GOPATH}/src/${GO_PROJECT}/
  - cd ${GOPATH}/src/${GO_PROJECT}
 
build-awesome-api:
  stage: build
  script:
    - glide install
    - go build -o ${CI_PROJECT_DIR}/${ARTIFACTS_DIR}/${BIN_NAME} cmd/awesome-server/main.go
  artifacts:
    paths:
      - ${ARTIFACTS_DIR}

test-awesome-api:
  stage: test
  script:
    - glide install
    - go test -v -cover ./...

@spuder
Copy link

spuder commented Mar 29, 2021

Gitlab also has 'autodevops' which is based on heroku buildpacks. The official gitlab example for go projects is here: https://gitlab.com/gitlab-org/project-templates/go-micro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment