Cloud Cleanup #950
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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) }' |