Skip to content

Commit 39fe11b

Browse files
authoredNov 25, 2021
fix(codepipeline): cannot trigger on all tags anymore in EcrSourceAction (#17270)
The EcrSourceAction could previously be used to trigger on changes to all tags of an image. As part of the fix #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 #13818 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 1fe2215 commit 39fe11b

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed
 

‎packages/@aws-cdk/aws-codepipeline-actions/lib/ecr/source-action.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export interface EcrSourceVariables {
3636
export interface EcrSourceActionProps extends codepipeline.CommonAwsActionProps {
3737
/**
3838
* The image tag that will be checked for changes.
39+
* Provide an empty string to trigger on changes to any tag.
3940
*
4041
* @default 'latest'
4142
*/
@@ -95,7 +96,7 @@ export class EcrSourceAction extends Action {
9596

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

101102
// the Action Role also needs to write to the Pipeline's bucket
@@ -104,7 +105,7 @@ export class EcrSourceAction extends Action {
104105
return {
105106
configuration: {
106107
RepositoryName: this.props.repository.repositoryName,
107-
ImageTag: this.props.imageTag,
108+
ImageTag: this.props.imageTag ? this.props.imageTag : undefined, // `''` is falsy in JS/TS
108109
},
109110
};
110111
}

‎packages/@aws-cdk/aws-codepipeline-actions/test/ecr/ecr-source-action.test.ts

+73
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import '@aws-cdk/assert-internal/jest';
2+
import { ABSENT } from '@aws-cdk/assert-internal';
23
import * as codebuild from '@aws-cdk/aws-codebuild';
34
import * as codepipeline from '@aws-cdk/aws-codepipeline';
45
import * as ecr from '@aws-cdk/aws-ecr';
@@ -60,6 +61,78 @@ describe('ecr source action', () => {
6061
});
6162

6263

64+
expect(stack).toHaveResourceLike('AWS::Events::Rule', {
65+
'EventPattern': {
66+
'detail': {
67+
'requestParameters': {
68+
'imageTag': ['latest'],
69+
},
70+
},
71+
},
72+
});
73+
});
74+
75+
test('watches all tags when imageTag provided as empty string', () => {
76+
const stack = new Stack();
77+
78+
const sourceOutput = new codepipeline.Artifact();
79+
const ecrSourceAction = new cpactions.EcrSourceAction({
80+
actionName: 'Source',
81+
output: sourceOutput,
82+
repository: ecr.Repository.fromRepositoryName(stack, 'Repo', 'repo'),
83+
imageTag: '',
84+
});
85+
86+
new codepipeline.Pipeline(stack, 'Pipeline', {
87+
stages: [
88+
{
89+
stageName: 'Source',
90+
actions: [ecrSourceAction],
91+
},
92+
{
93+
stageName: 'Build',
94+
actions: [
95+
new cpactions.CodeBuildAction({
96+
actionName: 'Build',
97+
project: new codebuild.PipelineProject(stack, 'MyProject'),
98+
input: sourceOutput,
99+
environmentVariables: {
100+
ImageDigest: { value: ecrSourceAction.variables.imageDigest },
101+
},
102+
}),
103+
],
104+
},
105+
],
106+
});
107+
108+
expect(stack).toHaveResourceLike('AWS::Events::Rule', {
109+
'EventPattern': {
110+
'source': [
111+
'aws.ecr',
112+
],
113+
'detail': {
114+
'requestParameters': {
115+
'imageTag': ABSENT,
116+
},
117+
},
118+
},
119+
});
120+
121+
expect(stack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
122+
'Stages': [
123+
{
124+
'Name': 'Source',
125+
'Actions': [
126+
{
127+
'Name': 'Source',
128+
'Configuration': {
129+
'ImageTag': ABSENT,
130+
},
131+
},
132+
],
133+
},
134+
],
135+
});
63136
});
64137
});
65138
});

‎packages/@aws-cdk/aws-codepipeline-actions/test/integ.pipeline-ecr-source.expected.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,4 @@
415415
}
416416
}
417417
}
418-
}
418+
}

0 commit comments

Comments
 (0)
Please sign in to comment.