|
| 1 | +import codepipeline = require('@aws-cdk/aws-codepipeline'); |
| 2 | +import ecs = require('@aws-cdk/aws-ecs'); |
| 3 | +import iam = require('@aws-cdk/aws-iam'); |
| 4 | + |
| 5 | +/** |
| 6 | + * Construction properties of {@link EcsDeployAction}. |
| 7 | + */ |
| 8 | +export interface EcsDeployActionProps extends codepipeline.CommonActionProps { |
| 9 | + /** |
| 10 | + * The input artifact that contains the JSON image definitions file to use for deployments. |
| 11 | + * The JSON file is a list of objects, |
| 12 | + * each with 2 keys: `name` is the name of the container in the Task Definition, |
| 13 | + * and `imageUri` is the Docker image URI you want to update your service with. |
| 14 | + * If you use this property, it's assumed the file is called 'imagedefinitions.json'. |
| 15 | + * If your build uses a different file, leave this property empty, |
| 16 | + * and use the `imageFile` property instead. |
| 17 | + * |
| 18 | + * @default - one of this property, or `imageFile`, is required |
| 19 | + * @see https://docs.aws.amazon.com/codepipeline/latest/userguide/pipelines-create.html#pipelines-create-image-definitions |
| 20 | + */ |
| 21 | + readonly inputArtifact?: codepipeline.Artifact; |
| 22 | + |
| 23 | + /** |
| 24 | + * The name of the JSON image definitions file to use for deployments. |
| 25 | + * The JSON file is a list of objects, |
| 26 | + * each with 2 keys: `name` is the name of the container in the Task Definition, |
| 27 | + * and `imageUri` is the Docker image URI you want to update your service with. |
| 28 | + * Use this property if you want to use a different name for this file than the default 'imagedefinitions.json'. |
| 29 | + * If you use this property, you don't need to specify the `inputArtifact` property. |
| 30 | + * |
| 31 | + * @default - one of this property, or `inputArtifact`, is required |
| 32 | + * @see https://docs.aws.amazon.com/codepipeline/latest/userguide/pipelines-create.html#pipelines-create-image-definitions |
| 33 | + */ |
| 34 | + readonly imageFile?: codepipeline.ArtifactPath; |
| 35 | + |
| 36 | + /** |
| 37 | + * The ECS Service to deploy. |
| 38 | + */ |
| 39 | + readonly service: ecs.BaseService; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * CodePipeline Action to deploy an ECS Service. |
| 44 | + */ |
| 45 | +export class EcsDeployAction extends codepipeline.DeployAction { |
| 46 | + constructor(props: EcsDeployActionProps) { |
| 47 | + super({ |
| 48 | + ...props, |
| 49 | + inputArtifact: determineInputArtifact(props), |
| 50 | + provider: 'ECS', |
| 51 | + artifactBounds: { |
| 52 | + minInputs: 1, |
| 53 | + maxInputs: 1, |
| 54 | + minOutputs: 0, |
| 55 | + maxOutputs: 0, |
| 56 | + }, |
| 57 | + configuration: { |
| 58 | + ClusterName: props.service.clusterName, |
| 59 | + ServiceName: props.service.serviceName, |
| 60 | + FileName: props.imageFile && props.imageFile.fileName, |
| 61 | + }, |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + protected bind(info: codepipeline.ActionBind): void { |
| 66 | + // permissions based on CodePipeline documentation: |
| 67 | + // https://docs.aws.amazon.com/codepipeline/latest/userguide/how-to-custom-role.html#how-to-update-role-new-services |
| 68 | + info.role.addToPolicy(new iam.PolicyStatement() |
| 69 | + .addActions( |
| 70 | + 'ecs:DescribeServices', |
| 71 | + 'ecs:DescribeTaskDefinition', |
| 72 | + 'ecs:DescribeTasks', |
| 73 | + 'ecs:ListTasks', |
| 74 | + 'ecs:RegisterTaskDefinition', |
| 75 | + 'ecs:UpdateService', |
| 76 | + ) |
| 77 | + .addAllResources()); |
| 78 | + |
| 79 | + info.role.addToPolicy(new iam.PolicyStatement() |
| 80 | + .addActions( |
| 81 | + 'iam:PassRole', |
| 82 | + ) |
| 83 | + .addAllResources() |
| 84 | + .addCondition('StringEqualsIfExists', { |
| 85 | + 'iam:PassedToService': [ |
| 86 | + 'ec2.amazonaws.com', |
| 87 | + 'ecs-tasks.amazonaws.com', |
| 88 | + ], |
| 89 | + })); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +function determineInputArtifact(props: EcsDeployActionProps): codepipeline.Artifact { |
| 94 | + if (props.imageFile && props.inputArtifact) { |
| 95 | + throw new Error("Exactly one of 'inputArtifact' or 'imageFile' can be provided in the ECS deploy Action"); |
| 96 | + } |
| 97 | + if (props.imageFile) { |
| 98 | + return props.imageFile.artifact; |
| 99 | + } |
| 100 | + if (props.inputArtifact) { |
| 101 | + return props.inputArtifact; |
| 102 | + } |
| 103 | + throw new Error("Specifying one of 'inputArtifact' or 'imageFile' is required for the ECS deploy Action"); |
| 104 | +} |
0 commit comments