-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgitops_plugin_kustomize.go
86 lines (74 loc) · 2.15 KB
/
gitops_plugin_kustomize.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
package main
import (
"fmt"
"strings"
)
// GitOpsPluginKustomize is a gocat gitops plugin to prepare
// deployments using kustomize and gocat's builtin Git and GitHub support.
// This is used when you want to use gocat as a workflow engine
// with a chatops interface, while using kustomize along with
// the gocat native features as a deployment tool.
type GitOpsPluginKustomize struct {
github *GitHub
git *GitOperator
}
func NewGitOpsPluginKustomize(github *GitHub, git *GitOperator) GitOpsPlugin {
return &GitOpsPluginKustomize{github: github, git: git}
}
func (k GitOpsPluginKustomize) Prepare(pj DeployProject, phase string, branch string, assigner User, tag string) (o GitOpsPrepareOutput, err error) {
o.status = DeployStatusFail
if tag == "" {
ecr, err := CreateECRInstance()
if err != nil {
return o, err
}
tag, err = ecr.FindImageTagByRegexp(pj.ECRRegistryId(), pj.ECRRepository(), pj.ImageTagRegexp(), pj.TargetRegexp(), ImageTagVars{Branch: branch, Phase: phase})
if err != nil {
return o, err
}
}
ph := pj.FindPhase(phase)
currentTag, err := ph.Destination.GetCurrentRevision(GetCurrentRevisionInput{github: k.github})
if err != nil {
return
}
if tag == currentTag {
o.status = DeployStatusAlready
return
}
commits, err := k.github.CommitsBetween(GitHubCommitsBetweenInput{
Repository: pj.GitHubRepository(),
Branch: branch,
FirstCommitID: currentTag,
LastCommitID: tag,
})
if err != nil {
return
}
commitlog := "*Commit Log*\n"
for _, c := range commits {
m := strings.Replace(c.Message, "\n", " ", -1)
commitlog = commitlog + "- " + m + "\n"
}
prBranch, err := k.git.PushDockerImageTag(pj.ID, ph, tag, pj.DockerRepository())
if err != nil {
return
}
prID, prNum, err := k.github.CreatePullRequest(prBranch, fmt.Sprintf("Deploy %s %s", pj.ID, branch), commitlog)
if err != nil {
return
}
if assigner.GitHubNodeID != "" {
err = k.github.UpdatePullRequest(prID, assigner.GitHubNodeID)
if err != nil {
return
}
}
o = GitOpsPrepareOutput{
PullRequestID: prID,
PullRequestNumber: prNum,
Branch: prBranch,
status: DeployStatusSuccess,
}
return
}