Skip to content

Commit bdc77cd

Browse files
committed
Use log and build linux binary
1 parent 370affe commit bdc77cd

6 files changed

Lines changed: 43 additions & 45 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
/vendor
2-
/deployer
2+
/deployer*
33
/.*

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ dependencies:
55

66
build:
77
GOPATH=$$HOME go build
8+
9+
build-linux:
10+
GOPATH=$$HOME GOOS=linux GOARCH=amd64 go build -o deployer-linux-amd64

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
# Deployer
22

3-
###
3+
Deploy dokku application using GitHub Deployments
44

55
1. Listen to new GitHub deployments
66
2. Create pending GitHub deployment status and empty Gist
7-
3. Deploy application to dokku and update Gist
8-
4. Create success/failure Github deployment status
7+
3. Deploy application to dokku
8+
4. Create success/failure Github deployment status and update Gist
99
5. Sleep
1010
6. Go to step #1
1111

12+
Make sure to run `deployer` as `dokku` user.
13+
1214
## Usage
1315

1416
```bash

glide.lock

Lines changed: 2 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package: .
22
import:
3-
- package: github.com/denisenkom/go-mssqldb
4-
- package: github.com/lib/pq
53
- package: github.com/google/go-github
64
subpackages:
75
- github

main.go

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import "time"
99
import "errors"
1010
import "bytes"
1111
import "io"
12+
import "log"
1213
import "github.com/google/go-github/github"
1314

1415
func main() {
@@ -31,22 +32,22 @@ func main() {
3132

3233
fmt.Println(*username, *password, *org, *repo, *env, *app)
3334

35+
sleepDuration := time.Duration(*sleepInt) * time.Second
36+
fmt.Printf("Sleep duration: %v\n", sleepDuration)
37+
3438
repoPath := "." + *app;
3539
_, err := os.Stat(repoPath)
3640
if err != nil {
3741
repoURL := "https://" + *username + ":" + *password + "@github.com/" + *org + "/" + *repo + ".git"
3842
err = cloneRepo(repoURL, repoPath)
3943
if err != nil {
40-
fmt.Printf("Problem cloning repo %v\n", err)
44+
log.Printf("Problem cloning repo %v\n", err)
4145
os.Exit(1)
4246
}
4347
}
4448

4549
addRepoDokkuRemote(repoPath, *app)
4650

47-
sleepDuration := time.Duration(*sleepInt) * time.Second
48-
fmt.Printf("Sleep duration: %v\n", sleepDuration)
49-
5051
ctx := context.Background()
5152
tp := github.BasicAuthTransport{
5253
Username: *username,
@@ -59,29 +60,29 @@ func main() {
5960
deployment, _, err := getDeployment(ctx, client, *org, *repo, *env)
6061

6162
if err != nil {
62-
fmt.Printf("Problem in getting deployment %v\n", err)
63+
log.Printf("Problem in getting deployment %v\n", err)
6364
sleep(sleepDuration)
6465
continue
6566
}
6667

67-
fmt.Println(*deployment.ID, *deployment.Ref, *deployment.Environment)
68+
log.Println(*deployment.ID, *deployment.Ref, *deployment.Environment)
6869

6970
err = fetchRepo(repoPath)
7071
if err != nil {
71-
fmt.Printf("Problem in fetching repo %v\n", err)
72+
log.Printf("Problem in fetching repo %v\n", err)
7273
sleep(sleepDuration)
7374
continue
7475
}
7576

7677
gist, _, err := createDeploymentGist(ctx, client, deployment)
7778

7879
if err != nil {
79-
fmt.Printf("Problem in creating gist %v\n", err)
80+
log.Printf("Problem in creating gist %v\n", err)
8081
sleep(sleepDuration)
8182
continue
8283
}
8384

84-
fmt.Println(*gist.HTMLURL)
85+
log.Println("Gist", *gist.HTMLURL)
8586

8687
createDeploymentStatus(ctx, client, deployment, *org, *repo, "pending", *gist.HTMLURL)
8788

@@ -91,7 +92,7 @@ func main() {
9192
cmd.Stderr = cmd.Stdout
9293
err = cmd.Run()
9394
if err != nil {
94-
fmt.Printf("Problem in deploying to dokku %v\n", err)
95+
log.Printf("Problem in deploying to dokku %v\n", err)
9596
createDeploymentStatus(ctx, client, deployment, *org, *repo, "error", *gist.HTMLURL)
9697
updateDeploymentGist(ctx, client, gist, output.String())
9798
sleep(sleepDuration)
@@ -106,12 +107,12 @@ func main() {
106107
}
107108

108109
func sleep(duration time.Duration) {
109-
fmt.Printf("Sleeping %v\n", duration)
110+
log.Printf("Sleeping %v\n", duration)
110111
time.Sleep(duration)
111112
}
112113

113114
func cloneRepo(repoURL string, repoPath string) (error) {
114-
fmt.Println("Cloning repo ", repoURL)
115+
log.Println("Cloning repo ", repoURL)
115116
cmd := exec.Command("git", "clone", repoURL, repoPath)
116117
cmd.Stdout = os.Stdout
117118
cmd.Stderr = os.Stderr
@@ -120,8 +121,8 @@ func cloneRepo(repoURL string, repoPath string) (error) {
120121
}
121122

122123
func addRepoDokkuRemote(repoPath string, app string) (error) {
123-
fmt.Println("Adding repo dokku remote ", repoPath, app)
124-
cmd := exec.Command("git", "remote", "add", "dokku", "dokku@localhost:" + app)
124+
log.Println("Adding repo dokku remote ", repoPath, app)
125+
cmd := exec.Command("git", "remote", "add", "dokku", "/home/dokku/" + app)
125126
cmd.Dir = repoPath
126127
cmd.Stdout = os.Stdout
127128
cmd.Stderr = os.Stderr
@@ -130,7 +131,7 @@ func addRepoDokkuRemote(repoPath string, app string) (error) {
130131
}
131132

132133
func fetchRepo(repoPath string) (error) {
133-
fmt.Println("Fetchin repo ", repoPath)
134+
log.Println("Fetchin repo ", repoPath)
134135
cmd := exec.Command("git", "fetch", "origin", "--force", "--tags")
135136
cmd.Dir = repoPath
136137
cmd.Stdout = os.Stdout
@@ -140,16 +141,14 @@ func fetchRepo(repoPath string) (error) {
140141
}
141142

142143
func deployToDokku(repoPath string, ref string) (*exec.Cmd) {
143-
fmt.Println("Deploying repo ", repoPath)
144-
cmd := exec.Command("bin/deploy-stub")
145-
// cmd := exec.Command("git", "push", "-f", "dokku", ref + ":master")
146-
// cmd.Dir = repoPath
147-
// cmd.Stdout = os.Stdout
148-
// cmd.Stderr = os.Stderr
144+
log.Println("Deploying repo ", repoPath)
145+
cmd := exec.Command("git", "push", "-f", "dokku", ref + ":master")
146+
cmd.Dir = repoPath
149147
return cmd
150148
}
151149

152150
func getDeployment(ctx context.Context, client *github.Client, org string, repo string, env string) (*github.Deployment, *github.Response, error) {
151+
log.Println("Getting deployment ", env)
153152
opt := &github.DeploymentsListOptions{
154153
Environment: env,
155154
ListOptions: github.ListOptions{PerPage: 1},
@@ -162,21 +161,21 @@ func getDeployment(ctx context.Context, client *github.Client, org string, repo
162161
return deployment, resp, err
163162
}
164163

165-
// statuses, resp, err := client.Repositories.ListDeploymentStatuses(ctx, org, repo, *deployment.ID, &github.ListOptions{ PerPage: 1 })
166-
// if err != nil {
167-
// return deployment, resp, err
168-
// }
169-
//
170-
// if len(statuses) > 0 {
171-
// err := errors.New("Deployment statuses already present")
172-
// return deployment, resp, err
173-
// }
164+
statuses, resp, err := client.Repositories.ListDeploymentStatuses(ctx, org, repo, *deployment.ID, &github.ListOptions{ PerPage: 1 })
165+
if err != nil {
166+
return deployment, resp, err
167+
}
168+
169+
if len(statuses) > 0 {
170+
err := errors.New("Deployment statuses already present")
171+
return deployment, resp, err
172+
}
174173

175174
return deployment, resp, err
176175
}
177176

178177
func createDeploymentStatus(ctx context.Context, client *github.Client, deployment *github.Deployment, org string, repo string, state string, url string) (*github.DeploymentStatus, *github.Response, error) {
179-
fmt.Println("Deployment status create ", state)
178+
log.Println("Deployment status create ", state)
180179
input := &github.DeploymentStatusRequest{
181180
State: github.String(state),
182181
LogURL: github.String(url),
@@ -186,8 +185,8 @@ func createDeploymentStatus(ctx context.Context, client *github.Client, deployme
186185
}
187186

188187
func createDeploymentGist(ctx context.Context, client *github.Client, deployment *github.Deployment) (*github.Gist, *github.Response, error) {
189-
fmt.Println("Gist create")
190188
title := fmt.Sprintf("ID: %d, Ref: %s, Environment: %s", *deployment.ID, *deployment.Ref, *deployment.Environment)
189+
log.Println("Gist create", title)
191190
input := &github.Gist{
192191
Description: github.String(title),
193192
Public: github.Bool(false),
@@ -200,7 +199,7 @@ func createDeploymentGist(ctx context.Context, client *github.Client, deployment
200199
}
201200

202201
func updateDeploymentGist(ctx context.Context, client *github.Client, gist *github.Gist, content string) (*github.Gist, *github.Response, error) {
203-
fmt.Println("Gist update")
202+
log.Println("Gist update")
204203
file := gist.Files["output.txt"]
205204
file.Content = github.String(content)
206205
gist.Files["output.txt"] = file

0 commit comments

Comments
 (0)