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

Feature Branch Deploys #98

Draft
wants to merge 38 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
dbb89d2
Add RUN_POPULATE check to run manage.py populate
joyliu-q Oct 24, 2021
dde5574
Exit from script if errors
joyliu-q Oct 24, 2021
9d2b9d0
:art: Modify env variable
joyliu-q Apr 12, 2022
0dd8059
Merge branch 'master' of https://github.com/pennlabs/infrastructure i…
joyliu-q Apr 12, 2022
7698c00
:art: Test Config
joyliu-q Apr 12, 2022
42fe3d9
:memo: Update snapshots
joyliu-q Apr 12, 2022
7326fab
:art: Generate Cleaner Workflow Files
joyliu-q Apr 17, 2022
46d30d4
:new: ADD NUKE JOBS BAHAHAHAHA
joyliu-q Apr 17, 2022
6a02646
:art: Nit formatting
joyliu-q Apr 17, 2022
f8c14d9
Merge branch 'master' of https://github.com/pennlabs/infrastructure i…
joyliu-q Apr 19, 2022
8cdeb4b
:art: Make feature branch deploys optional (safer transition for each…
joyliu-q Apr 19, 2022
b231014
:bug: Fix dedent backslash bug
joyliu-q Apr 19, 2022
88f2edf
:new: [Kittyhawk] Add IS_FEATURE_BRANCH env variable to applications
joyliu-q Apr 19, 2022
2a647a2
:new: [Kittyhawk] Add IS_FEATURE_BRANCH env variable to cronjob conta…
joyliu-q Apr 19, 2022
54c3d51
:boom: [Kraken] Change Feature Branch Deploy Workflow Behavior
joyliu-q Apr 21, 2022
be16591
:art: Rename branch: follow branch prefix naming convention
joyliu-q Apr 21, 2022
1bcff82
:adhesive_bandage: Add Deployment Needs Oops
joyliu-q Apr 21, 2022
a5363ce
:memo: Add nuke to exports
joyliu-q Apr 21, 2022
d93384f
:bug: Handle feature branch but no PR case
joyliu-q Apr 21, 2022
3a42911
:art: Consistent naming
joyliu-q Jun 19, 2022
d7b663e
:art: Nits
joyliu-q Jun 19, 2022
2a11e50
:art: Fix jest tests
joyliu-q Jun 20, 2022
f962d57
Merge branch 'master' of https://github.com/pennlabs/infrastructure i…
joyliu-q Jun 20, 2022
dbb1cdd
:bug: STOP ESCAPING MY BACKSLASHES DEDENT UR A CLOWN
joyliu-q Jun 20, 2022
fff2270
:sweat_drops: Tears have been shed
joyliu-q Jun 20, 2022
15767c5
:arrow_up: Bump version and add changelog
joyliu-q Jun 23, 2022
1b3aecb
:tada: Add Commenting Feature to Deployment
joyliu-q Jun 23, 2022
63ec282
:art: Clean-up
joyliu-q Jun 25, 2022
0bdb237
:arrow_up: Update package.json
joyliu-q Jun 25, 2022
3ac3f57
:bug: Fix deps
joyliu-q Jun 25, 2022
0c5403e
:arrow_up: Bump kittyhawk
joyliu-q Jun 27, 2022
e820a05
:arrow_up: Bump kraken version
joyliu-q Jun 27, 2022
ac8f6a9
:art: Add Docker Publish Support for Feature Branch Deploys
joyliu-q Jun 28, 2022
2fad452
:tada: Fix deploy announcement
joyliu-q Jun 28, 2022
bfc7f01
:tada: Add announcement & fix nuke job
joyliu-q Jun 30, 2022
6e20bfa
:arrow_up: Bump version
joyliu-q Jun 30, 2022
4c618e7
:art: Fix pushing-to-closed-branch bug
joyliu-q Jun 30, 2022
c887d91
:art: Fix snapshots
joyliu-q Jul 5, 2022
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
31 changes: 28 additions & 3 deletions cdk/kraken/src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export interface DeployJobProps {
* @default master
*/
defaultBranch?: string;

/**
* Deploy for a specific feature branch.
* @default false
*/
isFeatureDeploy?: boolean;
}

/**
Expand All @@ -37,27 +43,46 @@ export class DeployJob extends CheckoutJob {
const fullConfig: Required<DeployJobProps> = {
deployTag: "${{ github.sha }}",
defaultBranch: "master",
isFeatureDeploy: false,
...config,
};

super(scope, "deploy", {
runsOn: "ubuntu-latest",
if: `github.ref == 'refs/heads/${fullConfig.defaultBranch}'`,
if: fullConfig.isFeatureDeploy
? `github.event_name == 'pull_request'`
: `github.ref == 'refs/heads/${fullConfig.defaultBranch}'`,
steps: [
{
id: "synth",
name: "Synth cdk8s manifests",
run: dedent`cd k8s
yarn install --frozen-lockfile
${
fullConfig.isFeatureDeploy
? dedent`

# Feature Branch deployment set-up
export IS_FEATURE_BRANCH=true;
export RELEASE_NAME=\${REPOSITORY#*/}-pr-$PR_NUMBER;

`
: dedent`

# get repo name (by removing owner/organization)
# Get repo name (by removing owner/organization)
export RELEASE_NAME=\${REPOSITORY#*/}

`
}
# Export RELEASE_NAME as an output
echo "::set-output name=RELEASE_NAME::$RELEASE_NAME"

yarn build`,
env: {
...(fullConfig.isFeatureDeploy && {
PR_NUMBER: "${{ github.event.pull_request.number }}",
GIT_REF: "${{ github.ref }}",
}),
GIT_SHA: fullConfig.deployTag,
REPOSITORY: "${{ github.repository }}",
AWS_ACCOUNT_ID: "${{ secrets.AWS_ACCOUNT_ID }}",
Expand All @@ -67,7 +92,7 @@ export class DeployJob extends CheckoutJob {
name: "Deploy",
run: dedent`aws eks --region us-east-1 update-kubeconfig --name production --role-arn arn:aws:iam::\${AWS_ACCOUNT_ID}:role/kubectl

# get repo name from synth step
# Get repo name from synth step
joyliu-q marked this conversation as resolved.
Show resolved Hide resolved
RELEASE_NAME=\${{ steps.synth.outputs.RELEASE_NAME }}

# Deploy
Expand Down
38 changes: 38 additions & 0 deletions cdk/kraken/src/labs-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
IntegrationTestsJob,
IntegrationTestsJobProps,
} from "./integration-tests";
import { NukeJob } from "./nuke";
import { ReactCheckJobProps } from "./react";
import { ReactProject } from "./react-project";

Expand Down Expand Up @@ -207,5 +208,42 @@ export class LabsApplicationStack extends Stack {
...fullConfig.deployOverrides,
needs: deployNeeds,
});

// Feature Branch Deploy
const featureBranchDeployWorkflow = new Workflow(
this,
"feature-branch-deploy",
{
name: "Feature Branch Deploy",
on: { pullRequest: { types: ["opened"] } },
...overrides,
}
);

new DeployJob(
featureBranchDeployWorkflow,
{
...fullConfig.deployProps,
isFeatureDeploy: true,
},
{
...fullConfig.deployOverrides,
}
);

// Feature Branch Nuke
const featureBranchNukeWorkflow = new Workflow(
this,
"feature-branch-nuke",
{
name: "Feature Branch Nuke",
on: { pullRequest: { types: ["closed"] } },
...overrides,
}
);

new NukeJob(featureBranchNukeWorkflow, fullConfig.deployProps, {
...fullConfig.deployOverrides,
});
}
}
82 changes: 82 additions & 0 deletions cdk/kraken/src/nuke.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { CheckoutJob, Workflow, JobProps } from "cdkactions";
import dedent from "ts-dedent";

/**
* Optional props to configure the nuke job.
*/
export interface NukeJobProps {
/**
* Deploy tag to set in kittyhawk.
* @default current git sha
*/
deployTag?: string;
}

/**
* A job to undeploy/nuke a product.
* When we nuke, it should ALWAYS be for feature branch deployment, not production.
*/
export class NukeJob extends CheckoutJob {
/**
*
* @param scope cdkactions Workflow instance
* @param config Optional configuration for the deploy job.
* @param overrides Optional overrides for the job.
*/
public constructor(
scope: Workflow,
config?: NukeJobProps,
overrides?: Partial<JobProps>
) {
// Nuke config
const fullConfig: Required<NukeJobProps> = {
deployTag: "${{ github.sha }}",
...config,
};

super(scope, "nuke", {
runsOn: "ubuntu-latest",
steps: [
{
id: "synth",
name: "Synth cdk8s manifests",
run: dedent`cd k8s
yarn install --frozen-lockfile

# Feature Branch nuke set-up
export IS_FEATURE_BRANCH=true;
export RELEASE_NAME=\${REPOSITORY#*/}-pr-$PR_NUMBER;

# Export RELEASE_NAME as an output
echo "::set-output name=RELEASE_NAME::$RELEASE_NAME"

yarn build`,
env: {
PR_NUMBER: "${{ github.event.pull_request.number }}",
GIT_REF: "${{ github.ref }}",
GIT_SHA: fullConfig.deployTag,
REPOSITORY: "${{ github.repository }}",
AWS_ACCOUNT_ID: "${{ secrets.AWS_ACCOUNT_ID }}",
},
},
{
name: "Nuke",
run: dedent`aws eks --region us-east-1 update-kubeconfig --name production --role-arn arn:aws:iam::\${AWS_ACCOUNT_ID}:role/kubectl

# Get repo name from synth step
RELEASE_NAME=\${{ steps.synth.outputs.RELEASE_NAME }}

# Delete all non-certificate resources
kubectl delete -f k8s/dist/ -l app.kubernetes.io/part-of=$RELEASE_NAME
`,
env: {
AWS_ACCOUNT_ID: "${{ secrets.AWS_ACCOUNT_ID }}",
AWS_ACCESS_KEY_ID: "${{ secrets.GH_AWS_ACCESS_KEY_ID }}",
AWS_SECRET_ACCESS_KEY: "${{ secrets.GH_AWS_SECRET_ACCESS_KEY }}",
},
},
],
...overrides,
});
}
}
4 changes: 2 additions & 2 deletions cdk/kraken/test/__snapshots__/custom.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading