-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker.go
110 lines (87 loc) · 1.63 KB
/
docker.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package zmage
import (
"strings"
"time"
)
func buildTag(image string) (string, error) {
if i := strings.Index(image, ":"); i != -1 {
return image[i+1:], nil
}
b, err := branch()
if err != nil {
return "", err
}
if b == "master" {
return "latest", nil
}
return b, nil
}
func buildArgs() ([]string, error) {
v, err := version()
if err != nil {
return nil, err
}
hash, err := commitHash()
if err != nil {
return nil, err
}
return []string{
"--build-arg", "VERSION=" + v,
"--build-arg", "GIT_COMMIT=" + hash,
}, nil
}
func BuildImage(image, dockerFile, ctxDir string) error {
tag, err := buildTag(image)
if err != nil {
return err
}
ba, err := buildArgs()
if err != nil {
return err
}
args := []string{
"build",
"-t", image + ":" + tag,
"-f", dockerFile,
}
args = append(args, ba...)
args = append(args, ctxDir)
return docker(args...)
}
func PushImage(image string) error {
bt, err := buildTag(image)
if err != nil {
return err
}
var tags []string
if bt == "latest" {
var v string
if v, err = version(); err != nil {
return err
}
if err = docker("push", image+":latest"); err != nil {
return err
}
tags = append(tags, v)
} else {
var ch string
if ch, err = commitHash(); err != nil {
return err
}
tags = append(tags, time.Now().Format("20060102-151405")+"-"+ch)
var b string
if b, err = branch(); err != nil {
return err
}
tags = append(tags, b)
}
for _, tag := range tags {
if err = docker("tag", image+":"+bt, image+":"+tag); err != nil {
return err
}
if err = docker("push", image+":"+tag); err != nil {
return err
}
}
return nil
}