generated from int128/typescript-actions-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support GitHub Deployment (environment-matrix)
- Loading branch information
Showing
12 changed files
with
237 additions
and
40 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
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
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,73 @@ | ||
import * as core from '@actions/core' | ||
import * as github from '@actions/github' | ||
import { Environment } from './rule' | ||
import { RequestError } from '@octokit/request-error' | ||
import { Octokit, assertPullRequestPayload } from './github' | ||
import assert from 'assert' | ||
|
||
type Context = Pick<typeof github.context, 'eventName' | 'repo' | 'ref' | 'payload'> | ||
|
||
export const createGitHubDeploymentForEnvironments = async ( | ||
octokit: Octokit, | ||
context: Context, | ||
environments: Environment[], | ||
) => { | ||
for (const environment of environments) { | ||
if (environment['github-deployment'] === 'true' && environment['github-deployment-environment']) { | ||
const deploymentEnvironment = environment['github-deployment-environment'] | ||
const deployment = await createDeployment(octokit, context, deploymentEnvironment) | ||
environment['github-deployment-url'] = deployment.url | ||
} | ||
} | ||
} | ||
|
||
const createDeployment = async (octokit: Octokit, context: Context, deploymentEnvironment: string) => { | ||
core.info(`Finding the old deployments for environment ${deploymentEnvironment}`) | ||
const oldDeployments = await octokit.rest.repos.listDeployments({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
environment: deploymentEnvironment, | ||
}) | ||
|
||
core.info(`Deleting ${oldDeployments.data.length} deployment(s)`) | ||
for (const deployment of oldDeployments.data) { | ||
try { | ||
await octokit.rest.repos.deleteDeployment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
deployment_id: deployment.id, | ||
}) | ||
core.info(`Deleted the old deployment ${deployment.url}`) | ||
} catch (error) { | ||
if (error instanceof RequestError) { | ||
core.warning(`Unable to delete previous deployment ${deployment.url}: ${error.status} ${error.message}`) | ||
continue | ||
} | ||
throw error | ||
} | ||
} | ||
|
||
const deploymentRef = getDeploymentRef(context) | ||
core.info(`Creating a deployment for environment=${deploymentEnvironment}, ref=${deploymentRef}`) | ||
const created = await octokit.rest.repos.createDeployment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
ref: deploymentRef, | ||
environment: deploymentEnvironment, | ||
auto_merge: false, | ||
required_contexts: [], | ||
transient_environment: context.eventName === 'pull_request', | ||
}) | ||
assert.strictEqual(created.status, 201) | ||
core.info(`Created a deployment ${created.data.url}`) | ||
return created.data | ||
} | ||
|
||
const getDeploymentRef = (context: Context): string => { | ||
if (context.eventName === 'pull_request') { | ||
// Set the head ref to associate a deployment with the pull request | ||
assertPullRequestPayload(context.payload.pull_request) | ||
return context.payload.pull_request.head.ref | ||
} | ||
return context.ref | ||
} |
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,37 @@ | ||
import assert from 'assert' | ||
import * as pluginRetry from '@octokit/plugin-retry' | ||
import { GitHub, getOctokitOptions } from '@actions/github/lib/utils' | ||
|
||
export type Octokit = InstanceType<typeof GitHub> | ||
|
||
export const getOctokit = (token: string): Octokit => { | ||
const MyOctokit = GitHub.plugin(pluginRetry.retry) | ||
return new MyOctokit(getOctokitOptions(token, { previews: ['ant-man', 'flash'] })) | ||
} | ||
|
||
// picked from https://docs.github.com/en/rest/pulls/pulls#get-a-pull-request | ||
export type PullRequestPayload = { | ||
head: { | ||
ref: string | ||
} | ||
base: { | ||
ref: string | ||
} | ||
} | ||
|
||
export function assertPullRequestPayload(x: unknown): asserts x is PullRequestPayload { | ||
assert(typeof x === 'object') | ||
assert(x != null) | ||
|
||
assert('base' in x) | ||
assert(typeof x.base === 'object') | ||
assert(x.base != null) | ||
assert('ref' in x.base) | ||
assert(typeof x.base.ref === 'string') | ||
|
||
assert('head' in x) | ||
assert(typeof x.head === 'object') | ||
assert(x.head != null) | ||
assert('ref' in x.head) | ||
assert(typeof x.head.ref === 'string') | ||
} |
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
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
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
Oops, something went wrong.