Sign inSign up

aligent/cdk-deploy-pipe

By aligent

•Updated 3 months ago

Image
0

3.4K

aligent/cdk-deploy-pipe repository overview

⁠Aligent CDK Deploy Pipe

A BitBucket pipe to deploy CDK Stacks

⁠Terminologies

⁠Static Config

Static config is a yaml script which can be used to configure and customise the behaviour of the cdk commands.

cdk-pipe:
  commands:
    cdk:
      bootstrap: npx cdk bootstrap
      deploy: npx cdk deploy
      synth: npx cdk synth
      diff: npx cdk diff
    npm:
      checks:
        lint: npm run lint
        format: npm run format
      install: npm ci
  beforeScripts:
    - npm version
  afterScripts:
    - echo "Deployment is completed"

The default static script can be overridden at runtime by setting CDK_CONFIG_PATH environment variable. When a custom config is provided, it will be deep-merged with the default configuration, allowing you to override only specific commands while inheriting the rest.

For example, if you only want to override the deploy command, your custom config file can contain just:

cdk-pipe:
  commands:
    cdk:
      deploy: npx cdk deploy --require-approval never

All other commands (bootstrap, synth, diff, npm commands, etc.) will be inherited from the default config.

⁠Before and After scripts

As the name suggests, the before and after scripts can be used to extend the behaviour. Both static config script and environment variables (CDK_BEFORE_SCRIPT, CDK_AFTER_SCRIPT) can be used in the context. When setting before and after scripts using environment variables, use ; to separate the statements.

⁠Linting and Formatting Checks

Linting and Formatting checks against the CDK code can be executed at build time.
Required packages and configurations should be included as a apart of the infrastructure code.
The default execution commands are defined as a part of static config⁠

cdk-pipe:
  commands:
    cdk:
      ...
    npm:
      checks:
        lint: npm run lint
        format: npm run format
      ...

Note: If the linting and formatting commands in package.json supposed to auto-correct the issues, you may either use a custom static config file with a custom set of commands, or CHECK_LINT_CMD/CHECK_FORMAT_CMD to override them
If CHECK_LINT_CMD/CHECK_FORMAT_CMD has been set, it is not required to set CHECK_LINT/CHECK_FORMAT

⁠CDK_EXTRA_ARGS

CDK_EXTRA_ARGS environment variable only to be associated, when it is required to append additional arguments to cdk deploy command. For example, CDK_EXTRA_ARGS='--require-approval never' will extend the deploy command to cdk deploy --require-approval never

If it is required to extend diff, synth or bootstrap commands, CDK_EXTRA_ARGS_DIFF, CDK_EXTRA_ARGS_SYNTH and CDK_EXTRA_ARGS_BOOTSTRAP environment variables should be used.

⁠YAML Definition

Minimum configuration

- step:
    name: "CDK Deploy"
    script:
    - pipe: docker://aligent/cdk-deploy-pipe
    variables: 
        AWS_ACCESS_KEY_ID=AWSACCESSKEYID123
        AWS_SECRET_ACCESS_KEY=awssecretaccesskey@#1232

⁠Variables

VariableUsageDefaults
AWS_ACCESS_KEY_IDAWS access key id for CDK deploymentN/A
AWS_SECRET_ACCESS_KEYAWS secret key for CDK deploymentN/A
AWS_DEFAULT_REGIONDefault AWS regionNil
CDK_ROOT_DIRThe working directory where the cdk commands should executed./
DEBUGTo enable debug logsfalse
CDK_BOOTSTRAPSet this to true if it is required to bootstrap the stack prior to the deploymentfalse
CDK_SYNTHSet this to true if it is required to run synth on the stack prior to the deploymentfalse
CDK_DIFFSet this to true if it is required to run diff on the stack prior to the deploymentfalse
CDK_DEPLOYSet this to false to skip CDK deploymenttrue
CDK_BEFORE_SCRIPTSet to extend the before script which is configured using the static configNil
CDK_AFTER_SCRIPTSet to extend the after script which is configured using the static configNil
CDK_EXTRA_ARGSSet to extend CDK deployment statement which is configured in the static configNil
CDK_CONFIG_PATHSet this if a custom static config should be associated./cdk-config.yml
CHECK_LINTSet this to true to validate the lintingfalse
CHECK_FORMATSet this to true to validate the formattingfalse
CHECK_LINT_CMDUse this to override default linting command from static config⁠, (this would set CHECK_LINT)false
CHECK_FORMAT_CMDUse this to override default formatting command from static config⁠, (this would set CHECK_FORMAT)false

⁠Development

To build and run locally

    # cd into the root directory
    docker build -t cdk-pipe:dev .
    docker run -e AWS_ACCESS_KEY_ID=AWSACCESSKEYID1212 -e AWS_SECRET_ACCESS_KEY=AWSSCRETKEYACCESSKEY1212 -e CDK_ROOT_DIR='./' -e CDK_EXTRA_ARGS='--require-approval never' -e DEBUG=true cdk-pipe:dev

To execute the cdk processes, python subprocess⁠ library has been used. To alter this with a new library use __scriptRunner() function

    def __scriptRunner(self, working_dir, arr):
        # Change working Directory
        ...
        while i < len(arr):
            try:
                output = subprocess.run(str(arr[i]).strip().split(" "), check=True)
                combined_output += ["exec => {} returned {}".format(arr[i], output.returncode)]
            ...
        return True, combined_output

Tag summary

Content type

Image

Digest

sha256:cf95e85ac…

Size

81.1 MB

Last updated

3 months ago

docker pull aligent/cdk-deploy-pipe