aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.5k stars 3.85k forks source link

CDKv2: `DockerImageAsset` need to execute command before `docker build` / `cdk-asset` #20689

Open dennisguse opened 2 years ago

dennisguse commented 2 years ago

Describe the feature

I need to execute a shell command before executing cdk-asset (that triggers docker build) within the generated CodeBuildAction.

Use Case

Preparing the environment for the docker build as it otherwise fails.

Proposed Solution

Add a property to DockerImageAssetProps:

export interface DockerImageAssetProps extends DockerImageAssetOptions { 
  /** 
   * The directory where the Dockerfile is stored 
   * 
   * Any directory inside with a name that matches the CDK output folder (cdk.out by default) will be excluded from the asset 
   */ 
  readonly directory: string; 

  // New
  prepareCommands : []string;
}

These prepareCommands will then be added to the CodeBuildStep that executes the docker build:

https://github.com/aws/aws-cdk/blob/244135b333007adc1188f0ad8690be4e7526353d/packages/%40aws-cdk/pipelines/lib/codepipeline/codepipeline.ts#L687

Other Information

Hacky workaround: preprocess generated Cloudformation template using shell scripts after cdk synth.

Acknowledgements

CDK version used

v2

Environment details (OS name and version, etc.)

not relevant

indrora commented 2 years ago

Shouldn't this be done in the Dockerfile itself?

FROM alpine
ADD . /app
WORKDIR /app
RUN prepare-environment.sh
RUN yarn build
CMD yarn run
dennisguse commented 2 years ago

No. Commands must be executed before docker build. During the docker build is too late.

ncaq commented 10 months ago

I wanted this because my requirements prevent me from doing cdk synth because docker build depends on other deploy results of CDK. docker images: cannot use tokens in buildArgs - improve validation and errors · Issue #3981 · aws/aws-cdk