Skip to content

Commit c1fe3aa

Browse files
committed
feat(deployment/delete): optimize delete of review apps environments
Signed-off-by: Emilien Escalle <[email protected]>
1 parent 060859d commit c1fe3aa

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

.github/workflows/__main-ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ concurrency:
2121
jobs:
2222
ci:
2323
uses: ./.github/workflows/__shared-ci.yml
24+
secrets: inherit
2425

2526
release:
2627
needs: ci

.github/workflows/__pull-request-ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ permissions:
1212
packages: write
1313
pull-requests: read
1414
statuses: write
15+
deployments: write
1516
# FIXME: This is a workaround for having workflow actions. See https://github.com/orgs/community/discussions/38659
1617
id-token: write
1718

@@ -22,3 +23,4 @@ concurrency:
2223
jobs:
2324
ci:
2425
uses: ./.github/workflows/__shared-ci.yml
26+
secrets: inherit

.github/workflows/__shared-ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ jobs:
1616
test-action-docker-build-image:
1717
needs: linter
1818
uses: ./.github/workflows/__test-action-deployment.yml
19+
secrets: inherit

.github/workflows/__test-action-deployment.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,16 @@ jobs:
3232
deployment-id: ${{ steps.create-deployment.outputs.deployment-id }}
3333
state: "in_progress"
3434

35+
- uses: actions/create-github-app-token@v1
36+
id: generate-token
37+
with:
38+
app-id: ${{ vars.CI_BOT_APP_ID }}
39+
private-key: ${{ secrets.CI_BOT_APP_PRIVATE_KEY }}
40+
3541
- id: delete-deployment
3642
uses: ./actions/deployment/delete
43+
with:
44+
token: ${{ steps.generate-token.outputs.token }}
3745

3846
- name: Check delete outputs
3947
run: |

actions/deployment/delete/action.yml

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
name: "Delete deployment(s)"
2-
description: "Action to delete some deployment(s) for the current ref. It also deletes the associated environment(s) if it is a review-app"
2+
description: "Action to delete some deployment(s) for the current ref. It also deletes the associated review apps environment(s) if any."
33
author: Hoverkraft
44
branding:
55
icon: trash-2
66
color: gray-dark
77

88
inputs:
99
token:
10-
description: "The token to use to delete the environment(s)"
10+
description: "The token to use to delete the review apps environment(s). It needs the `repo` scope."
1111
required: false
1212

1313
outputs:
1414
deployment-ids:
15-
description: "The id(s) of the deleted deployment(s). Json array format"
15+
description: "The id(s) of the deleted deployment(s). Json array format."
1616
value: ${{ steps.delete-deployments.outputs.deployment-ids }}
1717
environments:
18-
description: "The name(s) of the environment(s) related to deleted deployment(s). Json array format"
18+
description: "The name(s) of the environment(s) related to deleted deployment(s). Json array format."
1919
value: ${{ steps.delete-deployments.outputs.environments }}
2020

2121
runs:
@@ -66,23 +66,23 @@ runs:
6666
environments.add(deployment.environment);
6767
}
6868
69+
const reviewAppsEnvironments = Array.from(environments).filter((environment) => environment.startsWith('review-apps'));
70+
6971
core.setOutput('deployment-ids', JSON.stringify(deploymentIds));
7072
core.setOutput('environments', JSON.stringify(Array.from(environments)));
73+
core.setOutput('review-apps-environments', JSON.stringify(reviewAppsEnvironments));
7174
72-
- id: delete-environments
75+
- id: delete-review-apps-environments
76+
if: steps.delete-deployments.outputs.review-apps-environments != '[]'
7377
uses: actions/github-script@v7
7478
with:
7579
github-token: ${{ inputs.token }}
7680
script: |
77-
const environments = JSON.parse(core.getInput('environments'));
78-
79-
// Clean up the environment if it is a review-app
80-
for (const environment of environments) {
81-
if (environment.startsWith('review-apps')) {
82-
await github.rest.repos.deleteAnEnvironment({
83-
owner: context.repo.owner,
84-
repo: context.repo.repo,
85-
environment_name: environment,
86-
});
87-
}
81+
const reviewAppsEnvironments = JSON.parse(`${{ steps.delete-deployments.outputs.review-apps-environments }}`);
82+
for (const environment of reviewAppsEnvironments) {
83+
await github.rest.repos.deleteAnEnvironment({
84+
owner: context.repo.owner,
85+
repo: context.repo.repo,
86+
environment_name: environment,
87+
});
8888
}

0 commit comments

Comments
 (0)