-
Notifications
You must be signed in to change notification settings - Fork 0
61 lines (54 loc) · 2.28 KB
/
set_cname.yml
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
57
58
59
60
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