-
Notifications
You must be signed in to change notification settings - Fork 2
/
action.yml
180 lines (160 loc) · 6.22 KB
/
action.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
name: Cloudflare Pages Action
description: Deploy your project to Cloudflare Pages with automatic project creation and custom domain
author: Ade Hery Shopyan
branding:
icon: 'upload-cloud'
color: 'blue'
inputs:
api-token:
required: true
description: "Set api token"
type: string
account-id:
required: true
description: "Set account id"
type: string
branch:
required: true
description: "Set branch"
type: string
production-branch:
required: true
description: "Set production branch"
type: string
build-directory:
required: true
description: "Set build directory"
type: string
package-manager:
required: true
description: "Set package manager"
type: string
project-name:
required: true
description: "Set project name"
type: string
zone-name:
required: false
description: "Set zone name"
default: ""
type: string
custom-domain:
required: false
description: "Set custom domain"
default: ""
type: string
working-directory:
required: false
description: "Set working directory"
default: "."
type: string
outputs:
deployment-url:
description: "Define deployment url"
value: ${{ steps.url.outputs.deployment-url }}
pages-url:
description: "Define pages url"
value: ${{ steps.url.outputs.pages-url }}
runs:
using: composite
steps:
- name: Deploy
id: deploy
# use specific wrangler-action version to handle this issue
# https://github.com/cloudflare/wrangler-action/issues/306
uses: cloudflare/[email protected]
with:
workingDirectory: ${{ inputs.working-directory }}
apiToken: ${{ inputs.api-token }}
accountId: ${{ inputs.account-id }}
preCommands: wrangler pages project create ${{ inputs.project-name }} --production-branch=${{ inputs.production-branch }} || true
command: pages deploy ${{ inputs.build-directory }} --project-name=${{ inputs.project-name }} --branch=${{ inputs.branch }} --commit-dirty=true
packageManager: ${{ inputs.package-manager }}
- name: Get Cloudflare Zone ID
id: get-zone-id
if: "${{ inputs.zone-name != '' }}"
shell: bash
env:
CLOUDFLARE_API_TOKEN: ${{ inputs.api-token }}
ZONE_DOMAIN: ${{ inputs.zone-name }}
run: |
response=$(curl -X GET "https://api.cloudflare.com/client/v4/zones?name=$ZONE_DOMAIN" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json")
zone_id=$(echo $response | jq -r '.result[0].id')
echo "id=$zone_id" >> $GITHUB_OUTPUT
- name: Set Cloudflare DNS Record & Custom Domain
id: dnsrecord
if: "${{ steps.get-zone-id.outputs.id != 'null' && inputs.zone-name != '' && inputs.custom-domain != '' }}"
shell: bash
env:
CLOUDFLARE_API_TOKEN: ${{ inputs.api-token }}
CLOUDFLARE_ACCOUNT_ID: ${{ inputs.account-id }}
CLOUDFLARE_ZONE_ID: ${{ steps.get-zone-id.outputs.id }}
PROJECT_NAME: ${{ inputs.project-name }}
CUSTOM_DOMAIN: ${{ inputs.custom-domain }}
run: |
echo "Check DNS record exist or not for the domain: ${CUSTOM_DOMAIN}"
res_record=$(curl -X GET "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/dns_records?type=CNAME&name=$CUSTOM_DOMAIN" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json")
dns_record_id=$(echo $res_record | jq -r '.result[0].id')
if [ "$dns_record_id" == "null" ] || [ -z "$dns_record_id" ]; then
echo "DNS record not found for the domain: ${CUSTOM_DOMAIN}"
response=$(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": "'"$CUSTOM_DOMAIN"'",
"content": "'"$PROJECT_NAME"'.pages.dev",
"ttl": 1,
"proxied": true
}')
id=$(echo $response | jq -r '.result.id')
echo "id=$id" >> $GITHUB_OUTPUT
echo "Set Cloudflare Pages Custom Domain for: ${CUSTOM_DOMAIN}"
curl -X POST "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/pages/projects/$PROJECT_NAME/domains" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json" \
--data '{"name": "'$CUSTOM_DOMAIN'"}'
else
echo "DNS record found for the domain: ${CUSTOM_DOMAIN}"
echo "id=$dns_record_id" >> $GITHUB_OUTPUT
fi
- name: Update Cloudflare DNS Record
if: "${{ steps.get-zone-id.outputs.id != 'null' && inputs.zone-name != '' && inputs.custom-domain != '' && inputs.branch != inputs.production-branch }}"
shell: bash
env:
CLOUDFLARE_API_TOKEN: ${{ inputs.api-token }}
CLOUDFLARE_ZONE_ID: ${{ steps.get-zone-id.outputs.id }}
CUSTOM_DOMAIN: ${{ inputs.custom-domain }}
DNS_RECORD_ID: ${{ steps.dnsrecord.outputs.id }}
PAGES_URL: ${{ steps.deploy.outputs.deployment-url }}
run: |
DNS_RECORD_CONTENT=$(echo "$PAGES_URL" | sed 's/^https:\/\///')
curl -X PATCH "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/dns_records/$DNS_RECORD_ID" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json" \
--data '{
"type": "CNAME",
"name": "'"$CUSTOM_DOMAIN"'",
"content": "'"$DNS_RECORD_CONTENT"'",
"id": "'"$DNS_RECORD_ID"'",
"ttl": 1,
"proxied": true
}'
- name: Get deployment url
id: url
shell: bash
env:
DEPLOYMENT_URL: ${{ inputs.custom-domain }}
PAGES_URL: ${{ steps.deploy.outputs.deployment-url }}
run: |
if [ "$DEPLOYMENT_URL" != "" ]; then
echo "deployment-url=https://$DEPLOYMENT_URL" >> $GITHUB_OUTPUT
echo "pages-url=$PAGES_URL" >> $GITHUB_OUTPUT
else
echo "deployment-url=$PAGES_URL" >> $GITHUB_OUTPUT
echo "pages-url=$PAGES_URL" >> $GITHUB_OUTPUT
fi