@@ -19,6 +19,7 @@ package main
19
19
import (
20
20
"context"
21
21
"errors"
22
+ "fmt"
22
23
"log/slog"
23
24
"os"
24
25
"strings"
@@ -52,85 +53,98 @@ func main() {
52
53
kingpin .Parse ()
53
54
ctx := context .Background ()
54
55
55
- cfg , err := config .LoadDefaultConfig (context . TODO () , config .WithRetryer (func () aws.Retryer {
56
+ cfg , err := config .LoadDefaultConfig (ctx , config .WithRetryer (func () aws.Retryer {
56
57
return retry .AddWithMaxAttempts (retry .NewStandard (), 10 )
57
58
}))
58
59
if err != nil {
59
60
logger .Error ("failed to load configuration" , "error" , err )
60
61
os .Exit (1 )
61
62
}
62
63
64
+ if amplifyAppIDs == nil || len (* amplifyAppIDs ) == 0 {
65
+ logger .Error ("expected one more more Amplify App IDs" )
66
+ os .Exit (1 )
67
+ }
68
+
63
69
amp := AmplifyPreview {
64
- client : amplify .NewFromConfig (cfg ),
65
- appIDs : func () []string {
66
- if len (* amplifyAppIDs ) == 1 {
67
- // kingpin env variables are separated by new lines, and there is no way to change the behavior
68
- // https://github.com/alecthomas/kingpin/issues/249
69
- return strings .Split ((* amplifyAppIDs )[0 ], "," )
70
- }
71
- return * amplifyAppIDs
72
- }(),
70
+ client : amplify .NewFromConfig (cfg ),
71
+ branchName : * gitBranchName ,
72
+ appIDs : * amplifyAppIDs ,
73
73
}
74
74
75
- // Check if Amplify branch is already connected to one of the Amplify Apps
76
- branch , err := amp .FindExistingBranch (ctx , * gitBranchName )
77
- if err != nil {
78
- if ! * createBranches && ! errors .Is (err , errNoJobForBranch ) {
79
- logger .Error ("failed to lookup branch" , logKeyBranchName , * gitBranchName , "error" , err )
80
- os .Exit (1 )
81
- }
75
+ if len (amp .appIDs ) == 1 {
76
+ // kingpin env variables are separated by new lines, and there is no way to change the behavior
77
+ // https://github.com/alecthomas/kingpin/issues/249
78
+ amp .appIDs = strings .Split (amp .appIDs [0 ], "," )
79
+ }
82
80
83
- // If branch wasn't found, and branch creation enabled - create new branch
84
- branch , err = amp .CreateBranch (ctx , * gitBranchName )
85
- if err != nil {
86
- logger .Error ("failed to create branch" , logKeyBranchName , * gitBranchName , "error" , err )
87
- os .Exit (1 )
88
- }
81
+ if err := execute (ctx , amp ); err != nil {
82
+ logger .Error ("execution failed" , "error" , err )
83
+ os .Exit (1 )
89
84
}
85
+ }
90
86
91
- // check if existing branch was/being deployed already
92
- latestJob , activeJob , err := amp . GetLatestAndActiveJobs (ctx , branch )
87
+ func execute ( ctx context. Context , amp AmplifyPreview ) error {
88
+ branch , err := ensureAmplifyBranch (ctx , amp )
93
89
if err != nil {
94
- if ! * createBranches && ! errors .Is (err , errNoJobForBranch ) {
95
- logger .Error ("failed to get amplify job" , logKeyBranchName , * gitBranchName , "error" , err )
96
- os .Exit (1 )
97
- }
90
+ return err
91
+ }
98
92
99
- // if job not found and branch was just created - start new job
100
- latestJob , err = amp .StartJob (ctx , branch )
101
- if err != nil {
102
- logger .Error ("failed to start amplify job" , logKeyBranchName , * gitBranchName , "error" , err )
103
- os .Exit (1 )
104
- }
93
+ currentJob , activeJob , err := ensureAmplifyDeployment (ctx , amp , branch )
94
+ if err != nil {
95
+ return err
105
96
}
106
97
107
- if err := postPreviewURL (ctx , amplifyJobsToMarkdown (branch , latestJob , activeJob )); err != nil {
108
- logger .Error ("failed to post preview URL" , "error" , err )
109
- os .Exit (1 )
98
+ if err := postPreviewURL (ctx , amplifyJobsToMarkdown (branch , currentJob , activeJob )); err != nil {
99
+ return fmt .Errorf ("failed to post preview URL: %w" , err )
110
100
}
101
+
111
102
logger .Info ("Successfully posted PR comment" )
112
103
113
104
if * wait {
114
- for i := 0 ; ! isAmplifyJobCompleted (latestJob ) && i < jobWaitTimeAttempts ; i ++ {
115
- latestJob , activeJob , err = amp .GetLatestAndActiveJobs (ctx , branch )
116
- if err != nil {
117
- logger .Error ("failed to get amplify job" , logKeyBranchName , * gitBranchName , "error" , err )
118
- os .Exit (1 )
119
- }
120
-
121
- logger .Info ("Job is not in a completed state yet. Sleeping..." ,
122
- logKeyBranchName , * gitBranchName , "job_status" , latestJob .Status , "job_id" , * latestJob .JobId , "attempts_left" , jobWaitTimeAttempts - i )
123
- time .Sleep (jobWaitSleepTime )
105
+ currentJob , activeJob , err = amp .WaitForJobCompletion (ctx , branch , currentJob )
106
+ if err != nil {
107
+ return fmt .Errorf ("failed to follow job status: %w" , err )
124
108
}
125
109
126
- if err := postPreviewURL ( ctx , amplifyJobsToMarkdown ( branch , latestJob , activeJob )); err != nil {
127
- logger . Error ( "failed to post preview URL" , "error" , err )
128
- os . Exit ( 1 )
110
+ // update comment when job is completed
111
+ if err := postPreviewURL ( ctx , amplifyJobsToMarkdown ( branch , currentJob , activeJob )); err != nil {
112
+ return fmt . Errorf ( "failed to post preview URL: %w" , err )
129
113
}
130
114
}
131
115
132
- if latestJob .Status == types .JobStatusFailed {
133
- logger .Error ("amplify job is in failed state" , logKeyBranchName , * gitBranchName , "job_status" , latestJob .Status , "job_id" , * latestJob .JobId )
134
- os .Exit (1 )
116
+ if currentJob .Status == types .JobStatusFailed {
117
+ logger .Error ("amplify job is in failed state" , logKeyBranchName , amp .branchName , "job_status" , currentJob .Status , "job_id" , * currentJob .JobId )
118
+ return fmt .Errorf ("amplify job is in %q state" , currentJob .Status )
119
+ }
120
+
121
+ return nil
122
+ }
123
+
124
+ // ensureAmplifyBranch checks if git branch is connected to amplify across multiple amplify apps
125
+ // if "create-branch" is enabled, then branch is created if not found, otherwise returns error
126
+ func ensureAmplifyBranch (ctx context.Context , amp AmplifyPreview ) (* types.Branch , error ) {
127
+ branch , err := amp .FindExistingBranch (ctx )
128
+ if err == nil {
129
+ return branch , nil
130
+ } else if errors .Is (err , errBranchNotFound ) && * createBranches {
131
+ return amp .CreateBranch (ctx )
132
+ } else {
133
+ return nil , fmt .Errorf ("failed to lookup branch %q: %w" , amp .branchName , err )
134
+ }
135
+ }
136
+
137
+ // ensureAmplifyDeployment lists deployments and checks for latest and active (the one that's currently live) deployments
138
+ // if this branch has no deployments yet and "create-branch" is enabled, then deployment will be kicked off
139
+ // this is because when new branch is created on Amplify and "AutoBuild" is enabled, it won't start the deployment until next commit
140
+ func ensureAmplifyDeployment (ctx context.Context , amp AmplifyPreview , branch * types.Branch ) (currentJob , activeJob * types.JobSummary , err error ) {
141
+ currentJob , activeJob , err = amp .GetLatestAndActiveJobs (ctx , branch )
142
+ if err == nil {
143
+ return currentJob , activeJob , nil
144
+ } else if errors .Is (err , errNoJobForBranch ) && * createBranches {
145
+ currentJob , err = amp .StartJob (ctx , branch )
146
+ return currentJob , activeJob , err
147
+ } else {
148
+ return nil , nil , fmt .Errorf ("failed to lookup amplify job for branch %q: %w" , amp .branchName , err )
135
149
}
136
150
}
0 commit comments