Skip to content

Commit 4679e96

Browse files
committed
ci: auto-create DNS CNAME record for custom domain
Requires CLOUDFLARE_ZONE_ID secret to create DNS records automatically
1 parent c8241c1 commit 4679e96

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

.github/workflows/deploy.yml

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,40 @@ jobs:
5252
env:
5353
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
5454
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
55+
CLOUDFLARE_ZONE_ID: ${{ secrets.CLOUDFLARE_ZONE_ID }}
5556
run: |
56-
# Check if custom domain already configured
57+
# Add domain to Pages project
5758
EXISTING=$(curl -s -X GET \
5859
"https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/pages/projects/$PROJECT_NAME/domains" \
5960
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
6061
-H "Content-Type: application/json" | jq -r '.result[].name' | grep -x "$CUSTOM_DOMAIN" || true)
6162
6263
if [ -z "$EXISTING" ]; then
63-
echo "Adding custom domain: $CUSTOM_DOMAIN"
64+
echo "Adding custom domain to Pages: $CUSTOM_DOMAIN"
6465
curl -s -X POST \
6566
"https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/pages/projects/$PROJECT_NAME/domains" \
6667
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
6768
-H "Content-Type: application/json" \
6869
--data "{\"name\":\"$CUSTOM_DOMAIN\"}"
6970
else
70-
echo "Custom domain $CUSTOM_DOMAIN already configured"
71+
echo "Custom domain $CUSTOM_DOMAIN already in Pages"
72+
fi
73+
74+
# Create DNS CNAME record if zone ID provided
75+
if [ -n "$CLOUDFLARE_ZONE_ID" ]; then
76+
DNS_EXISTS=$(curl -s -X GET \
77+
"https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/dns_records?name=$CUSTOM_DOMAIN" \
78+
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
79+
-H "Content-Type: application/json" | jq -r '.result | length')
80+
81+
if [ "$DNS_EXISTS" = "0" ]; then
82+
echo "Creating DNS CNAME record for $CUSTOM_DOMAIN"
83+
curl -s -X POST \
84+
"https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/dns_records" \
85+
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
86+
-H "Content-Type: application/json" \
87+
--data "{\"type\":\"CNAME\",\"name\":\"@\",\"content\":\"$PROJECT_NAME.pages.dev\",\"proxied\":true}"
88+
else
89+
echo "DNS record for $CUSTOM_DOMAIN already exists"
90+
fi
7191
fi

0 commit comments

Comments
 (0)