-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeployment.go
94 lines (87 loc) · 2.13 KB
/
deployment.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
package scalingo_deployer
import (
"context"
"fmt"
"io"
"log"
"os"
"time"
scalingo "github.com/Scalingo/go-scalingo/v6"
)
var ctx context.Context
func newScalingoClient(config Config) *scalingo.Client {
clientConfig := scalingo.ClientConfig{
APIEndpoint: config.ScalingoAPIEndpoint,
APIToken: config.ScalingoAPIToken,
}
client, err := scalingo.New(ctx, clientConfig)
if err != nil {
panic(err)
}
return client
}
func buildDeploymentOutput(config Config, client *scalingo.Client, deployment *scalingo.Deployment) string {
res, err := client.DeploymentLogs(ctx, deployment.Links.Output)
if err != nil {
panic(err)
}
body, err := io.ReadAll(res.Body)
if err != nil {
panic(err)
}
return fmt.Sprintf(
"Deployment of %s@%s to %s finished with %s\nLog Output:\n%s\n",
config.GitRef,
config.GithubOwnerRepo,
config.ScalingoApp,
deployment.Status,
string(body),
)
}
func waitToFinish(config Config, client *scalingo.Client, deploymentID string) bool {
var lastStatus scalingo.DeploymentStatus
for {
deployment, err := client.Deployment(ctx, config.ScalingoApp, deploymentID)
if err != nil {
panic(err)
}
if deployment.HasFailed() {
output := buildDeploymentOutput(config, client, deployment)
log.Print(output)
return false
} else if deployment.IsFinished() {
output := buildDeploymentOutput(config, client, deployment)
log.Print(output)
break
} else {
if deployment.Status != lastStatus {
log.Printf("Deployment is running in state %s\n", deployment.Status)
}
time.Sleep(time.Second)
}
lastStatus = deployment.Status
}
return true
}
func Start(config Config) {
ctx = context.Background()
log.Printf(
"Starting deployment of %s@%s to scalingo app %s\n",
config.GitRef,
config.GithubOwnerRepo,
config.ScalingoApp,
)
sourceURL := archiveDownloadURL(config)
client := newScalingoClient(config)
params := scalingo.DeploymentsCreateParams{
GitRef: &config.GitRef,
SourceURL: sourceURL,
}
deployment, err := client.DeploymentsCreate(ctx, config.ScalingoApp, ¶ms)
if err != nil {
panic(err)
}
if !waitToFinish(config, client, deployment.ID) {
os.Exit(1)
}
}