Skip to content

Commit 3fbeb5e

Browse files
authored
Merge pull request #4 from mak-it/file-execution
Replaced Dokku integration with file execution
2 parents d443428 + db51c97 commit 3fbeb5e

3 files changed

Lines changed: 38 additions & 120 deletions

File tree

README.md

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
# Deployer
22

3-
Deploy [dokku](http://dokku.viewdocs.io/dokku/) applications using [GitHub Deployments API](https://developer.github.com/v3/repos/deployments/).
3+
Deploy applications using [GitHub Deployments API](https://developer.github.com/v3/repos/deployments/).
44

55
1. Listen/poll new GitHub deployments
66
2. Create pending GitHub deployment status and empty Gist
7-
3. Deploy application to dokku
7+
3. Execute command file that deploys application
88
4. Create success/failure Github deployment status and update Gist
99
5. Sleep 30 seconds
1010
6. Go to step #1
1111

12-
Make sure to run `deployer` as `dokku` user.
13-
14-
To register github deployments you can use [deploy](https://github.com/remind101/deploy),
12+
To register github deployments you can use curl,
1513
[slashdeploy](https://github.com/remind101/slashdeploy) or something else.
1614

1715
If you are using Slack, you can enable 'Deploy Events -> Show deployment statuses'
@@ -24,59 +22,34 @@ deployer -h
2422
```
2523

2624
```
27-
-app string
28-
Dokku application name (required)
2925
-env string
30-
Github deployment environment (required)
26+
Github deployment environment (required)
27+
-file string
28+
File to execute when new deployment is available (required)
3129
-org string
32-
GitHub org (required)
30+
GitHub org (required)
3331
-p string
34-
GitHub password (required)
32+
GitHub password (required)
3533
-repo string
36-
GitHub repo (required)
34+
GitHub repo (required)
3735
-sleep int
38-
Time to sleep between loops (defaults to 30 seconds) (default 30)
36+
Time to sleep between loops (defaults to 30 seconds) (default 30)
3937
-u string
40-
GitHub username (required)
41-
```
42-
43-
## Installation
44-
45-
Install `deployer` on dokku server:
46-
47-
```bash
48-
$ su - dokku
49-
$ curl -L -o deployer https://github.com/mak-it/deployer/releases/download/v0.1.2/deployer-linux-amd64
50-
$ chmod +x deployer
51-
```
52-
53-
Start `deployer` on system reboot using `cron` and `screen`:
54-
55-
```bash
56-
$ su - dokku
57-
$ echo "@reboot /usr/bin/screen -d -m /home/dokku/deployer -u ghuser -p ghpass -org mak-it -repo myapp -env demo -app myapp-demo -sleep 30" | crontab -
58-
```
59-
60-
Start `deployer` in `screen`:
61-
62-
```bash
63-
$ su - dokku
64-
$ /usr/bin/screen -d -m /home/dokku/deployer -u ghuser -p ghpass -org mak-it -repo myapp -env demo -app myapp-demo -sleep 30
38+
GitHub username (required)
6539
```
6640

6741
## Example
6842

6943
Trigger deployment from developer's machine:
7044

7145
```bash
72-
$ cd ~/src/myapp
7346
$ deploy --branch v49.3 --env demo
7447
```
7548

76-
On the dokku server:
49+
On the application server:
7750

7851
```bash
79-
$ deployer -u ghuser -p ghpass -org mak-it -repo myapp -env demo -app myapp-demo -sleep 30
52+
$ deployer -u ghuser -p ghpass -org mak-it -repo myapp -env demo -file bin/deploy-stub -sleep 30
8053
```
8154

8255
Output:
@@ -85,19 +58,20 @@ Output:
8558
Deployer
8659
ghuser ghpass mak-it myapp demo myapp-demo
8760
Sleep duration: 30s
88-
2017/08/24 13:31:43 Cloning repo
89-
2017/08/24 13:31:51 Adding repo dokku remote .myapp-demo myapp-demo
9061
2017/08/24 13:32:52 Getting deployment demo
9162
2017/08/24 13:32:53 Problem in getting deployment Deployment statuses already present
9263
2017/08/24 13:32:53 Sleeping 30s
9364
2017/08/24 13:33:23 Getting deployment demo
9465
2017/08/24 13:33:23 123 v49.3 demo
95-
2017/08/24 13:33:23 Fetchin repo .myapp-demo
9666
2017/08/24 13:33:24 Gist create ID: 123, Ref: v49.3, Environment: demo
9767
2017/08/24 13:33:25 Gist https://gist.github.com/...
9868
2017/08/24 13:33:25 Deployment status create pending
99-
2017/08/24 13:33:26 Deploying repo .myapp-demo
100-
Everything up-to-date
69+
2017/08/24 13:33:26 Executing File bin/deploy-stub
70+
-----> Deploying version: v49.3...
71+
-----> Adding BUILD_ENV to build environment...
72+
-----> Compiling Ruby/Rails
73+
=====> Application deployed:
74+
http://ruby-rails-sample.dokku.me
10175
2017/08/24 13:33:26 Deployment status create success
10276
2017/08/24 13:33:26 Gist update
10377
```

bin/deploy-stub

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/bash
22

3+
VERSION=$1
34

5+
echo $'remote: \e[1G-----> Deploying version: '$VERSION'...'
46
echo $'remote: \e[1G-----> Adding BUILD_ENV to build environment...'
57
echo $'remote: \e[1G-----> Compiling Ruby/Rails'
68
sleep 2

main.go

Lines changed: 17 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,26 @@ import "github.com/google/go-github/github"
1616
func main() {
1717
fmt.Println("Deployer")
1818

19-
username := flag.String("u", "", "GitHub username (required)")
20-
password := flag.String("p", "", "GitHub password (required)")
21-
org := flag.String("org", "", "GitHub org (required)")
22-
repo := flag.String("repo", "", "GitHub repo (required)")
23-
env := flag.String("env", "", "Github deployment environment (required)")
24-
app := flag.String("app", "", "Dokku application name (required)")
25-
sleepInt := flag.Int( "sleep", 30, "Time to sleep between loops (defaults to 30 seconds)")
19+
username := flag.String("u", "", "GitHub username (required)")
20+
password := flag.String("p", "", "GitHub password (required)")
21+
org := flag.String("org", "", "GitHub org (required)")
22+
repo := flag.String("repo", "", "GitHub repo (required)")
23+
env := flag.String("env", "", "Github deployment environment (required)")
24+
file := flag.String("file", "", "File to execute when new deployment is available (required)")
25+
sleepInt := flag.Int( "sleep", 30, "Time to sleep between loops (defaults to 30 seconds)")
2626

2727
flag.Parse()
2828

29-
if *app == "" {
29+
if *username == "" || *password == "" || *org == "" || *repo == "" || *env == "" || *file == "" {
3030
flag.PrintDefaults()
3131
os.Exit(1)
3232
}
3333

34-
fmt.Println(*username, *password, *org, *repo, *env, *app)
34+
fmt.Println(*username, *password, *org, *repo, *env, *file)
3535

3636
sleepDuration := time.Duration(*sleepInt) * time.Second
3737
fmt.Printf("Sleep duration: %v\n", sleepDuration)
3838

39-
repoPath := "." + *app;
40-
_, err := os.Stat(repoPath)
41-
if err != nil {
42-
repoURL := "https://" + *username + ":" + *password + "@github.com/" + *org + "/" + *repo + ".git"
43-
err = cloneRepo(repoURL, repoPath)
44-
if err != nil {
45-
log.Printf("Problem cloning repo %v\n", err)
46-
os.Exit(1)
47-
}
48-
}
49-
50-
addRepoDokkuRemote(repoPath, *app)
51-
5239
ctx := context.Background()
5340
tp := github.BasicAuthTransport{
5441
Username: *username,
@@ -68,13 +55,6 @@ func main() {
6855

6956
log.Println(*deployment.ID, *deployment.Ref, *deployment.Environment)
7057

71-
err = fetchRepo(repoPath)
72-
if err != nil {
73-
log.Printf("Problem in fetching repo %v\n", err)
74-
sleep(sleepDuration)
75-
continue
76-
}
77-
7858
gist, _, err := createDeploymentGist(ctx, client, deployment)
7959

8060
if err != nil {
@@ -88,12 +68,12 @@ func main() {
8868
createDeploymentStatus(ctx, client, deployment, *org, *repo, "pending", *gist.HTMLURL)
8969

9070
var output bytes.Buffer
91-
cmd := deployToDokku(repoPath, *deployment.Ref)
71+
cmd := executeFile(*file, *deployment.Ref)
9272
cmd.Stdout = io.MultiWriter(&output, os.Stdout)
9373
cmd.Stderr = cmd.Stdout
9474
err = cmd.Run()
9575
if err != nil {
96-
log.Printf("Problem in deploying to dokku %v\n", err)
76+
log.Printf("Problem with file execution %v\n", err)
9777
createDeploymentStatus(ctx, client, deployment, *org, *repo, "error", *gist.HTMLURL)
9878
updateDeploymentGist(ctx, client, gist, output.String())
9979
sleep(sleepDuration)
@@ -112,48 +92,10 @@ func sleep(duration time.Duration) {
11292
time.Sleep(duration)
11393
}
11494

115-
func cloneRepo(repoURL string, repoPath string) (error) {
116-
log.Println("Cloning repo ", repoURL)
117-
cmd := exec.Command("git", "clone", repoURL, repoPath)
118-
cmd.Stdout = os.Stdout
119-
cmd.Stderr = os.Stderr
120-
err := cmd.Run()
121-
return err
122-
}
123-
124-
func addRepoDokkuRemote(repoPath string, app string) (error) {
125-
log.Println("Adding repo dokku remote ", repoPath, app)
126-
cmd := exec.Command("git", "remote", "add", "dokku", "/home/dokku/" + app)
127-
cmd.Dir = repoPath
128-
cmd.Stdout = os.Stdout
129-
cmd.Stderr = os.Stderr
130-
err := cmd.Run()
131-
return err
132-
}
133-
134-
func fetchRepo(repoPath string) (error) {
135-
log.Println("Fetchin repo ", repoPath)
136-
cmd := exec.Command("git", "fetch", "origin", "--force", "--tags")
137-
cmd.Dir = repoPath
138-
cmd.Stdout = os.Stdout
139-
cmd.Stderr = os.Stderr
140-
err := cmd.Run()
141-
if err != nil {
142-
return err
143-
}
144-
cmd = exec.Command("git", "reset", "--hard", "origin/HEAD")
145-
cmd.Dir = repoPath
146-
cmd.Stdout = os.Stdout
147-
cmd.Stderr = os.Stderr
148-
err = cmd.Run()
149-
return err
150-
}
151-
152-
func deployToDokku(repoPath string, ref string) (*exec.Cmd) {
153-
log.Println("Deploying repo ", repoPath)
154-
cmd := exec.Command("git", "push", "-f", "dokku", ref + ":master")
155-
cmd.Dir = repoPath
156-
// cmd := exec.Command("bin/deploy-stub")
95+
func executeFile(filePath string, ref string) *exec.Cmd {
96+
log.Println("Executing File", filePath)
97+
cmd := exec.Command(filePath, ref)
98+
// cmd := exec.Command("bin/deploy-stub", ref)
15799
return cmd
158100
}
159101

@@ -173,12 +115,12 @@ func getDeployment(ctx context.Context, client *github.Client, org string, repo
173115
return nil, resp, err
174116
}
175117
deployment := deployments[0]
176-
118+
177119
statuses, resp, err := client.Repositories.ListDeploymentStatuses(ctx, org, repo, *deployment.ID, &github.ListOptions{ PerPage: 1 })
178120
if err != nil {
179121
return deployment, resp, err
180122
}
181-
123+
182124
if len(statuses) > 0 {
183125
err := errors.New("Deployment statuses already present")
184126
return deployment, resp, err

0 commit comments

Comments
 (0)