Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(codepeline): ECR cannot trigger on multiple tags #20897

Merged
merged 2 commits into from
Jun 30, 2022

Conversation

rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented Jun 28, 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 #20594.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

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.
@rix0rrr rix0rrr requested a review from a team June 28, 2022 09:05
@rix0rrr rix0rrr self-assigned this Jun 28, 2022
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Jun 28, 2022
@rix0rrr rix0rrr marked this pull request as ready for review June 28, 2022 09:05
@gitpod-io
Copy link

gitpod-io bot commented Jun 28, 2022

@aws-cdk-automation aws-cdk-automation requested a review from a team June 28, 2022 09:05
@github-actions github-actions bot added bug This issue is a bug. effort/small Small work item – less than a day of effort p1 labels Jun 28, 2022
@ahammond
Copy link
Contributor

It's worth pointing out that if you trigger on latest, the received artifact includes all other tags as well. So you can, for example tag an image with both v1.2.3 and latest and then trigger on latest, but publish v1.2.3.

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,
            }),
          ],
        },
      ],
    });
  }
}

@mergify
Copy link
Contributor

mergify bot commented Jun 30, 2022

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).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 2f13604
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit 0368374 into main Jun 30, 2022
@mergify mergify bot deleted the huijbers/no-ecr-multiple-tags branch June 30, 2022 12:39
@mergify
Copy link
Contributor

mergify bot commented Jun 30, 2022

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*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. contribution/core This is a PR that came from AWS. effort/small Small work item – less than a day of effort p1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

aws-codepipeline-actions: EcrSourceAction should trigger for all tags when imageTag is empty string
4 participants