Skip to content

Commit

Permalink
ci: update codebuild stack using SSM pointers
Browse files Browse the repository at this point in the history
This uses the SSM pointers defined and provisioned in another stack that
provides a pre-established parameter with the Container Image and its
Tag to use.
  • Loading branch information
jahkeup committed Jan 10, 2020
1 parent f48c6fc commit 8ecb5fb
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 46 deletions.
74 changes: 48 additions & 26 deletions tools/infra/stacks/infra-pr-build.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# stack: infra-pr-build

Parameters:
BuildSpecPath:
Type: String
Expand All @@ -12,6 +14,28 @@ Parameters:
Description: >-
The GitHub repository that builds run for. Your account must be authorized
to modify this repository's settings.
EnvironmentImageName:
Type: AWS::SSM::Parameter::Value<String>
Default: /infra/container/infra/builder
Description: >-
Parameter that defines the image name the builder uses as its execution
environment *without* a tag (eg: registry/image-name, not
registry/image-name:tag). The EnvironmentImageTag Parameter provides the
appropriate tag separately.
EnvironmentImageTag:
Type: String
Default: latest
Description: >-
The image 'tag' (as in registry/image-name:tag) to select of the EnvironmentImage
provided.
ImageCredentialsType:
Type: String
Default: CODEBUILD
AllowedValues: [ CODEBUILD, SERVICE_ROLE ]
Description: >-
If image policy does not trust codebuild.amazonaws.com OR cross-account
role is needed, then the SERVICE_ROLE must be specified to use the role
assigned to the build project.
Resources:
BuildArtifactBucket:
Expand Down Expand Up @@ -41,40 +65,37 @@ Resources:
BuildRolePolicy:
Type: AWS::IAM::Policy
Properties:
Roles:
- Ref: BuildRole
Roles: !Ref BuildRole
PolicyName: BuildRolePolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
# For validation API usage performed on templates during build.
- Action:
- cloudformation:ValidateTemplate
- Sid: "validateCfnTemplates"
Effect: Allow
Action:
- cloudformation:ValidateTemplate
Resource: "*"
# For managing cache, logs, and artifacts in the build's buckets.
- Action:
- Sid: "manageBuildArtifacts"
Effect: Allow
Action:
- s3:GetObject*
- s3:GetBucket*
- s3:List*
- s3:PutObject*
- s3:Abort*
Effect: Allow
Resource:
- !GetAtt BuildArtifactBucket.Arn
- Fn::Join:
- ""
- - !GetAtt BuildArtifactBucket.Arn
- /*
- !Sub "${BuildArtifactBucket.Arn}/*"
- !GetAtt BuildLogBucket.Arn
- Fn::Join:
- ""
- - !GetAtt BuildLogBucket.Arn
- /*
- !Sub "${BuildLogBucket.Arn}/*"
# For writing to CloudWatch Logs Streams for each build.
- Action:
- Sid: "manageBuildLogs"
Effect: Allow
Action:
- logs:CreateLogStream
- logs:PutLogEvents
Effect: Allow
Resource:
- !GetAtt BuildLogGroup.Arn

Expand All @@ -86,14 +107,19 @@ Resources:
- BuildLogGroup
- BuildRole
Properties:
Artifacts:
Location: !Ref BuildArtifactBucket
Name: /
NamespaceType: BUILD_ID
Packaging: NONE
Path: artifact/
Type: S3
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/standard:2.0
Type: LINUX_CONTAINER
ServiceRole:
Fn::GetAtt:
- BuildRole
- Arn
Image: !Sub "${EnvironmentImageName}:${EnvironmentImageTag}"
ImagePullCredentialsType: !Ref ImageCredentialsType
ServiceRole: !GetAtt BuildRole.Arn
Source:
BuildSpec: !Ref BuildSpecPath
Location: !Ref SourceGitHubRepositoryURL
Expand All @@ -102,11 +128,7 @@ Resources:
LogsConfig:
S3Logs:
Status: ENABLED
Location:
Fn::Join:
- "/"
- - !GetAtt BuildArtifactBucket.Arn
- "codebuild/log"
Location: !Sub "${BuildArtifactBucket.Arn}/codebuild/log"
CloudWatchLogs:
Status: ENABLED
GroupName: !Ref BuildLogGroup
Expand Down
59 changes: 39 additions & 20 deletions tools/infra/stacks/thar-pr-build.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# stack: thar-pr-build

Parameters:
BuildSpecPath:
Type: String
AllowedPattern: '.+\.yml$'
Default: "./tools/infra/buildspec/thar-pr-build.yml"
Description: "The path to the buildspec.yml file to use."
Description: >-
The path to the buildspec.yml file to use.
SourceGitHubRepositoryURL:
Type: String
Default: "https://github.com/amazonlinux/PRIVATE-thar.git"
Expand All @@ -12,6 +15,29 @@ Parameters:
Description: >-
The GitHub repository that builds run for. Your account must be authorized
to modify this repository's settings.
EnvironmentImageName:
Type: AWS::SSM::Parameter::Value<String>
Default: /infra/container/infra/builder
Description: >-
Parameter that defines the image name the builder uses as its execution
environment *without* a tag (eg: registry/image-name, not
registry/image-name:tag). The EnvironmentImageTag Parameter provides the
appropriate tag separately.
EnvironmentImageTag:
Type: String
Default: latest
Description: >-
The image 'tag' (as in registry/image-name:tag) to select of the EnvironmentImage
provided.
ImageCredentialsType:
Type: String
Default: CODEBUILD
AllowedValues: [ CODEBUILD, SERVICE_ROLE ]
Description: >-
If image policy does not trust codebuild.amazonaws.com OR cross-account
role is needed, then the SERVICE_ROLE must be specified to use the role
assigned to the build project.
Resources:
BuildArtifactBucket:
Type: AWS::S3::Bucket
Expand Down Expand Up @@ -47,29 +73,25 @@ Resources:
Version: "2012-10-17"
Statement:
# For managing cache, logs, and artifacts in the build's buckets.
- Action:
- Sid: "manageBuildArtifacts"
Effect: Allow
Action:
- s3:GetObject*
- s3:GetBucket*
- s3:List*
- s3:PutObject*
- s3:Abort*
Effect: Allow
Resource:
- !GetAtt BuildArtifactBucket.Arn
- Fn::Join:
- ""
- - !GetAtt BuildArtifactBucket.Arn
- /*
- !Sub "${BuildArtifactBucket.Arn}/*"
- !GetAtt BuildLogBucket.Arn
- Fn::Join:
- ""
- - !GetAtt BuildLogBucket.Arn
- /*
- !Sub "${BuildLogBucket.Arn}/*"
# For writing to CloudWatch Logs Streams for each build.
- Action:
- Sid: "manageBuildLogs"
Effect: Allow
Action:
- logs:CreateLogStream
- logs:PutLogEvents
Effect: Allow
Resource:
- !GetAtt BuildLogGroup.Arn

Expand All @@ -90,9 +112,10 @@ Resources:
Type: S3
Environment:
ComputeType: BUILD_GENERAL1_LARGE
Image: aws/codebuild/standard:2.0
PrivilegedMode: true
Type: LINUX_CONTAINER
PrivilegedMode: true
Image: !Sub "${EnvironmentImageName}:${EnvironmentImageTag}"
ImagePullCredentialsType: !Ref ImageCredentialsType
ServiceRole: !GetAtt BuildRole.Arn
Source:
BuildSpec: !Ref BuildSpecPath
Expand All @@ -102,11 +125,7 @@ Resources:
LogsConfig:
S3Logs:
Status: ENABLED
Location:
Fn::Join:
- "/"
- - !GetAtt BuildLogBucket.Arn
- "codebuild/log"
Location: !Sub "${BuildLogBucket.Arn}/codebuild/log"
CloudWatchLogs:
Status: ENABLED
GroupName: !Ref BuildLogGroup
Expand Down

0 comments on commit 8ecb5fb

Please sign in to comment.