-
Notifications
You must be signed in to change notification settings - Fork 75
56 lines (52 loc) · 2.08 KB
/
cloud-cleanup.yaml
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: Cloud Cleanup
on:
schedule:
- cron: '42 2 * * *'
defaults:
run:
shell: sh -ex {0}
jobs:
check-secrets:
uses: ./.github/workflows/check-secrets-available.yaml
secrets:
# Use PYCLOUDLIB_CONFIG_CONTENTS as a flag for "secrets present".
SECRET_TO_CHECK: '${{ secrets.PYCLOUDLIB_CONFIG_CONTENTS }}'
cleanup-ec2:
name: Cleanup EC2
needs: check-secrets
if: ${{ needs.check-secrets.outputs.has-secrets == 'true' }}
runs-on: ubuntu-latest
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-2
- name: Delete stale CI instances
run: |
yesterday=$(date --utc --iso-8601=seconds --date=yesterday)
current_aws_principal=$(aws sts get-caller-identity | jq -r '.UserId')
stale_instances=$(
aws ec2 describe-instances \
--query "Reservations[].Instances[?LaunchTime<=\`$yesterday\`][].InstanceId" \
--filters "Name=tag:PrincipalId,Values=$current_aws_principal" 'Name=tag:Name,Values=uaclient-ci-*' \
--output text
)
[ -z "$stale_instances" ] || aws ec2 terminate-instances --instance-ids $stale_instances
cleanup-gce:
name: Cleanup GCE
runs-on: ubuntu-latest
steps:
- name: Configure GCE credentials
uses: 'google-github-actions/auth@v0'
with:
credentials_json: '${{ secrets.GOOGLE_APPLICATION_CREDENTIALS_CONTENTS }}'
- name: 'Set up GCloud SDK'
uses: 'google-github-actions/setup-gcloud@v0'
- name: Delete stale CI instances
run: |
export CLOUDSDK_CORE_DISABLE_PROMPTS=1
yesterday=$(date --utc --iso-8601=seconds --date=yesterday)
gcloud compute instances list --format="value(name,zone)" --filter="creationTimestamp<=$yesterday" --filter="name ~ .*uaclient-ci.*" | awk '{ system("gcloud compute instances delete --zone="$2 " " $1) }'