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

feat: allow files to be excluded from scriptDirectory #1526

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/__tests__/shellable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
],
},
});
});
11 changes: 9 additions & 2 deletions lib/shellable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
Expand Down