Skip to content

Commit 22401bf

Browse files
Destroy hanging preview environments after 12 hours of inactivity (CopilotKit#1300)
1 parent 9b45224 commit 22401bf

3 files changed

Lines changed: 77 additions & 21 deletions

File tree

.github/workflows/destroy-inactive-preview-environments.yml

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ on:
66
workflow_dispatch:
77

88
jobs:
9-
deploy:
9+
destroy:
1010
name: Destroy Inactive Preview Environments
1111
runs-on: ubuntu-20.04
1212
outputs:
13-
e2e-urls: ${{ steps.export-e2e-urls.outputs.e2e-urls }}
13+
destroyed-pr-numbers: ${{ steps.delete-inactive-preview-environments.outputs.destroyed-pr-numbers }}
1414
defaults:
1515
run:
1616
working-directory: examples/helmfile
@@ -51,23 +51,32 @@ jobs:
5151
id: delete-inactive-preview-environments
5252
run: |
5353
node destroy-inactive-preview-environments.js
54-
55-
# - name: Delete a comment
56-
# if: github.event_name == 'pull_request'
57-
# uses: thollander/actions-comment-pull-request@v3
58-
# with:
59-
# comment-tag: preview-status-update
60-
# mode: delete
54+
echo "destroyed-pr-numbers=$(cat inactive-pull-requests.json)" >> "$GITHUB_OUTPUT"
6155
62-
# - name: Comment post-deployment
63-
# if: github.event_name == 'pull_request'
64-
# uses: thollander/actions-comment-pull-request@v3
65-
# with:
66-
# comment-tag: preview-status-update
67-
# file-path: ./examples/helmfile/preview-comment.md
56+
pr-comments:
57+
needs: destroy
58+
name: Comment on PRs
59+
runs-on: ubuntu-20.04
60+
strategy:
61+
matrix:
62+
pr-number: ${{ fromJSON(needs.destroy.outputs.destroyed-pr-numbers) }}
63+
steps:
64+
- name: Comment on PRs
65+
run: |
66+
echo "PR number: ${{ matrix.pr-number }}"
6867
69-
# - name: Export Variable - E2E URLs
70-
# id: export-e2e-urls
71-
# working-directory: examples/e2e
72-
# run: |
73-
# echo "e2e-urls=$(cat test-config.json | base64 -w 0)" >> "$GITHUB_OUTPUT"
68+
- name: Delete a comment
69+
uses: thollander/actions-comment-pull-request@v3
70+
with:
71+
comment-tag: preview-status-update
72+
mode: delete
73+
pr-number: ${{ matrix.pr-number }}
74+
75+
- name: Comment
76+
uses: thollander/actions-comment-pull-request@v3
77+
with:
78+
comment-tag: preview-status-update
79+
pr-number: ${{ matrix.pr-number }}
80+
message: |
81+
The preview environment for this PR has been deleted because it has been inactive for more than 12 hours.
82+
To recreate the preview environment, you can push a new change to the PR.

examples/helmfile/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
preview-comment.md
1+
preview-comment.md
2+
inactive-pull-requests.json
3+
override-values.yaml
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const childProcess = require("child_process");
2+
const fs = require("fs");
3+
4+
async function main() {
5+
const releaseList = JSON.parse(
6+
childProcess.execSync(`helm list --all-namespaces -o json`).toString()
7+
);
8+
9+
const previewReleases = releaseList
10+
.filter((release) => release.namespace.startsWith("copilotkit-examples"))
11+
.filter((release) => release.name === "next-openai")
12+
.filter((release) => release.namespace !== "copilotkit-examples-main");
13+
14+
const inactiveReleases = previewReleases.filter(
15+
(release) => {
16+
const now = new Date();
17+
const releaseUpdatedAt = new Date(release.updated);
18+
const compareDate = new Date(now.getTime() - 12 * 60 * 60 * 1000); // 12 hours ago
19+
return releaseUpdatedAt < compareDate;
20+
}
21+
);
22+
23+
const numReleasesToDelete = inactiveReleases.length;
24+
25+
console.log(`Found ${numReleasesToDelete} inactive releases to delete`);
26+
27+
const pullRequestNumbers = [];
28+
29+
for (const release of inactiveReleases) {
30+
const environment = release.namespace.replace("copilotkit-examples-", "");
31+
const pullRequestNumber = environment.replace("pr-", "");
32+
const namespace = release.namespace;
33+
console.log(`Deleting environment ${environment} in namespace ${namespace}`);
34+
childProcess.execSync(`helmfile --state-values-set environment=${environment},namespace=${namespace} destroy`);
35+
childProcess.execSync(`kubectl delete namespace ${release.namespace}`);
36+
pullRequestNumbers.push(pullRequestNumber);
37+
}
38+
39+
console.log(`Deleted ${numReleasesToDelete} inactive releases`);
40+
41+
// Write to local file - inactive-pull-requests.json
42+
fs.writeFileSync("inactive-pull-requests.json", JSON.stringify(pullRequestNumbers));
43+
}
44+
45+
main();

0 commit comments

Comments
 (0)