-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
02b28a0
commit 83c4b63
Showing
5 changed files
with
311 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
Oops, something went wrong.