Example Go Pipeline

This guide covers configuring continuous integration pipelines for Go projects that use Go Modules. If you’re new to Drone please read our Tutorial and build configuration guides first.

Build and Test

In the below example we demonstrate a pipeline that executes go test and go build commands. These commands are executed inside the golang Docker container, downloaded at runtime from DockerHub.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
kind: pipeline
type: kubernetes
name: default

steps:
- name: test
  image: golang
  commands:
  - go test
  - go build

Please note that you can use any Docker image in your pipeline from any Docker registry. You can use the official golang images, or your can bring your own.

Dependencies

If you decide to split your pipeline into multiple steps you need to make sure each step has access to project dependencies. Dependencies are downloaded to /go which is outside the shared workspace. Create a named volume to share this directory with all pipeline steps:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
kind: pipeline
type: kubernetes
name: default

steps:
- name: test
  image: golang
  volumes:
  - name: deps
    path: /go
  commands:
  - go test

- name: build
  image: golang
  volumes:
  - name: deps
    path: /go
  commands:
  - go build

volumes:
- name: deps
  temp: {}

Test Multiple Versions

You can use Drone’s multi-pipeline feature to concurrently test against multiple versions of Go. This is equivalent to matrix capabilities found in other continuous integration systems.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
---
kind: pipeline
type: kubernetes
name: go-1-11

steps:
- name: test
  image: golang:1.11
  commands:
  - go get
  - go test

---
kind: pipeline
type: kubernetes
name: go-1-10

steps:
- name: test
  image: golang:1.10
  commands:
  - go get
  - go test

...

If you find this syntax too verbose we recommend using jsonnet. If you are unfamiliar with jsonnet please read our guide.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
local Pipeline(name, image) = {
  kind: "pipeline",
  type: "kubernetes",
  name: name,
  steps: [
    {
      name: "test",
      image: image,
      commands: [
        "go get",
        "go test"
      ]
    }
  ]
};

[
  Pipeline("go-1-11", "golang:1.11"),
  Pipeline("go-1-12", "golang:1.12"),
]

Test Multiple Architectures

You can use Drone’s multi-pipeline feature to concurrently test your code on multiple architectures and operating systems.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
---
kind: pipeline
type: kubernetes
name: test-on-amd64

platform:
  arch: amd64

steps:
- name: test
  image: golang
  commands:
  - go get
  - go test

---
kind: pipeline
type: kubernetes
name: test-on-arm64

platform:
  arch: arm64

steps:
- name: test
  image: golang
  commands:
  - go get
  - go test

...

If you find this syntax too verbose we recommend using jsonnet. If you are unfamiliar with jsonnet please read our guide.