/
build.go
46 lines (39 loc) · 1.13 KB
/
build.go
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package image
import (
"context"
"os"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/term"
)
// BuildImage builds a docker image
func BuildImage(dockerFilePath string, buildContextPath string, tag string) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(300)*time.Second)
defer cancel()
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
}
buildOpts := types.ImageBuildOptions{
Dockerfile: dockerFilePath,
Tags: []string{tag},
}
buildCtx, _ := archive.TarWithOptions(buildContextPath, &archive.TarOptions{})
resp, err := cli.ImageBuild(ctx, buildCtx, buildOpts)
if err != nil {
return err
}
defer resp.Body.Close()
termFd, isTerm := term.GetFdInfo(os.Stderr)
jsonmessage.DisplayJSONMessagesStream(resp.Body, os.Stderr, termFd, isTerm, nil)
imagePushOpts := types.ImagePushOptions{} // TODO add auth creds
pushResp, err := cli.ImagePush(ctx, tag, imagePushOpts)
if err != nil {
return err
}
defer pushResp.Close()
return nil
}