Manual DNS Cleanup #2
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: "Manual DNS Cleanup" | |
on: | |
workflow_dispatch | |
jobs: | |
DNS_Cleanup: | |
runs-on: "ubuntu-latest" | |
steps: | |
- name: "Checkout repository" | |
uses: "actions/checkout@v4" | |
- name: "Run Cleanup via Bash" | |
shell: "bash" | |
run: | | |
date=$(date) | |
authemail="${{ secrets.LF_DNS_AUTH_EMAIL }}" | |
apitoken="${{ secrets.LF_DNS_API }}" | |
#Get DNS Record IDs | |
DNSIDs=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/08be24924fc30f320e7329020986bad2/dns_records?per_page=1000" -H "X-Auth-Email: $authemail" -H "Authorization: Bearer $apitoken" -H "Content-Type: application/json" | jq -r '.result[] | .id') | |
printf "\nFound $(echo -n "$DNSIDs" | grep -c '^') IDs to process\n" | |
for id in $DNSIDs | |
do | |
#printf "\nChecking DNS Record with ID:$id\n" | |
DNSRecord=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/08be24924fc30f320e7329020986bad2/dns_records/$id" -H "X-Auth-Email: $authemail" -H "Authorization: Bearer $apitoken" -H "Content-Type: application/json") | |
DNSContent=$(echo $DNSRecord | jq -r '.result | .content') | |
#Logic for Naming Convention goes here. We want to leave our other DNS records alone. Crazy {^^} converts to upper. It's a happy thing. | |
if [[ "${DNSContent}" == *".instruqt.io"* ]]; then | |
#printf "Record Matches Training Naming Convention\n" | |
DNSDateExp=$(date -d "$(echo $DNSRecord | jq -r '.result | .created_on')+7days") | |
#printf "DNS Name:$DNSContent \nDNS Expiry Date:$DNSDateExp\nCurrent Date:$date" | |
#Compare Dates | |
if [ $(date -d "$date" +%s) -gt $(date -d "$DNSDateExp" +%s) ]; then | |
printf "\nRecord $DNSContent is older than 7 days. Deleting record.\n" | |
DeleteRecord=$(curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/08be24924fc30f320e7329020986bad2/dns_records/$id" -H "X-Auth-Email: $authemail" -H "Authorization: Bearer $apitoken" -H "Content-Type: application/json") | |
fi | |
fi | |
done |