docs(codepeline): ECR cannot trigger on multiple tags#20897
Merged
mergify[bot] merged 2 commits intomainfrom Jun 30, 2022
Merged
docs(codepeline): ECR cannot trigger on multiple tags#20897mergify[bot] merged 2 commits intomainfrom
mergify[bot] merged 2 commits intomainfrom
Conversation
The current ECR source action docs seem to indicate you can make it trigger on more than one tag at a time (or even all tags). This is not true, so stop advertising that feature. Fixes #20594.
Contributor
|
It's worth pointing out that if you trigger on import {
aws_codebuild,
aws_codepipeline,
aws_codepipeline_actions,
aws_ec2,
aws_ecr,
aws_ecs,
aws_ssm,
} from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { Namer } from 'multi-convention-namer';
// import { ServicePrincipals } from 'cdk-constants';
export interface CouplerPipelineProps {
/**
* The AWS account hosting the ECR.
*/
readonly account: string;
/**
* A string containing a python re which will be used to identify the tag to put
* in the `imagedefinitions.json` file that will be handed off to the EcsDeployAction.
* Typically you want SemVer tags. But...
*
* @default '^v\\d+\\.\\d+\\.\\d+'
*/
readonly immutableTagPattern?: string;
/**
* The AWS region hosting the ECR.
*/
readonly region: string;
/**
* When referencing the ECS Cluster, we can optionally provide the list of
* securityGroups it is in. We haven't seen a need for this yet.
*
* @default []
*/
readonly securityGroups?: aws_ec2.ISecurityGroup[];
/**
* Which docker tag should we watch for pushes?
* This will be used by the EcrSourceAction to filter.
* Until we get a proper resolution to https://github.com/aws/aws-cdk/issues/20594
*
* @default 'latest'
*/
readonly triggerTag?: string;
/**
* The VPC in which the ECS Cluster resides.
*/
readonly vpc: aws_ec2.IVpc;
/**
* The Repository which to connect it to. If omitted it will make certain
* assumptions about the repo.
* @default - service specific repository pulled from ssm params
*/
readonly repository?: aws_ecr.IRepository;
}
export class CouplerPipeline extends Construct {
constructor(scope: Construct, id: Namer, props: CouplerPipelineProps) {
super(scope, id.addSuffix(['coupler']).pascal);
const immutableTagPattern = props.immutableTagPattern ?? '^v\\d+\\.\\d+\\.\\d+';
const imageInput = new aws_codepipeline.Artifact('Image');
const updateInput = new aws_codepipeline.Artifact('ImageDefinitions');
const repository =
props.repository ??
aws_ecr.Repository.fromRepositoryAttributes(this, 'Repository', {
repositoryArn: aws_ssm.StringParameter.valueForStringParameter(
this,
`/${id.pascal}/${id.addSuffix(['repository']).pascal}/${id.pascal}/repositoryArn`,
),
repositoryName: id.kebab,
});
const cluster = aws_ecs.Cluster.fromClusterAttributes(this, 'Cluster', {
clusterName: id.kebab,
securityGroups: props.securityGroups ?? [],
vpc: props.vpc,
});
const service = aws_ecs.FargateService.fromFargateServiceAttributes(this, 'Service', {
cluster,
serviceName: id.pascal,
});
new aws_codepipeline.Pipeline(this, id.pascal, {
pipelineName: id.pascal,
stages: [
{
stageName: 'Received',
actions: [
new aws_codepipeline_actions.EcrSourceAction({
actionName: 'Image',
imageTag: props.triggerTag ?? 'latest',
repository,
output: imageInput,
}),
],
},
{
stageName: 'GenerateImageDefinitions',
actions: [
new aws_codepipeline_actions.CodeBuildAction({
environmentVariables: {
ECR_REPO_URI: { value: `${props.account}.dkr.ecr.${props.region}.amazonaws.com/${id.kebab}` },
},
input: imageInput,
outputs: [updateInput],
actionName: 'GenerateImageDefinitions',
project: new aws_codebuild.Project(this, 'GenerateImageDefinitions', {
buildSpec: aws_codebuild.BuildSpec.fromObject({
version: '0.2',
phases: {
build: {
commands: [
'cat imageDetail.json',
//https://stackoverflow.com/a/57015190
`TAG=$(cat imageDetail.json | python -c "import re, sys, json; p=re.compile('${immutableTagPattern}'); tags=json.load(sys.stdin)['ImageTags']; print([s for s in tags if p.match(s)][0])")`,
'echo "${ECR_REPO_URI}:${TAG}"',
`printf '[{\"name\":\"${id.kebab}\",\"imageUri\":\"%s\"}]' $ECR_REPO_URI:$TAG > imagedefinitions.json`,
'pwd; ls -al; cat imagedefinitions.json',
],
},
},
artifacts: { files: ['imagedefinitions.json'] },
}),
}),
}),
],
},
{
stageName: 'UpdateFargate',
actions: [
new aws_codepipeline_actions.EcsDeployAction({
actionName: 'Deploy',
input: updateInput,
service,
}),
],
},
],
});
}
} |
corymhall
approved these changes
Jun 30, 2022
Contributor
|
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
Collaborator
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
Contributor
|
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
daschaa
pushed a commit
to daschaa/aws-cdk
that referenced
this pull request
Jul 9, 2022
The current ECR source action docs seem to indicate you can make it trigger on more than one tag at a time (or even all tags). This is not true, so stop advertising that feature. Fixes aws#20594. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The current ECR source action docs seem to indicate you can make it
trigger on more than one tag at a time (or even all tags). This is
not true, so stop advertising that feature.
Fixes #20594.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license