-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplease.go
More file actions
198 lines (162 loc) · 4.53 KB
/
please.go
File metadata and controls
198 lines (162 loc) · 4.53 KB
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
package command
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/google/go-github/github"
cli "gopkg.in/urfave/cli.v1"
)
const TaskName = "github-deploy"
func CmdPlease(c *cli.Context) (err error) {
deployScript := c.String("deploy-script")
if deployScript != "" {
return fmt.Errorf("--deploy-script is deprecated, use a positional argument instead")
}
if c.NArg() == 0 {
return fmt.Errorf("missing the deploy script as a positional argument")
}
// Compose the Git originl URL in the case of GitHub Actions
origin := c.GlobalString("git-origin")
if origin == "" && os.Getenv("GITHUB_SERVER_URL") != "" {
origin = fmt.Sprintf(
"%s/%s.git",
os.Getenv("GITHUB_SERVER_URL"),
os.Getenv("GITHUB_REPOSITORY"),
)
}
// Compose the log URL in the case of GitHub Actions
logURL := c.String("build-url")
if logURL == "" && os.Getenv("GITHUB_SERVER_URL") != "" {
logURL = fmt.Sprintf(
"%s/%s/actions/runs/%s",
os.Getenv("GITHUB_SERVER_URL"),
os.Getenv("GITHUB_REPOSITORY"),
os.Getenv("GITHUB_RUN_ID"),
)
}
var ref string
branch := c.GlobalString("git-branch")
commit := c.GlobalString("git-commit")
commitRef := c.GlobalBool("git-ref-commit")
if commitRef {
if commit == "" {
return errors.New("trying to use commit as ref but commit is not set")
}
ref = commit
} else {
if branch == "" {
return errors.New("trying to use branch as ref but branch is not set")
}
ref = branch
}
var pr int
prStr := c.String("pull-request")
if prStr != "" && prStr != "false" {
// prStr might be a URL, in that case pull the last component of the path
strs := strings.Split(prStr, "/")
prStr = strs[len(strs)-1]
pr, err = strconv.Atoi(prStr)
if err != nil {
return err
}
}
// Override the deployment target on pull-request
environment := c.String("environment")
if pr > 0 {
environment = fmt.Sprintf("pr-%d", pr)
}
ctx := context.Background()
ghCli := githubClient(ctx, c)
log.Println("deploy ref", ref)
log.Println("origin", origin)
// First, declare the new deployment to GitHub
// Look for an existing deployment in the same environment
owner, repo := githubSlug(c)
deployments, _, err := ghCli.Repositories.ListDeployments(ctx, owner, repo, &github.DeploymentsListOptions{
Ref: ref,
Task: TaskName,
Environment: environment,
})
if err != nil {
return err
}
var deployment *github.Deployment
if len(deployments) > 0 {
deployment = deployments[0]
} else {
deployment, _, err = ghCli.Repositories.CreateDeployment(ctx, owner, repo, &github.DeploymentRequest{
Ref: refString(ref),
Task: refString(TaskName),
AutoMerge: refBool(false),
RequiredContexts: refStringList([]string{}),
Payload: refString("{}"),
Environment: refString(environment),
Description: refString(TaskName),
TransientEnvironment: refBool(pr > 0),
ProductionEnvironment: refBool(pr == 0),
})
if err != nil {
return err
}
}
// Prepare deploy script
var stdout bytes.Buffer
cmd := exec.Command(c.Args().Get(0), c.Args()[1:]...) //#nosec
cmd.Stdout = io.MultiWriter(os.Stdout, &stdout)
cmd.Stderr = os.Stderr
environmentURL := c.String("environment-url")
updateStatus := func(state string, environmentURL string) error {
_, _, err := ghCli.Repositories.CreateDeploymentStatus(ctx, owner,
repo, *deployment.ID, &github.DeploymentStatusRequest{
State: refString(state),
LogURL: refString(logURL),
Description: refString(TaskName),
EnvironmentURL: refString(environmentURL),
// AutoInactive: refBool(true),
})
return err
}
// Start deploy script
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go propagateSignalsTo(cmd, signalChannel)
err = cmd.Start()
if err != nil {
err2 := updateStatus(StateError, "")
if err2 != nil {
log.Println("updateStatus:", err)
}
return err
}
// Record progress
err = updateStatus(StatePending, "")
if err != nil {
return err
}
// Wait on the deploy to finish
err = cmd.Wait()
signal.Stop(signalChannel)
if err != nil {
err2 := updateStatus(StateFailure, "")
if err2 != nil {
log.Println("updateStatus:", err)
}
return err
}
// Success!
out := strings.SplitN(stdout.String(), "\n", 2)
if environmentURL == "" {
environmentURL = strings.TrimSpace(out[0])
}
err = updateStatus(StateSuccess, environmentURL)
return err
}