Skip to content

Commit

Permalink
fix(codepipeline): cannot trigger on all tags anymore in EcrSourceAct…
Browse files Browse the repository at this point in the history
…ion (aws#17270)

The EcrSourceAction could previously be used to trigger on changes to all tags of an image. As part of the fix aws#13818, the imageTag was defaulted to latest if not provided. Therefore it was no longer possible to call the underlying onCloudTrailImagePushed function with an undefined imageTag to watch changes on all tags.

Reintroduce triggering on all tags by passing an empty string as the imageTag.

Fixes aws#13818


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
sparten11740 authored and Andrew Beresford committed Nov 29, 2021
1 parent 1d91770 commit 2ab3446
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface EcrSourceVariables {
export interface EcrSourceActionProps extends codepipeline.CommonAwsActionProps {
/**
* The image tag that will be checked for changes.
* Provide an empty string to trigger on changes to any tag.
*
* @default 'latest'
*/
Expand Down Expand Up @@ -95,7 +96,7 @@ export class EcrSourceAction extends Action {

this.props.repository.onCloudTrailImagePushed(Names.nodeUniqueId(stage.pipeline.node) + 'SourceEventRule', {
target: new targets.CodePipeline(stage.pipeline),
imageTag: this.props.imageTag ?? 'latest',
imageTag: this.props.imageTag === '' ? undefined : (this.props.imageTag ?? 'latest'),
});

// the Action Role also needs to write to the Pipeline's bucket
Expand All @@ -104,7 +105,7 @@ export class EcrSourceAction extends Action {
return {
configuration: {
RepositoryName: this.props.repository.repositoryName,
ImageTag: this.props.imageTag,
ImageTag: this.props.imageTag ? this.props.imageTag : undefined, // `''` is falsy in JS/TS
},
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import '@aws-cdk/assert-internal/jest';
import { ABSENT } from '@aws-cdk/assert-internal';
import * as codebuild from '@aws-cdk/aws-codebuild';
import * as codepipeline from '@aws-cdk/aws-codepipeline';
import * as ecr from '@aws-cdk/aws-ecr';
Expand Down Expand Up @@ -60,6 +61,78 @@ describe('ecr source action', () => {
});


expect(stack).toHaveResourceLike('AWS::Events::Rule', {
'EventPattern': {
'detail': {
'requestParameters': {
'imageTag': ['latest'],
},
},
},
});
});

test('watches all tags when imageTag provided as empty string', () => {
const stack = new Stack();

const sourceOutput = new codepipeline.Artifact();
const ecrSourceAction = new cpactions.EcrSourceAction({
actionName: 'Source',
output: sourceOutput,
repository: ecr.Repository.fromRepositoryName(stack, 'Repo', 'repo'),
imageTag: '',
});

new codepipeline.Pipeline(stack, 'Pipeline', {
stages: [
{
stageName: 'Source',
actions: [ecrSourceAction],
},
{
stageName: 'Build',
actions: [
new cpactions.CodeBuildAction({
actionName: 'Build',
project: new codebuild.PipelineProject(stack, 'MyProject'),
input: sourceOutput,
environmentVariables: {
ImageDigest: { value: ecrSourceAction.variables.imageDigest },
},
}),
],
},
],
});

expect(stack).toHaveResourceLike('AWS::Events::Rule', {
'EventPattern': {
'source': [
'aws.ecr',
],
'detail': {
'requestParameters': {
'imageTag': ABSENT,
},
},
},
});

expect(stack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
'Stages': [
{
'Name': 'Source',
'Actions': [
{
'Name': 'Source',
'Configuration': {
'ImageTag': ABSENT,
},
},
],
},
],
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -415,4 +415,4 @@
}
}
}
}
}

0 comments on commit 2ab3446

Please sign in to comment.