Skip to content

Commit f89122e

Browse files
committed
add logic to wait for job completion
1 parent 8790c1f commit f89122e

File tree

5 files changed

+68
-141
lines changed

5 files changed

+68
-141
lines changed

.github/actions/amplify-preview/action.yaml

-138
This file was deleted.

tools/amplify-preview/action.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ inputs:
1111
github_token:
1212
required: true
1313
description: "Github token with permissions to read/write comments in pull request"
14+
wait:
15+
default: "false"
16+
description: "If Amplify deployment is pending/running state wait for it's completion"
1417
runs:
1518
using: composite
1619
steps:
@@ -30,6 +33,7 @@ runs:
3033
GIT_BRANCH_NAME: ${{ steps.extract_branch.outputs.branch }}
3134
CREATE_BRANCHES: ${{ inputs.create_branches }}
3235
GITHUB_TOKEN: ${{ inputs.github_token }}
36+
WAIT: ${{ inputs.wait }}
3337
shell: bash
3438
run: |
3539
pushd ./tools/amplify-preview/; go run ./; popd

tools/amplify-preview/amplify.go

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2024 Gravitational, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package main
218

319
import (
@@ -15,8 +31,13 @@ import (
1531
)
1632

1733
var (
18-
errBranchNotFound = errors.New("Branch not found")
19-
errNoJobForBranch = errors.New("Current branch has no jobs")
34+
errBranchNotFound = errors.New("Branch not found")
35+
errNoJobForBranch = errors.New("Current branch has no jobs")
36+
amplifyJobCompletedStatuses = map[types.JobStatus]struct{}{
37+
types.JobStatusFailed: {},
38+
types.JobStatusCancelled: {},
39+
types.JobStatusSucceed: {},
40+
}
2041
)
2142

2243
const (
@@ -186,6 +207,11 @@ func appIDFromBranchARN(branchArn string) (string, error) {
186207
return "", fmt.Errorf("Invalid branch ARN")
187208
}
188209

210+
func isAmplifyJobCompleted(job *types.JobSummary) bool {
211+
_, ok := amplifyJobCompletedStatuses[job.Status]
212+
return ok
213+
}
214+
189215
func (err aggregatedError) Error() error {
190216
if len(err.perAppErr) == 0 {
191217
return nil

tools/amplify-preview/github.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2024 Gravitational, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package main
218

319
import (

tools/amplify-preview/main.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"log/slog"
2323
"os"
2424
"strings"
25+
"time"
2526

2627
"github.com/alecthomas/kingpin/v2"
2728
"github.com/aws/aws-sdk-go-v2/aws"
@@ -38,6 +39,13 @@ var (
3839
gitBranchName = kingpin.Flag("git-branch-name", "Git branch name").Envar("GIT_BRANCH_NAME").Required().String()
3940
createBranches = kingpin.Flag("crate-branches",
4041
"Defines whether Amplify branches should be created if missing, or just lookup existing ones").Envar("CREATE_BRANCHES").Default("false").Bool()
42+
wait = kingpin.Flag("wait",
43+
"Wait for pending/running job to complete").Envar("wait").Default("false").Bool()
44+
)
45+
46+
const (
47+
jobWaitSleepTime = 30 * time.Second
48+
jobWaitTimeAttempts = 40
4149
)
4250

4351
func main() {
@@ -96,6 +104,17 @@ func main() {
96104
}
97105
}
98106

107+
for i := 0; *wait && !isAmplifyJobCompleted(job) && i < jobWaitTimeAttempts; i++ {
108+
job, err := amp.GetJob(ctx, branch, nil)
109+
if err != nil {
110+
logger.Error("failed to get amplify job", logKeyBranchName, *gitBranchName, "error", err)
111+
os.Exit(1)
112+
}
113+
114+
logger.Info("Job is not in a completed state yet. Sleeping...", logKeyBranchName, *gitBranchName, "job_status", job.Status, "job_id", job.JobId)
115+
time.Sleep(jobWaitSleepTime)
116+
}
117+
99118
if err := postPreviewURL(ctx, amplifyJobToMarkdown(job, branch)); err != nil {
100119
logger.Error("failed to post preview URL", "error", err)
101120
os.Exit(1)
@@ -104,7 +123,7 @@ func main() {
104123
logger.Info("Successfully posted PR comment")
105124

106125
if job.Status == types.JobStatusFailed {
107-
logger.Error("amplify job is in failed state", "job_status", job.Status, "job_id", job.JobId)
126+
logger.Error("amplify job is in failed state", logKeyBranchName, *gitBranchName, "job_status", job.Status, "job_id", job.JobId)
108127
os.Exit(1)
109128
}
110129
}

0 commit comments

Comments
 (0)