diff --git a/lib/__tests__/shellable.test.ts b/lib/__tests__/shellable.test.ts index 681ded52..46d0771d 100644 --- a/lib/__tests__/shellable.test.ts +++ b/lib/__tests__/shellable.test.ts @@ -555,3 +555,36 @@ test('environment variables', () => { ], }); }); + +test('can exclude files from scriptDirectory', () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'TestStack'); + + new Shellable(stack, 'EnvironmentVariables', { + scriptDirectory: path.join(__dirname, 'delivlib-tests/linux'), + // This should result in only `test.sh` being included + excludeFilePatterns: ['*.sh', '**/README', '!test.sh'], + entrypoint: 'test.sh', + }); + + const template = Template.fromStack(stack); + template.hasResourceProperties('AWS::CodeBuild::Project', { + Environment: { + EnvironmentVariables: [ + { + Name: 'SCRIPT_S3_BUCKET', + Type: 'PLAINTEXT', + Value: { + 'Fn::Sub': 'cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}', + }, + }, + { + Name: 'SCRIPT_S3_KEY', + Type: 'PLAINTEXT', + // This is the hash of a directory with only `test.sh` included + Value: 'f2ad7bd80137ae8bf3e86164ae8943f7ffbe8b99470f91eb3f24c3b83873a089.zip', + }, + ], + }, + }); +}); diff --git a/lib/shellable.ts b/lib/shellable.ts index 2f4ab585..4ced79d5 100644 --- a/lib/shellable.ts +++ b/lib/shellable.ts @@ -4,7 +4,7 @@ import { Duration, aws_cloudwatch as cloudwatch, aws_codebuild as cbuild, aws_codepipeline as cpipeline, aws_codepipeline_actions as cpipeline_actions, - aws_iam as iam, aws_s3_assets as assets, aws_secretsmanager, aws_ssm, + aws_iam as iam, aws_s3_assets as assets, aws_secretsmanager, aws_ssm, IgnoreMode, } from 'aws-cdk-lib'; import { IRole } from 'aws-cdk-lib/aws-iam'; import { Construct } from 'constructs'; @@ -170,10 +170,15 @@ export interface ShellableProps extends ShellableOptions { /** * Directory with the scripts. * - * The whole directory will be uploaded. + * By default the whole directory will be uploaded. Use `excludeFilePatterns` to ignore files. */ scriptDirectory: string; + /** + * File paths matching the glob patterns will be excluded from the script dir. + */ + excludeFilePatterns?: string[]; + /** * Filename of the initial script to start, relative to scriptDirectory. */ @@ -299,6 +304,8 @@ export class Shellable extends Construct { const asset = new assets.Asset(this, 'ScriptDirectory', { path: props.scriptDirectory, + exclude: props.excludeFilePatterns, + ignoreMode: IgnoreMode.GLOB, }); this.outputArtifactName = (props.producesArtifacts ?? true) ? `Artifact_${this.node.addr}` : undefined;