@@ -9,6 +9,7 @@ import "time"
99import "errors"
1010import "bytes"
1111import "io"
12+ import "log"
1213import "github.com/google/go-github/github"
1314
1415func 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
108109func sleep (duration time.Duration ) {
109- fmt .Printf ("Sleeping %v\n " , duration )
110+ log .Printf ("Sleeping %v\n " , duration )
110111 time .Sleep (duration )
111112}
112113
113114func 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
122123func 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
132133func 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
142143func 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
152150func 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
178177func 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
188187func 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
202201func 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