Set CNAME Record for gen.dev #6
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: Create or Modify CNAME Record in CloudFlare | |
on: | |
workflow_dispatch: | |
inputs: | |
subdomain: | |
description: 'The subdomain' | |
required: true | |
type: string | |
target: | |
description: 'The CNAME target' | |
required: true | |
type: string | |
jobs: | |
create-or-modify-cname: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Create or Modify CNAME Record in CloudFlare | |
env: | |
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
CLOUDFLARE_ZONE_ID: ${{ secrets.CLOUDFLARE_ZONE_ID }} | |
run: | | |
SUBDOMAIN=${{ github.event.inputs.subdomain }} | |
TARGET=${{ github.event.inputs.target }} | |
echo "Checking if the CNAME record already exists for subdomain: ${SUBDOMAIN}" | |
RECORD_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE_ID}/dns_records?type=CNAME&name=${SUBDOMAIN}.gen.dev" \ | |
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \ | |
-H "Content-Type: application/json" | jq -r '.result[] | select(.name=="'"${SUBDOMAIN}.gen.dev"'") | .id') | |
if [ -z "$RECORD_ID" ]; then | |
echo "No existing CNAME record found. Creating a new CNAME record for subdomain: ${SUBDOMAIN}" | |
curl -X POST "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE_ID}/dns_records" \ | |
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \ | |
-H "Content-Type: application/json" \ | |
--data '{ | |
"type": "CNAME", | |
"name": "'"${SUBDOMAIN}"'", | |
"content": "'"${TARGET}"'", | |
"ttl": 3600, | |
"proxied": false | |
}' | |
else | |
echo "Existing CNAME record found with ID: ${RECORD_ID}. Updating the CNAME record for subdomain: ${SUBDOMAIN}" | |
curl -X PUT "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE_ID}/dns_records/${RECORD_ID}" \ | |
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \ | |
-H "Content-Type: application/json" \ | |
--data '{ | |
"type": "CNAME", | |
"name": "'"${SUBDOMAIN}"'", | |
"content": "'"${TARGET}"'", | |
"ttl": 3600, | |
"proxied": false | |
}' | |
fi | |