-
Notifications
You must be signed in to change notification settings - Fork 135
56 lines (48 loc) · 1.67 KB
/
cleanup.yml
File metadata and controls
56 lines (48 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
name: 'Cleanup'
on:
pull_request:
paths:
- '.github/workflows/cleanup.yml'
push:
paths:
- '.github/workflows/cleanup.yml'
schedule:
- cron: '0 */6 * * *'
workflow_dispatch:
permissions:
contents: 'read'
id-token: 'write'
jobs:
cleanup:
runs-on: 'ubuntu-latest'
strategy:
fail-fast: false
matrix:
target:
- 'jobs'
- 'services'
name: 'Cleanup (${{ matrix.target }})'
steps:
- uses: 'actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683' # ratchet:actions/checkout@v4
- uses: 'google-github-actions/auth@v3' # ratchet:exclude
with:
workload_identity_provider: '${{ vars.WIF_PROVIDER_NAME }}'
service_account: '${{ vars.SERVICE_ACCOUNT_EMAIL }}'
- uses: 'google-github-actions/setup-gcloud@v2' # ratchet:exclude
with:
version: 'latest'
- name: 'Delete ${{ matrix.target }}'
env:
PROJECT_ID: '${{ vars.PROJECT_ID }}'
TARGET: '${{ matrix.target }}'
run: |-
gcloud config set core/project "${PROJECT_ID}"
gcloud config set run/region "us-central1"
# List and delete all jobs/services that were deployed 30 minutes ago
# or earlier. The date math here is a little weird, but we're looking
# for deployments "earlier than" 30 minutes ago, so it's less than
# since time increases.
(IFS=$'\n'; for NAME in $(gcloud run "${TARGET}" list --format='value(name)' --filter='metadata.creationTimestamp < "-pt30m"'); do
echo "Deleting ${NAME}..."
gcloud run "${TARGET}" delete "${NAME}" --quiet --async
done)