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: custom action to trigger amplify previews #287

Merged
merged 10 commits into from
Dec 18, 2024

Conversation

taraspos
Copy link
Contributor

@taraspos taraspos commented Nov 6, 2024

Summary

This PR creates custom github action which:

  • Accepts list of Amplify App IDs
  • Checks all Amplify App IDs to find if branch was already connected to one of the apps
  • If create-branch enabled
    • Connect branch to one of the AWS Amplify where limit hasn't been reached yet
    • Trigger initial deployment job
  • If wait enabled
    • Wait for deployment to succeed/fail
  • Post/update comment on the PR with preview URL

Context

Because of limitations with AWS Amplify, we can't use Amplify's GitHub integration in teleport or docs-website repos:

  1. Amplify has hard limits of 50 Amplify previews per app1, and we have more PRs in teleport app
  2. There is no way to create PR preview programatically, to use existing status checks and PR comments:
  3. If PR previews are created automatically there is no way to filter for which PRs to create previews, so as results previews are created even for PRs where docs were not modified which makes previews limit problem even more severe:
  4. Diff mechanism that exist doesn't work properly either:

Testing

Tested on:

This is a more advanced version of workflow running in docs-website repo, once this is merged, I will configure docs-website to run this version:

Run example:

PR comment looks like following:

image

or if latest deployment is not successful, it will show latest deployment and latest successful deployment.

image

Footnotes

  1. https://docs.aws.amazon.com/general/latest/gr/amplify.html

@taraspos taraspos requested a review from a team as a code owner November 6, 2024 17:06
@taraspos taraspos force-pushed the taras/amplify-preview-job branch from 844b294 to 31fd385 Compare November 6, 2024 17:35
@taraspos taraspos requested review from r0mant and jimbishopp November 6, 2024 18:40
Copy link
Contributor

@fheinecke fheinecke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this should be written as a Go or typescript tool. We've been trying to move this direction to reduce our reliance on GHA.

If you decide to go this route, there are tools in this directory for CI/CD workflows as well as local tooling.

.github/actions/amplify-preview/action.yaml Outdated Show resolved Hide resolved
.github/actions/amplify-preview/action.yaml Outdated Show resolved Hide resolved
.github/actions/amplify-preview/action.yaml Outdated Show resolved Hide resolved
@taraspos

This comment was marked as outdated.

@taraspos taraspos requested a review from fheinecke November 12, 2024 17:07
@taraspos taraspos marked this pull request as draft December 11, 2024 18:26
@taraspos taraspos force-pushed the taras/amplify-preview-job branch from 5e63c93 to 31b9ce9 Compare December 11, 2024 18:29
@taraspos taraspos force-pushed the taras/amplify-preview-job branch 4 times, most recently from f89122e to ac7989e Compare December 16, 2024 12:21
@taraspos taraspos force-pushed the taras/amplify-preview-job branch 2 times, most recently from 4578f7d to 5e7aaf5 Compare December 16, 2024 13:03
@taraspos taraspos force-pushed the taras/amplify-preview-job branch from 5e7aaf5 to 9cec7a5 Compare December 16, 2024 13:11
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/gravitational/shared-workflows/libs => ../../libs/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed to use new functions added to the github package.
Can be removed once new version is released.

@taraspos taraspos marked this pull request as ready for review December 16, 2024 15:57
@taraspos taraspos force-pushed the taras/amplify-preview-job branch from 92d7532 to 5b33a39 Compare December 16, 2024 16:41
@taraspos taraspos force-pushed the taras/amplify-preview-job branch from 9b2b5f0 to b7d7899 Compare December 16, 2024 17:03
Copy link
Contributor

@jimbishopp jimbishopp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 First pass.

libs/github/comment.go Show resolved Hide resolved
WAIT: ${{ inputs.wait }}
shell: bash
run: |
pushd ./tools/amplify-preview/; go run ./; popd
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need shell and pushd? go run ./tools/amplify-preview

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed in subsequent release PR once libs package is released and I can remove the replace rule from go.mod:

replace github.com/gravitational/shared-workflows/libs => ../../libs/

tools/amplify-preview/amplify.go Outdated Show resolved Hide resolved
"strings"
"time"

"github.com/alecthomas/kingpin/v2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, let's use either the flag package or cobra.

E.g.

func parseFlags() (flags, error) {
var (
workflow = flag.String("workflow", "", "specific workflow to run [assign, check, dismiss, backport]")
token = flag.String("token", "", "GitHub authentication token")
reviewers = flag.String("reviewers", "", "reviewer assignments")
local = flag.Bool("local", false, "local workflow dry run")
org = flag.String("org", "", "GitHub organization (local mode only)")
repo = flag.String("repo", "", "GitHub repository (local mode only)")
prNumber = flag.Int("pr", 0, "GitHub pull request number (local mode only)")
branch = flag.String("branch", "", "GitHub backport branch name (local mode only)")
baseStats = flag.String("base", "", "the artifact sizes as generated by binary-sizes to compare against for bloat")
buildDir = flag.String("builddir", "", "an absolute path to a build directory containing artifacts to be checked for bloat")
artifacts = flag.String("artifacts", "", "a comma separated list of compile artifacts to analyze for bloat")
)

https://github.com/gravitational/cloud/blob/master/tcctl/release/cli/cli.go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All tools in https://github.com/gravitational/shared-workflows/tree/master/tools are using github.com/alecthomas/kingpin and I followed their example for consistency 😬

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why kingpin was chosen for those tools. The review bot uses the simple flag package. We previously used kingpin in cloud but changed to cobra because it has a better design.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's because teleport uses kingpin everywhere:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing to align with other cloud projects is up to you. Lots of big projects chose cobra (K8s, Hugo, Github CLI, Docker, Istio, Helm, etc).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to keep it as is to avoid spending more time on this, as long term we would prefer using native integration, once Amplify get to resolve current issues.

tools/amplify-preview/main.go Outdated Show resolved Hide resolved
tools/amplify-preview/main.go Outdated Show resolved Hide resolved
tools/amplify-preview/main.go Outdated Show resolved Hide resolved
tools/amplify-preview/main.go Outdated Show resolved Hide resolved
tools/amplify-preview/main.go Outdated Show resolved Hide resolved
libs/github/comment.go Outdated Show resolved Hide resolved
@taraspos taraspos force-pushed the taras/amplify-preview-job branch 4 times, most recently from 0d00076 to 877547e Compare December 17, 2024 11:59
@taraspos taraspos force-pushed the taras/amplify-preview-job branch from 877547e to 42c033c Compare December 17, 2024 12:01
@taraspos taraspos requested a review from jimbishopp December 17, 2024 12:27
Comment on lines +36 to +49
for _, c := range comments {
matcher := true
if targetComment.UserLogin != "" {
matcher = matcher && c.User.GetLogin() == targetComment.UserLogin
}

if targetComment.BodyContains != "" {
matcher = matcher && strings.Contains(c.GetBody(), targetComment.BodyContains)
}

if matcher {
return c, nil
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proper zero values for CommentTraits means you can remove matcher and the empty string checks. Also, see https://pkg.go.dev/slices#ContainsFunc

tools/amplify-preview/amplify.go Outdated Show resolved Hide resolved
tools/amplify-preview/amplify.go Show resolved Hide resolved
"strings"
"time"

"github.com/alecthomas/kingpin/v2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing to align with other cloud projects is up to you. Lots of big projects chose cobra (K8s, Hugo, Github CLI, Docker, Istio, Helm, etc).

Comment on lines +92 to +95
failedResp := aggregatedError{
perAppErr: map[string]error{},
message: "failed to fetch branch",
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is aggregatedError needed? Looks like its value is in deduplicating error messages when the same appID is passed in via a flag. Why not deduplicate in main before reaching this point? That way this doesn't make two calls to GetBranch for the same appID and you could gather errors using a slice with errors join.

Same thing applies for CreateBranch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original purpose was not really to deduplicate errors but just to associate error with app where it happened, and then combine in one aggregated error.

tools/amplify-preview/github.go Outdated Show resolved Hide resolved
tools/amplify-preview/github.go Outdated Show resolved Hide resolved
tools/amplify-preview/github.go Outdated Show resolved Hide resolved
tools/amplify-preview/main.go Show resolved Hide resolved
tools/amplify-preview/main.go Outdated Show resolved Hide resolved
@taraspos taraspos force-pushed the taras/amplify-preview-job branch 2 times, most recently from 8ec9db3 to ce44d5b Compare December 17, 2024 17:32
@taraspos taraspos force-pushed the taras/amplify-preview-job branch from ce44d5b to 31cce17 Compare December 17, 2024 17:39
@taraspos taraspos merged commit 606b252 into main Dec 18, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants