Skip to content

Commit

Permalink
Undeploy without Environment cleanup (github#21530)
Browse files Browse the repository at this point in the history
* Ensure we handle multiple pages of Deployments
* Do NOT delete the Environment during undeploy
* Do NOT deploy/undeploy PRs on lock/unlock events
  • Loading branch information
JamesMGreene authored Sep 15, 2021
1 parent 2912ae7 commit 7ae1eea
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 25 deletions.
1 change: 0 additions & 1 deletion .github/workflows/staging-build-pr-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ on:
- opened
- reopened
- synchronize
- unlocked

permissions:
contents: read
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/staging-build-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ on:
- opened
- reopened
- synchronize
- unlocked

permissions:
contents: read
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/staging-undeploy-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ on:
pull_request_target:
types:
- closed
- locked

permissions:
contents: read
Expand Down
36 changes: 14 additions & 22 deletions script/deployment/undeploy-from-staging.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,21 @@ export default async function undeployFromStaging({
// that checks for stale PRs. This way, we aren't doing more cleaning than
// necessary if someone intends to reopen the PR momentarily.
if (wasMerged) {
// Get the latest deployment environment to signal its deactivation
const { data: deployments } = await octokit.repos.listDeployments({
// Get all of the Deployments to signal this environment's complete deactivation
for await (const response of octokit.paginate.iterator(octokit.repos.listDeployments, {
owner,
repo,

// In the GitHub API, there can only be one active deployment per environment.
// For our many staging apps, we must use the unique appName as the environment.
environment: appName,
})
})) {
const { data: deployments } = response

if (deployments.length === 0) {
console.log('πŸš€ No deployments to deactivate!')
} else {
console.log(`Found ${deployments.length} GitHub Deployments`, deployments)
console.log(
`Found ${deployments.length} GitHub Deployments for Environment ${appName}`,
deployments
)

// Deactivate ALL of the deployments
for (const deployment of deployments) {
Expand Down Expand Up @@ -106,21 +107,12 @@ export default async function undeployFromStaging({
}
}

// Delete this Environment
try {
await octokit.repos.deleteAnEnvironment({
owner,
repo,
environment_name: appName,
})
console.log(`πŸš€ Environment (${appName}): deleted`)
} catch (error) {
if (error.status === 404) {
console.log(`πŸš€ Environment (${appName}): already deleted`)
} else {
throw error
}
}
// IMPORTANT:
// We will leave the Deployment Environment to be cleaned up later by the
// workflow that checks for stale PRs. This way, we are not doing more
// cleaning than necessary if someone intends to reopen the PR momentarily,
// and we do not need to use an admin PAT to run this script.
console.log(`πŸš€ Environment (${appName}) is ready to be removed (later...)`)
}

console.log(`Finished undeploying after ${Math.round((Date.now() - startTime) / 1000)} seconds`)
Expand Down

0 comments on commit 7ae1eea

Please sign in to comment.