Skip to content

Commit

Permalink
feat(pipelines): add support for caching to codebuild steps (#20533)
Browse files Browse the repository at this point in the history
Fixes #16375

Closes #19084

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)?
	* [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Hi-Fi committed Jun 15, 2022
1 parent 703e62e commit 81ef665
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 4 deletions.
3 changes: 3 additions & 0 deletions packages/@aws-cdk/pipelines/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,9 @@ new pipelines.CodeBuildStep('Synth', {
subnetSelection: { subnetType: ec2.SubnetType.PRIVATE_WITH_NAT },
securityGroups: [mySecurityGroup],

// Control caching
cache: codebuild.Cache.bucket(new s3.Bucket(this, 'Cache')),

// Additional policy statements for the execution role
rolePolicyStatements: [
new iam.PolicyStatement({ /* ... */ }),
Expand Down
15 changes: 15 additions & 0 deletions packages/@aws-cdk/pipelines/lib/codepipeline/codebuild-step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ export interface CodeBuildStepProps extends ShellStepProps {
*/
readonly subnetSelection?: ec2.SubnetSelection;

/**
* Caching strategy to use.
*
* @default - No cache
*/
readonly cache?: codebuild.Cache;

/**
* Policy statements to add to role used during the synth
*
Expand Down Expand Up @@ -139,6 +146,13 @@ export class CodeBuildStep extends ShellStep {
*/
public readonly subnetSelection?: ec2.SubnetSelection;

/**
* Caching strategy to use.
*
* @default - No cache
*/
public readonly cache?: codebuild.Cache;

/**
* Policy statements to add to role used during the synth
*
Expand Down Expand Up @@ -196,6 +210,7 @@ export class CodeBuildStep extends ShellStep {
this._partialBuildSpec = props.partialBuildSpec;
this.vpc = props.vpc;
this.subnetSelection = props.subnetSelection;
this.cache = props.cache;
this.role = props.role;
this.actionRole = props.actionRole;
this.rolePolicyStatements = props.rolePolicyStatements;
Expand Down
7 changes: 7 additions & 0 deletions packages/@aws-cdk/pipelines/lib/codepipeline/codepipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ export interface CodeBuildOptions {
*/
readonly subnetSelection?: ec2.SubnetSelection;

/**
* Caching strategy to use.
*
* @default - No cache
*/
readonly cache?: cb.Cache;

/**
* The number of minutes after which AWS CodeBuild stops the build if it's
* not complete. For valid values, see the timeoutInMinutes field in the AWS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export class CodeBuildFactory implements ICodePipelineActionFactory {
partialBuildSpec: step.partialBuildSpec,
vpc: step.vpc,
subnetSelection: step.subnetSelection,
cache: step.cache,
timeout: step.timeout,
}),
});
Expand Down Expand Up @@ -298,6 +299,7 @@ export class CodeBuildFactory implements ICodePipelineActionFactory {
vpc: projectOptions.vpc,
subnetSelection: projectOptions.subnetSelection,
securityGroups: projectOptions.securityGroups,
cache: projectOptions.cache,
buildSpec: projectBuildSpec,
role: this.props.role,
timeout: projectOptions.timeout,
Expand Down Expand Up @@ -430,6 +432,7 @@ export function mergeCodeBuildOptions(...opts: Array<CodeBuildOptions | undefine
vpc: b.vpc ?? a.vpc,
subnetSelection: b.subnetSelection ?? a.subnetSelection,
timeout: b.timeout ?? a.timeout,
cache: b.cache ?? a.cache,
};
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Template, Match } from '@aws-cdk/assertions';
import * as codebuild from '@aws-cdk/aws-codebuild';
import * as iam from '@aws-cdk/aws-iam';
import * as s3 from '@aws-cdk/aws-s3';
import { Duration, Stack } from '@aws-cdk/core';
import * as cdkp from '../../lib';
import { PIPELINE_ENV, TestApp, ModernTestGitHubNpmPipeline, AppWithOutput } from '../testhelpers';
Expand Down Expand Up @@ -274,3 +276,24 @@ test('exportedVariables', () => {
},
});
});

test('step has caching set', () => {
// WHEN
const myCachingBucket = new s3.Bucket(pipelineStack, 'MyCachingBucket');
new cdkp.CodePipeline(pipelineStack, 'Pipeline', {
synth: new cdkp.CodeBuildStep('Synth', {
cache: codebuild.Cache.bucket(myCachingBucket),
commands: ['/bin/true'],
input: cdkp.CodePipelineSource.gitHub('test/test', 'main'),
}),
});

// THEN
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodeBuild::Project', {
Cache: {
Location: {
'Fn::Join': ['/', [{ Ref: 'MyCachingBucket8C98C553' }, { Ref: 'AWS::NoValue' }]],
},
},
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// eslint-disable-next-line import/no-extraneous-dependencies
/// !cdk-integ VariablePipelineStack pragma:set-context:@aws-cdk/core:newStyleStackSynthesis=true
import * as codebuild from '@aws-cdk/aws-codebuild';
import * as s3 from '@aws-cdk/aws-s3';
import { App, Stack, StackProps, RemovalPolicy } from '@aws-cdk/core';
import { Construct } from 'constructs';
Expand All @@ -24,8 +25,11 @@ class PipelineStack extends Stack {
selfMutation: false,
});

const cacheBucket = new s3.Bucket(this, 'TestCacheBucket');

const producer = new pipelines.CodeBuildStep('Produce', {
commands: ['export MY_VAR=hello'],
cache: codebuild.Cache.bucket(cacheBucket),
});

const consumer = new pipelines.CodeBuildStep('Consume', {
Expand All @@ -35,6 +39,7 @@ class PipelineStack extends Stack {
commands: [
'echo "The variable was: $THE_VAR"',
],
cache: codebuild.Cache.bucket(cacheBucket),
});

// WHEN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,43 @@
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": [
"s3:Abort*",
"s3:DeleteObject*",
"s3:GetBucket*",
"s3:GetObject*",
"s3:List*",
"s3:PutObject",
"s3:PutObjectLegalHold",
"s3:PutObjectRetention",
"s3:PutObjectTagging",
"s3:PutObjectVersionTagging"
],
"Effect": "Allow",
"Resource": [
{
"Fn::GetAtt": [
"TestCacheBucketA6BDC126",
"Arn"
]
},
{
"Fn::Join": [
"",
[
{
"Fn::GetAtt": [
"TestCacheBucketA6BDC126",
"Arn"
]
},
"/*"
]
]
}
]
},
{
"Action": [
"logs:CreateLogGroup",
Expand Down Expand Up @@ -932,7 +969,20 @@
"Type": "CODEPIPELINE"
},
"Cache": {
"Type": "NO_CACHE"
"Location": {
"Fn::Join": [
"/",
[
{
"Ref": "TestCacheBucketA6BDC126"
},
{
"Ref": "AWS::NoValue"
}
]
]
},
"Type": "S3"
},
"Description": "Pipeline step VariablePipelineStack/Pipeline/MyWave/Produce",
"EncryptionKey": "alias/aws/s3"
Expand Down Expand Up @@ -960,6 +1010,43 @@
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": [
"s3:Abort*",
"s3:DeleteObject*",
"s3:GetBucket*",
"s3:GetObject*",
"s3:List*",
"s3:PutObject",
"s3:PutObjectLegalHold",
"s3:PutObjectRetention",
"s3:PutObjectTagging",
"s3:PutObjectVersionTagging"
],
"Effect": "Allow",
"Resource": [
{
"Fn::GetAtt": [
"TestCacheBucketA6BDC126",
"Arn"
]
},
{
"Fn::Join": [
"",
[
{
"Fn::GetAtt": [
"TestCacheBucketA6BDC126",
"Arn"
]
},
"/*"
]
]
}
]
},
{
"Action": [
"logs:CreateLogGroup",
Expand Down Expand Up @@ -1116,7 +1203,20 @@
"Type": "CODEPIPELINE"
},
"Cache": {
"Type": "NO_CACHE"
"Location": {
"Fn::Join": [
"/",
[
{
"Ref": "TestCacheBucketA6BDC126"
},
{
"Ref": "AWS::NoValue"
}
]
]
},
"Type": "S3"
},
"Description": "Pipeline step VariablePipelineStack/Pipeline/MyWave/Consume",
"EncryptionKey": "alias/aws/s3"
Expand Down Expand Up @@ -1202,6 +1302,11 @@
}
]
}
},
"TestCacheBucketA6BDC126": {
"Type": "AWS::S3::Bucket",
"UpdateReplacePolicy": "Retain",
"DeletionPolicy": "Retain"
}
},
"Parameters": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@
"data": "PipelineCodeBuildActionRoleDefaultPolicy1D62A6FE"
}
],
"/VariablePipelineStack/TestCacheBucket/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "TestCacheBucketA6BDC126"
}
],
"/VariablePipelineStack/BootstrapVersion": [
{
"type": "aws:cdk:logicalId",
Expand Down

0 comments on commit 81ef665

Please sign in to comment.