Skip to content

Instantly share code, notes, and snippets.

@cfuerst
Created December 10, 2022 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfuerst/a33d48a299f2a3fa8293ad3983b051a1 to your computer and use it in GitHub Desktop.
Save cfuerst/a33d48a299f2a3fa8293ad3983b051a1 to your computer and use it in GitHub Desktop.
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 };
const { awscdk } = require("projen");
const { preDestroyJobs, patches } = require("./.projen-helper.js");
const project = new awscdk.AwsCdkTypeScriptApp({
author: "cfuerst",
authorAddress: "c.fuerst@gmail.com",
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();
@AminFazlMondo
Copy link

Hi @cfuerst ,

To add the deploy job, I would be doing something like below:

const { awscdk } = require("projen");
const { preDestroyJobs, patches } = require("./.projen-helper.js");

const project = new awscdk.AwsCdkTypeScriptApp({
  author: "cfuerst",
  authorAddress: "c.fuerst@gmail.com",
  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",
  ],
  codeArtifactOptions: {
    accessKeyIdSecret: 'AWS_ACCESS_KEY_ID',
    secretAccessKeySecret: 'AWS_SECRET_ACCESS_KEY',
  }
  deployOptions: {
    npmConfigEnvironment: 'stack',  //optional
    environments: [{
      name: 'amin1',
      awsCredentials: {
        region: 'ap-southeast-2',
      },
    },
    {
      name: 'amin2',
      awsCredentials: {
        region: 'ap-southeast-2',
        roleToAssume: 'arn:aws:iam::123456789012:role/amin-test-build-role',
      },
    }],
  },
  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);

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment