-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdeploy.go
427 lines (356 loc) · 9.73 KB
/
deploy.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package deploy
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
"strings"
"time"
"github.com/github/hub/git"
hub "github.com/github/hub/github"
"github.com/google/go-github/v35/github"
"github.com/urfave/cli"
)
const (
Name = "deploy"
Usage = "A command for creating GitHub deployments"
)
const (
DefaultRef = "master"
DefaultTimeout = 20 * time.Second
)
var errTimeout = errors.New("Timed out waiting for build to start. Did you add a webhook to handle deployment events?")
func init() {
cli.AppHelpTemplate = `USAGE:
# Deploy the master branch of remind101/acme-inc to staging
{{.Name}} --env=staging --ref=master remind101/acme-inc
# Deploy HEAD of the current branch to staging
{{.Name}} --env=staging remind101/acme-inc
# Deploy the current GitHub repo to staging
{{.Name}} --env=staging
{{if .Flags}}
OPTIONS:
{{range .Flags}}{{.}}
{{end}}{{end}}
`
}
var ProtectedEnvironments = map[string]bool{
"production": true,
}
var flags = []cli.Flag{
cli.StringFlag{
Name: "ref, branch, commit, tag",
Value: "",
Usage: "The git ref to deploy. Can be a git commit, branch or tag.",
},
cli.StringFlag{
Name: "env, e",
Value: "",
Usage: "The environment to deploy to.",
},
cli.BoolFlag{
Name: "force, f",
Usage: "Ignore commit status checks.",
},
cli.BoolFlag{
Name: "detached, d",
Usage: "Don't wait for the deployment to complete.",
},
cli.BoolFlag{
Name: "quiet, q",
Usage: "Silence any output to STDOUT.",
},
cli.BoolFlag{
Name: "update, u",
Usage: "Update the binary",
},
}
// NewApp returns a new cli.App for the deploy command.
func NewApp() *cli.App {
app := cli.NewApp()
app.Version = Version
app.Name = Name
app.Usage = Usage
app.Flags = flags
app.Action = func(c *cli.Context) {
if c.Bool("update") {
updater := NewUpdater()
if err := updater.Update(); err != nil {
fmt.Printf("Error Updating deploy command: %s\n", err)
os.Exit(-1)
} else {
os.Exit(0)
}
}
if err := RunDeploy(c); err != nil {
msg := err.Error()
if err, ok := err.(*github.ErrorResponse); ok {
if strings.HasPrefix(err.Message, "Conflict: Commit status checks failed for") {
msg = "Commit status checks failed. You can bypass commit status checks with the --force flag."
} else if strings.HasPrefix(err.Message, "No ref found for") {
msg = fmt.Sprintf("%s. Did you push it to GitHub?", err.Message)
} else {
msg = err.Message
}
}
fmt.Printf("Error from github deployments: %s\n", msg)
os.Exit(-1)
}
}
return app
}
// RunDeploy performs a deploy.
func RunDeploy(c *cli.Context) error {
var w io.Writer
if c.Bool("quiet") {
w = ioutil.Discard
} else {
w = c.App.Writer
}
h, err := hub.CurrentConfig().PromptForHost("github.com")
if err != nil {
return err
}
client, err := newGitHubClient(h)
if err != nil {
return err
}
nwo, err := Repo(c.Args())
if err != nil {
return err
}
owner, repo, err := SplitRepo(nwo, os.Getenv("GITHUB_ORGANIZATION"))
if err != nil {
return fmt.Errorf("Invalid GitHub repo: %s", nwo)
}
if c.String("env") == "" {
return fmt.Errorf("--env flag is required")
}
env := AliasEnvironment(c.String("env"))
ref := Ref(c.String("ref"), git.Head)
err = displayNewCommits(owner, repo, ref, env, client)
if err != nil {
return err
}
r, err := newDeploymentRequest(c, ref, env)
if err != nil {
return err
}
fmt.Fprintf(w, "Deploying %s/%s@%s to %s...\n", owner, repo, *r.Ref, *r.Environment)
ctx := context.TODO()
d, _, err := client.Repositories.CreateDeployment(ctx, owner, repo, r)
if err != nil {
return err
}
if c.Bool("detached") {
return nil
}
started := make(chan *github.DeploymentStatus)
completed := make(chan *github.DeploymentStatus)
go func() {
started <- waitState(pendingStates, owner, repo, *d.ID, client)
}()
go func() {
completed <- waitState(completedStates, owner, repo, *d.ID, client)
}()
select {
case <-time.After(DefaultTimeout):
return errTimeout
case status := <-started:
var url string
if status.TargetURL != nil {
url = *status.TargetURL
}
fmt.Fprintf(w, "%s\n", url)
}
status := <-completed
if isFailed(*status.State) {
return errors.New("Failed to deploy")
}
return nil
}
func displayNewCommits(owner string, repo string, ref string, env string, client *github.Client) error {
opt := &github.DeploymentsListOptions{
Environment: env,
}
deployments, _, err := client.Repositories.ListDeployments(context.TODO(), owner, repo, opt)
if err != nil {
return err
}
if len(deployments) == 0 {
return nil
}
sha := *deployments[0].SHA
compare, _, err := client.Repositories.CompareCommits(context.TODO(), owner, repo, sha, ref)
if err != nil {
return err
}
if len(compare.Commits) == 0 {
return nil
}
fmt.Printf("%s\n\n", "Deploying the following commits:")
for _, commit := range compare.Commits {
message := *commit.Commit.Message
fmt.Printf("%-20s\t%s\n", *commit.Commit.Author.Name, strings.Split(message, "\n")[0])
}
fmt.Printf("\nSee entire diff here: https://github.com/%s/%s/compare/%s...%s\n\n", owner, repo, sha, ref)
return nil
}
var EnvironmentAliases = map[string]string{
"prod": "production",
"stage": "staging",
}
func AliasEnvironment(env string) string {
if a, ok := EnvironmentAliases[env]; ok {
return a
}
return env
}
func newDeploymentRequest(c *cli.Context, ref string, env string) (*github.DeploymentRequest, error) {
if ProtectedEnvironments[env] {
yes := askYN(fmt.Sprintf("Are you sure you want to deploy %s to %s?", ref, env))
if !yes {
return nil, fmt.Errorf("Deployment aborted.")
}
}
var contexts *[]string
if c.Bool("force") {
s := []string{}
contexts = &s
}
return &github.DeploymentRequest{
Ref: github.String(ref),
Task: github.String("deploy"),
AutoMerge: github.Bool(false),
Environment: github.String(env),
RequiredContexts: contexts,
Payload: map[string]interface{}{
"force": c.Bool("force"),
},
Description: github.String("remind101/deploy CLI-initiated deploy"),
}, nil
}
var (
pendingStates = []string{"pending"}
completedStates = []string{"success", "error", "failure"}
)
func isFailed(state string) bool {
return state == "error" || state == "failure"
}
// waitState waits for a deployment status that matches the given states, then
// sends on the returned channel.
func waitState(states []string, owner, repo string, deploymentID int64, c *github.Client) *github.DeploymentStatus {
for {
<-time.After(1 * time.Second)
statuses, _, err := c.Repositories.ListDeploymentStatuses(context.TODO(), owner, repo, deploymentID, nil)
if err != nil {
continue
}
status := firstStatus(states, statuses)
if status != nil {
return status
}
}
}
// firstStatus takes a slice of github.DeploymentStatus and returns the
// first status that matches the provided slice of states.
func firstStatus(states []string, statuses []*github.DeploymentStatus) *github.DeploymentStatus {
for _, ds := range statuses {
for _, s := range states {
if ds.State != nil && *ds.State == s {
return ds
}
}
}
return nil
}
// refRegex is a regular expression that matches a full git HEAD ref.
var refRegex = regexp.MustCompile(`^refs/heads/(.*)$`)
// Ref attempts to return the proper git ref to deploy. If a ref is provided,
// that will be returned. If not, it will fallback to calling headFunc. If an
// error is returned (not in a git repo), then it will fallback to DefaultRef.
func Ref(ref string, headFunc func() (string, error)) string {
if ref != "" {
return ref
}
ref, err := headFunc()
if err != nil {
// An error means that we're either not in a GitRepo or we're
// not on a branch. In this case, we just fallback to the
// DefaultRef.
return DefaultRef
}
// Convert `refs/heads/test-deploy` => `test-deploy`
return refRegex.ReplaceAllString(ref, "$1")
}
// Repo will determine the correct GitHub repo to deploy to, based on a set of
// arguments.
func Repo(arguments []string) (string, error) {
if len(arguments) != 0 {
return arguments[0], nil
}
remotes, err := hub.Remotes()
if err != nil {
return "", err
}
repo := GitHubRepo(remotes)
if repo == "" {
return repo, errors.New("no GitHub repo found in .git/config")
}
return repo, nil
}
// A regular expression that can convert a URL.Path into a GitHub repo name.
var remoteRegex = regexp.MustCompile(`^/(.*)\.git$`)
// GitHubRepo, given a list of git remotes, will determine what the GitHub repo
// is.
func GitHubRepo(remotes []hub.Remote) string {
// We only want to look at the `origin` remote.
remote := findRemote("origin", remotes)
if remote == nil {
return ""
}
// Remotes that are not pointed at a GitHub repo are not valid.
if remote.URL.Host != "github.com" {
return ""
}
// Convert `/remind101/acme-inc.git` => `remind101/acme-inc`.
return remoteRegex.ReplaceAllString(remote.URL.Path, "$1")
}
func findRemote(name string, remotes []hub.Remote) *hub.Remote {
for _, r := range remotes {
if r.Name == name {
return &r
}
}
return nil
}
var errInvalidRepo = errors.New("invalid repo")
// SplitRepo splits a repo string in the form remind101/acme-inc into it's owner
// and repo components.
func SplitRepo(nwo, defaultOrg string) (owner string, repo string, err error) {
parts := strings.Split(nwo, "/")
// If we were only given a repo name, and a default organization is set,
// we'll use the defaultOrg as the owner.
if len(parts) == 1 && defaultOrg != "" && parts[0] != "" {
owner = defaultOrg
repo = parts[0]
return
}
if len(parts) != 2 {
err = errInvalidRepo
return
}
owner = parts[0]
repo = parts[1]
return
}
func askYN(prompt string) bool {
r := bufio.NewReader(os.Stdin)
fmt.Printf("%s (y/N)\n", prompt)
a, _ := r.ReadString('\n')
return strings.ToUpper(a) == "Y\n"
}