Skip to content

Commit

Permalink
feat: github ci/cd hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ap0nia committed Jul 15, 2023
1 parent 9f3491a commit c3957b9
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 41 deletions.
67 changes: 66 additions & 1 deletion ant.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
* import * as cdk from 'aws-cdk-lib'
* ```
*/
import { consola } from "consola";
import core from "@actions/core";
import github from "@actions/github";
import { App, Stack } from "aws-cdk-lib/core";
import { Api } from "ant-stack/constructs/Api";
import { GitHub } from "ant-stack/constructs/GitHub";

/**
* @see https://github.com/evanw/esbuild/issues/1921#issuecomment-1491470829
Expand All @@ -26,7 +30,7 @@ export class MyStack extends Stack {
constructor(scope: App, id: string) {
super(scope, id);

new Api(this, "Api", {
const api = new Api(this, "Api", {
directory: "apps/api",
constructs: {},
runtime: {
Expand All @@ -44,6 +48,67 @@ export class MyStack extends Stack {
},
},
});

new GitHub(this, "GitHub", {
outputs: {
invokeUrl: {
value: api.api.arnForExecuteApi(),
},
},
callbacks: {
async onPostDeploy(outputs) {
const GITHUB_TOKEN = process.env.GITHUB_TOKEN ?? core.getInput("GITHUB_TOKEN");
const PR_NUM = github.context.payload.pull_request?.number;

if (!PR_NUM) {
throw new Error("❌ Error: Pull request number not detected.");
}

const octokit = github.getOctokit(GITHUB_TOKEN);

const owner = github.context.repo.owner;
const repo = github.context.repo.repo;
const ref = github.context.ref;

const apiDeployment = await octokit.rest.repos.createDeployment({
owner,
repo,
ref,
required_contexts: [],
environment: "staging - docs",
});

if (apiDeployment.status !== 201) {
throw new Error("❌ Deployment failed!");
}

const apiDeploymentStatus = await octokit.rest.repos.createDeploymentStatus({
repo,
owner,
deployment_id: apiDeployment.data.id,
state: "success",
description: "Deployment succeeded",
environment_url: outputs.invokeUrl,
auto_inactive: false,
});

consola.info("ℹ️ API deployment status: ", apiDeploymentStatus.data);

await octokit.rest.issues.createComment({
owner,
repo,
issue_number: PR_NUM,
body: `\
🚀 Staging instances deployed!
API - ${apiDeploymentStatus.data.environment_url}
Docs - ${apiDeploymentStatus.data.environment_url}
`,
});
},
},
});
}
}

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
}
},
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1",
"consola": "^3.1.0",
"constructs": "^10.2.69"
},
"devDependencies": {
Expand Down
14 changes: 7 additions & 7 deletions packages/ant-stack/src/cdk/constructs/GitHub/GitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export type GitHubCallbacks<T> = {

export interface GitHubConfig<T> {
outputsFile?: string;
outputs: T;
callbacks: GitHubCallbacks<T>;
outputs?: T;
callbacks?: Partial<GitHubCallbacks<T>>;
}

/**
Expand Down Expand Up @@ -53,7 +53,7 @@ export class GitHub<T extends Outputs = Outputs> extends Construct {

this.stackName = Stack.of(this).stackName;

this.outputs = Object.entries(config.outputs).reduce((outputs, [key, value]) => {
this.outputs = Object.entries(config.outputs ?? {}).reduce((outputs, [key, value]) => {
outputs[key] = new CfnOutput(this, key, value);
return outputs;
}, {} as Record<PropertyKey, CfnOutput>);
Expand All @@ -71,19 +71,19 @@ export class GitHub<T extends Outputs = Outputs> extends Construct {
}

public onPreDeploy() {
return this.config.callbacks.onPreDeploy(this.parseOutputs());
return this.config.callbacks?.onPreDeploy?.(this.parseOutputs());
}

public onPostDeploy() {
return this.config.callbacks.onPostDeploy(this.parseOutputs());
return this.config.callbacks?.onPostDeploy?.(this.parseOutputs());
}

public onPreDestroy() {
return this.config.callbacks.onPreDestroy(this.parseOutputs());
return this.config.callbacks?.onPreDestroy?.(this.parseOutputs());
}

public onPostDestroy() {
return this.config.callbacks.onPostDestroy(this.parseOutputs());
return this.config.callbacks?.onPostDestroy?.(this.parseOutputs());
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/ant-stack/src/cdk/constructs/GitHub/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./GitHub.js";
44 changes: 11 additions & 33 deletions pnpm-lock.yaml

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

0 comments on commit c3957b9

Please sign in to comment.