generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 155
CodeBuild Setup for GitHub Docker Image Builds #2745
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 OR ISC | ||
| import itertools | ||
| import typing | ||
|
|
||
| from aws_cdk import ( | ||
| Duration, | ||
| aws_codebuild as codebuild, | ||
| aws_iam as iam, | ||
| aws_logs as logs, | ||
| aws_ecr as ecr, | ||
| Environment, | ||
| ) | ||
| from constructs import Construct | ||
|
|
||
| from cdk.aws_lc_base_ci_stack import AwsLcBaseCiStack | ||
| from util.iam_policies import ( | ||
| code_build_publish_metrics_in_json, | ||
| ) | ||
| from util.metadata import UBUNTU_ECR_REPO, AMAZONLINUX_ECR_REPO, CENTOS_ECR_REPO, FEDORA_ECR_REPO | ||
|
|
||
| class AwsLcGitHubDockerActionsStack(AwsLcBaseCiStack): | ||
| """Define a stack used to execute AWS-LC self-hosted GitHub Actions Runners on Docker Images.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| scope: Construct, | ||
| id: str, | ||
| env: typing.Union[Environment, typing.Dict[str, typing.Any]], | ||
| **kwargs | ||
| ) -> None: | ||
| super().__init__(scope, id, env=env, timeout=180, **kwargs) | ||
|
|
||
| # Define a IAM role for this stack. | ||
| metrics_policy = iam.PolicyDocument.from_json( | ||
| code_build_publish_metrics_in_json(env) | ||
| ) | ||
|
|
||
| repo_names = [UBUNTU_ECR_REPO, AMAZONLINUX_ECR_REPO, CENTOS_ECR_REPO, FEDORA_ECR_REPO] | ||
| ecr_repos = [ecr.Repository.from_repository_name(self, x.replace('/', '-'), repository_name=x) | ||
| for x in repo_names] | ||
|
|
||
| staging_repo = ecr.Repository(self, "aws-lc-ecr-staging", | ||
| image_tag_mutability=ecr.TagMutability.IMMUTABLE, | ||
| lifecycle_rules=[ecr.LifecycleRule( | ||
| max_image_age=Duration.days(7), | ||
| )]) | ||
|
|
||
| ecr_repos.append(staging_repo) | ||
|
|
||
| inline_policies = { | ||
| "metrics_policy": metrics_policy, | ||
| "ecr": iam.PolicyDocument( | ||
| statements=[ | ||
| iam.PolicyStatement( | ||
| effect=iam.Effect.ALLOW, | ||
| actions=[ | ||
| "ecr:GetAuthorizationToken", | ||
| ], | ||
| resources=["*"], | ||
| ), | ||
| iam.PolicyStatement( | ||
| effect=iam.Effect.ALLOW, | ||
| actions=[ | ||
| "ecr:BatchGetImage", | ||
| "ecr:BatchCheckLayerAvailability", | ||
| "ecr:CompleteLayerUpload", | ||
| "ecr:GetDownloadUrlForLayer", | ||
| "ecr:InitiateLayerUpload", | ||
| "ecr:PutImage", | ||
| "ecr:UploadLayerPart", | ||
| ], | ||
| resources=[x for x in itertools.chain([ | ||
| x.repository_arn for x in ecr_repos | ||
| ], [ecr.Repository.from_repository_name(self, "quay-io", "quay.io/*").repository_arn])], | ||
| ), | ||
| ], | ||
| ) | ||
| } | ||
| role = iam.Role( | ||
| scope=self, | ||
| id="{}-role".format(id), | ||
| assumed_by=iam.ServicePrincipal("codebuild.amazonaws.com"), | ||
| inline_policies=inline_policies, | ||
| ) | ||
|
|
||
| logging_options = codebuild.LoggingOptions( | ||
| cloud_watch=codebuild.CloudWatchLoggingOptions(log_group=logs.LogGroup( | ||
| self, id="{}-logs".format(id))) | ||
| ) | ||
|
|
||
| # Override base class provided configuration | ||
| self.git_hub_source = codebuild.Source.git_hub( | ||
| owner=self.github_repo_owner, | ||
| repo=self.github_repo_name, | ||
| webhook=True, | ||
| webhook_filters=[ | ||
| codebuild.FilterGroup.in_event_of( | ||
| codebuild.EventAction.WORKFLOW_JOB_QUEUED | ||
| ), | ||
| ], | ||
| ) | ||
|
|
||
| # Define CodeBuild. | ||
| project = codebuild.Project( | ||
| scope=self, | ||
| id=id, | ||
| project_name=id, | ||
| source=self.git_hub_source, | ||
| role=role, | ||
| timeout=Duration.minutes(self.timeout), | ||
| logging=logging_options, | ||
| environment=codebuild.BuildEnvironment( | ||
| compute_type=codebuild.ComputeType.SMALL, | ||
| privileged=True, | ||
| build_image=codebuild.LinuxBuildImage.STANDARD_7_0, | ||
| environment_variables={ | ||
| "AWS_ACCOUNT_ID": codebuild.BuildEnvironmentVariable(value=env.account), | ||
| "ECR_REGISTRY_URL": codebuild.BuildEnvironmentVariable(value=staging_repo.registry_uri), | ||
| "ECR_STAGING_REPO": codebuild.BuildEnvironmentVariable(value=staging_repo.repository_uri), | ||
| }, | ||
| ), | ||
| ) | ||
|
|
||
| cfn_project = project.node.default_child | ||
| cfn_project.add_property_override("Triggers.PullRequestBuildPolicy", self.pull_request_policy) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NP: In what situations would an image hit the 7-day maximum? Do we need 7 days?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can probably be shortened if we want. It's mainly for historical uses or to be able to look at an image that was staged if we forsee doing more extensive image testing in the future and want to deep dive into one of the docker images that failed to get to the publishing step.