Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

amplify-preview: set amplify job info to github outputs #311

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tools/amplify-preview/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"github.com/gravitational/shared-workflows/libs/github"
)

var errGithubOutputNotAvailable = errors.New("GITHUB_OUTPUT is not available")

func postPreviewURL(ctx context.Context, commentBody string) error {
const prRefNameSuffix = "/merge"
refName := os.Getenv("GITHUB_REF_NAME")
Expand Down Expand Up @@ -69,3 +71,24 @@ func postPreviewURL(ctx context.Context, commentBody string) error {

return gh.UpdateComment(ctx, currentPR, comment.GetID(), commentBody)
}

func setGithubOutputs(kv map[string]string) error {
githubOutput := os.Getenv(github.OutputEnv)
if githubOutput == "" {
return errGithubOutputNotAvailable
}

file, err := os.OpenFile(githubOutput, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return fmt.Errorf("failed to open file %s: %w", githubOutput, err)
}
defer file.Close()

for key, value := range kv {
if _, err := fmt.Fprintf(file, "%s=%s\n", key, value); err != nil {
return fmt.Errorf("failed to write to file %s: %w", githubOutput, err)
}
}

return nil
}
26 changes: 26 additions & 0 deletions tools/amplify-preview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ func run(ctx context.Context) error {
return fmt.Errorf("failed to post preview URL: %w", err)
}

setAmplifyInfoToGithubOutputs(branch, currentJob)

slog.Info("Successfully posted PR comment")

if *wait {
Expand Down Expand Up @@ -148,6 +150,30 @@ func ensureAmplifyDeployment(ctx context.Context, amp AmplifyPreview, branch *ty
}
}

func setAmplifyInfoToGithubOutputs(branch *types.Branch, job *types.JobSummary) {
kv := make(map[string]string)

if branch.BranchName != nil {
kv["AMPLIFY_BRANCH"] = *branch.BranchName
}

if branch.BranchArn != nil {
if appId, err := appIDFromBranchARN(*branch.BranchArn); err != nil {
slog.Error("failed to extract app ID from branch ARN", "branch_arn", *branch.BranchArn, "error", err)
} else {
kv["AMPLIFY_APP_ID"] = appId
}
}

if job.JobId != nil {
kv["AMPLIFY_JOB_ID"] = *job.JobId
}

if err := setGithubOutputs(kv); err != nil {
slog.Error("failed to set Amplify info to GitHub outputs", "error", err)
}
}

func handleInterruption(ctx context.Context) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(ctx)
c := make(chan os.Signal, 1)
Expand Down
Loading