Skip to content

Commit

Permalink
Add logic to create deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
jamieparkinson committed Aug 3, 2021
1 parent 02b28a0 commit 83c4b63
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 121 deletions.
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,32 @@
"version": "1.0.0",
"main": "src/index.ts",
"bin": "dist/index.js",
"type": "module",
"repository": "git@github.com:wellcomecollection/github-deployments-buildkite-plugin.git",
"license": "MIT",
"scripts": {
"build:ts": "tsc",
"build:pkg": "pkg ./package.json",
"build": "yarn run build:ts && yarn run build:pkg",
"clean": "rimraf dist bin",
"prepare": "husky install"
"prepare": "husky install",
"dev": "ts-node src/index.ts"
},
"devDependencies": {
"@types/git-url-parse": "^9.0.1",
"@types/node": "^16.4.7",
"husky": "^7.0.1",
"lint-staged": "^11.1.1",
"pkg": "^5.3.1",
"prettier": "^2.3.2",
"rimraf": "^3.0.2",
"ts-node": "^10.1.0",
"typescript": "^4.3.5"
},
"dependencies": {
"@octokit/auth-app": "^3.5.3",
"@octokit/core": "^3.5.1",
"aws-sdk": "^2.957.0",
"octokit": "^1.1.0"
"git-url-parse": "^11.5.0"
},
"lint-staged": {
"*.{js,css,md}": "prettier --write"
Expand Down
31 changes: 31 additions & 0 deletions src/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import AWS from "aws-sdk";
import { createAppAuth, StrategyOptions } from "@octokit/auth-app";
import { Octokit } from "@octokit/core";

const authConfigSecrets = {
appId: "github/weco_app/id",
privateKey: "github/weco_app/private_key",
};

const getAppAuthConfig = async () => {
const config = { ...authConfigSecrets };
const secretsManager = new AWS.SecretsManager();
for (const [configKey, secretId] of Object.entries(authConfigSecrets)) {
const result = await secretsManager
.getSecretValue({ SecretId: secretId })
.promise();
config[configKey as keyof typeof config] = result["SecretString"]!;
}
return config as StrategyOptions;
};

export const getAppOctokit = async () => {
const config = await getAppAuthConfig();
return (
config &&
new Octokit({
authStrategy: createAppAuth,
auth: config,
})
);
};
51 changes: 49 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
console.log("Hello world");
import { Octokit } from "@octokit/core";
import gitUrlParse from "git-url-parse";
import { getAppOctokit } from "./auth";
import { config, environment } from "./properties";

export {}
const main = async () => {
console.log("Fetching app config...");
const appOctokit = await getAppOctokit();
if (!appOctokit) {
throw new Error("Could not authenticate app");
}

const repo = gitUrlParse(environment.repo);
const octokitOptions = { owner: repo.owner, repo: repo.name };

console.log(`Finding app installation for repo: ${repo.name}`);
const installation = await appOctokit.request(
"GET /repos/{owner}/{repo}/installation",
octokitOptions
);
const octokit = (await appOctokit.auth({
type: "installation",
installationId: installation.data.id,
factory: (opts: any) => new Octokit(opts),
})) as Octokit;

console.log("Creating deployment...");
const res = await octokit.request("POST /repos/{owner}/{repo}/deployments", {
...octokitOptions,
ref: config.ref,
environment: config.environment,
production_environment:
config.environment === "prod" || config.environment === "production",
task: "deploy:weco",
mediaType: {
// See preview notice at https://docs.github.com/en/rest/reference/repos#deployments
previews: ["ant-man-preview"],
},
});

if (res.status === 201) {
console.log(`Created deployment at ${res.data.url}`);
} else {
throw new Error(
`Failed to create deployment: ${res.status} ${res.data.message}`
);
}
};

main();
14 changes: 14 additions & 0 deletions src/properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const PLUGIN_NAME = "GITHUB_DEPLOYMENTS";

const envVar = (name: string) =>
`BUILDKITE_PLUGIN_${PLUGIN_NAME}_${name.toUpperCase()}`;

export const environment = {
commit: process.env.BUILDKITE_COMMIT!,
repo: process.env.BUILDKITE_REPO!,
};

export const config = {
environment: process.env[envVar("environment")] || "stage",
ref: process.env[envVar("ref")] || environment.commit,
};
Loading

0 comments on commit 83c4b63

Please sign in to comment.