/
app_image_helper.go
114 lines (93 loc) · 2.83 KB
/
app_image_helper.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
111
112
113
114
package bosun
import (
"fmt"
"github.com/fatih/color"
"github.com/naveego/bosun/pkg/command"
"github.com/naveego/bosun/pkg/slack"
"github.com/pkg/errors"
"regexp"
"strings"
)
func NewAppImageHelper(b *Bosun) AppImageHelper {
return AppImageHelper{Bosun: b}
}
type AppImageHelper struct {
Bosun *Bosun
}
type PublishImagesRequest struct {
App *App
Pattern string
}
func (x AppImageHelper) PublishImages(req PublishImagesRequest) error {
a := req.App
ctx := x.Bosun.NewContext()
var err error
var re *regexp.Regexp
if req.Pattern != "" {
re, err = regexp.Compile(req.Pattern)
if err != nil {
return err
}
}
var report []string
tags := []string{"latest", a.Version.String()}
branch := a.GetBranchName()
if a.Branching.IsFeature(branch) {
tag := "unstable-" + featureBranchTagRE.ReplaceAllString(strings.ToLower(branch.String()), "-")
ctx.Log().WithField("branch", branch).Warnf(`Publishing from feature branch, pushing "unstable" and %q tag only.`, tag)
tags = []string{"unstable", tag}
} else if a.Branching.IsDevelop(branch) {
tags = append(tags, "develop")
} else if a.Branching.IsMaster(branch) {
tags = append(tags, "master")
} else if a.Branching.IsRelease(branch) {
_, releaseVersion, err := a.Branching.GetReleaseNameAndVersion(branch)
if err == nil {
tags = append(tags, fmt.Sprintf("%s-%s", a.Version, releaseVersion))
}
} else {
if ctx.GetParameters().Force {
ctx.Log().WithField("branch", branch).Warnf(`Non-standard branch format, pushing "unstable" tag only.`)
tags = []string{"unstable"}
} else {
return errors.Errorf("branch %q matches no patterns; use --force flag to publish with 'unstable' tag anyway", branch)
}
}
for _, tag := range tags {
for _, taggedName := range a.GetTaggedImageNames(tag) {
if re != nil && !re.MatchString(taggedName) {
ctx.Log().Infof("Skipping %s because it didn't match pattern %q", taggedName, req.Pattern)
continue
}
ctx.Log().Infof("Tagging and pushing %q", taggedName)
untaggedName := strings.Split(taggedName, ":")[0]
_, err = command.NewShellExe("docker", "tag", untaggedName, taggedName).Sudo(ctx.GetParameters().Sudo).RunOutLog()
if err != nil {
return err
}
_, err = command.NewShellExe("docker", "push", taggedName).Sudo(ctx.GetParameters().Sudo).RunOutLog()
if err != nil {
return err
}
report = append(report, fmt.Sprintf("Tagged and pushed %s", taggedName))
}
}
fmt.Println()
for _, line := range report {
color.Green("%s\n", line)
}
g, _ := a.Repo.LocalRepo.Git()
changes, _ := g.ExecLines("log", "--pretty=oneline", "-n", "5", "--no-color")
slack.Notification{
IconEmoji: ":frame_with_picture:",
}.WithMessage(`Pushed images for %s from branch %s:
%s
Recent commits:
%s`,
a.Name,
branch,
strings.Join(report, "\n"),
strings.Join(changes, "\n"),
).Send()
return nil
}