Skip to content

Instantly share code, notes, and snippets.

@cfuerst
Created December 10, 2022 19:40
Show Gist options
  • Select an option

  • Save cfuerst/a33d48a299f2a3fa8293ad3983b051a1 to your computer and use it in GitHub Desktop.

Select an option

Save cfuerst/a33d48a299f2a3fa8293ad3983b051a1 to your computer and use it in GitHub Desktop.

Revisions

  1. cfuerst created this gist Dec 10, 2022.
    63 changes: 63 additions & 0 deletions .projen-helper.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    const { JsonPatch } = require("projen");

    const awsConfigureStep = {
    uses: "aws-actions/configure-aws-credentials@v1-node16",
    with: {
    "aws-access-key-id": "${{ secrets.AWS_ACCESS_KEY_ID }}",
    "aws-secret-access-key": "${{ secrets.AWS_SECRET_ACCESS_KEY }}",
    "aws-region": "${{ secrets.CDK_DEPLOY_REGION }}",
    },
    };

    const preDestroyJobs = {
    runsOn: "ubuntu-latest",
    permissions: { contents: "write" },
    if: "github.event.ref_type == 'branch'",
    env: { GIT_DEL_BRANCH: "${{ github.event.ref }}" },
    steps: [
    {
    uses: "actions/checkout@v3",
    with: {
    ref: "${{ github.event.pull_request.head.ref }}",
    repository: "${{ github.event.pull_request.head.repo.full_name }}",
    },
    },
    {
    uses: "actions/setup-node@v3",
    with: {
    "node-version": "16.18.1",
    },
    },
    {
    run: "yarn install --check-files",
    },
    {
    uses: "aws-actions/configure-aws-credentials@v1-node16",
    with: {
    "aws-access-key-id": "${{ secrets.AWS_ACCESS_KEY_ID }}",
    "aws-secret-access-key": "${{ secrets.AWS_SECRET_ACCESS_KEY }}",
    "aws-region": "${{ secrets.CDK_DEPLOY_REGION }}",
    },
    },
    {
    run: "npx projen default",
    },
    awsConfigureStep,
    { id: "cdk-destroy", run: "npx cdk destroy --require-approval never --force >> $GITHUB_STEP_SUMMARY" },
    ],
    };

    const cdkDeployCmd = { id: "cdk-deploy", run: "npx cdk deploy --require-approval never --force >> $GITHUB_STEP_SUMMARY" };

    const patches = {
    build: [
    JsonPatch.add("/jobs/build/steps/3", awsConfigureStep),
    JsonPatch.add("/jobs/build/steps/-", cdkDeployCmd)
    ],
    release: [
    JsonPatch.add("/jobs/release/steps/3", awsConfigureStep),
    JsonPatch.add("/jobs/release/steps/-", cdkDeployCmd)
    ]
    };

    module.exports = { preDestroyJobs, patches };
    48 changes: 48 additions & 0 deletions .projen.rc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    const { awscdk } = require("projen");
    const { preDestroyJobs, patches } = require("./.projen-helper.js");

    const project = new awscdk.AwsCdkTypeScriptApp({
    author: "cfuerst",
    authorAddress: "[email protected]",
    authorName: "Christian Fürst",
    authorOrganization: true,
    copyrightOwner: "Christian Fürst",
    cdkVersion: "2.5.0",
    defaultReleaseBranch: "main",
    name: "@cloudy-with-a-chance-of-meatballs/cdk-app-example-rest-api-rad-typescript",
    repositoryUrl: "https://github.com/cloudy-with-a-chance-of-meatballs/cdk-app-example-rest-api-rad-typescript",
    license: "MIT",
    minNodeVersion: "16.18.1",
    gitignore: [".idea/"],
    deps: [
    "aws-cdk-lib",
    "constructs",
    "@cloudy-with-a-chance-of-meatballs/cdk-lambda-token-authorizer-jwt@^v0.0.41",
    "@pepperize/cdk-apigateway-swagger-ui@^0.0.160",
    ],
    description: "cdk sample application for a blueprint of a RAD (rapid application development) production ready rest api.",
    eslint: true,
    prettier: true,
    prettierOptions: {
    settings: { printWidth: 160 },
    },
    release: true,
    releaseEveryCommit: true,
    githubOptions: {
    pullRequestLint: false,
    },
    });

    //add a workflow for cdk destroy
    const destroyWorkflow = project.github.addWorkflow("destroy");
    destroyWorkflow.on({ delete: {}, workflowDispatch: {} });
    destroyWorkflow.addJob("cdk-destroy", preDestroyJobs);

    //add aws cli secret setup and deploy steps
    ["build", "release"].forEach((v) => {
    project.github.tryFindWorkflow(v).file.patch(patches[v][0]);
    project.github.tryFindWorkflow(v).file.patch(patches[v][1]);
    });

    // synthesize the project files
    project.synth();