Skip to content

Commit 3cd4031

Browse files
committed
Added the Golang docker example
1 parent 8354266 commit 3cd4031

File tree

7 files changed

+124
-0
lines changed

7 files changed

+124
-0
lines changed

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM golang:cross
2+
3+
# The golang Docker sets the $GOPATH to be /go
4+
# https://github.com/docker-library/golang/blob/c1baf037d71331eb0b8d4c70cff4c29cf124c5e0/1.4/Dockerfile
5+
RUN mkdir -p /go/src/github.com/buildkite/golang-docker-example
6+
WORKDIR /go/src/github.com/buildkite/golang-docker-example

Readme.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Buildkite Golang Docker Example
2+
3+
This repository is an example on how to test a Golang project through Docker
4+
using Buildkite.
5+
6+
[Add this example to your Buildkite organization](https://buildkite.com/new)
7+
8+
## Using in your own build pipelines
9+
10+
We've wrapped up the `$GOPATH` wrangling required to get Golang projects to run
11+
into a `pre-command` hook which you can see here:
12+
https://github.com/buildkite/golang-example/blob/master/.buildkite/hooks/pre-command
13+
14+
To use in your own build pipelines:
15+
16+
1. Ensure `docker-compose` is installed on your build system. For details on how to do this, see: https://docs.docker.com/compose/install/
17+
18+
2. Use our `Dockerfile` and `docker-compose.yml` files as defaults:
19+
20+
```sh
21+
cd /your/golang/repo
22+
curl -o Dockerfile https://raw.githubusercontent.com/buildkite/golang-golang-example/master/Dockerfile
23+
curl -o docker-compose.yml https://raw.githubusercontent.com/buildkite/golang-golang-example/master/docker-compose.yml
24+
```
25+
26+
3. Replace `/go/src/github.com/buildkite/golang-docker-example` in the `Dockerfile` and
27+
`docker-compose.yml` files with your own Golang import path. For example,
28+
if your import path in Golang looks like this:
29+
30+
```go
31+
import (
32+
"github.com/keithpitt/project/sub-package"
33+
)
34+
```
35+
36+
Then you would replace `/go/src/github.com/buildkite/golang-docker-example`
37+
with `/go/src/github.com/keithpitt/project` (note the `sub-package` part of
38+
the import is not included). This path should also match the directory
39+
structure within the `$GOPATH` on your own development machine.
40+
41+
4. Add to your build pipeline and add the `BUILDKITE_DOCKER_COMPOSE_CONTAINER` env:
42+
43+
```yml
44+
steps:
45+
- command: "./scripts/test.sh"
46+
env:
47+
BUILDKITE_DOCKER_COMPOSE_CONTAINER: "app"
48+
```
49+
50+
## License
51+
52+
See [Licence.md](Licence.md) (MIT)

docker-compose.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
app:
2+
build: .
3+
volumes:
4+
- ./:/go/src/github.com/buildkite/golang-docker-example
5+
- /usr/bin/buildkite-agent:/usr/bin/buildkite-agent
6+
environment:
7+
- BUILDKITE_AGENT_ACCESS_TOKEN
8+
- BUILDKITE_JOB_ID
9+
- BUILDKITE_BUILD_ID
10+
- BUILDKITE_BUILD_NUMBER

logger/logger.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package logger
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func Print(text string) {
8+
fmt.Printf("%s", text)
9+
}

main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import (
4+
"github.com/buildkite/golang-docker-example/logger"
5+
)
6+
7+
func main() {
8+
logger.Print("This is the best Golang program, ever!\n")
9+
}

main_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
"github.com/stretchr/testify/assert"
6+
7+
"os"
8+
"io"
9+
"bytes"
10+
)
11+
12+
func TestMain(t *testing.T) {
13+
mainStdout := captureStdout(main)
14+
15+
assert.Equal(t, mainStdout, "This is the best Golang program, ever!\n")
16+
}
17+
18+
// Source https://gist.github.com/mindscratch/0faa78bd3c0005d080bf
19+
func captureStdout(f func()) string {
20+
old := os.Stdout
21+
r, w, _ := os.Pipe()
22+
os.Stdout = w
23+
24+
f()
25+
26+
w.Close()
27+
os.Stdout = old
28+
29+
var buf bytes.Buffer
30+
io.Copy(&buf, r)
31+
return buf.String()
32+
}

scripts/test.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
# Get build + test dependencies. -d also doesn't bother with installing the
4+
# packages, it just downloads them
5+
go get -t -d
6+
go test

0 commit comments

Comments
 (0)