-
Notifications
You must be signed in to change notification settings - Fork 0
235 lines (188 loc) · 8.58 KB
/
manual-build-and-deploy.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
name: Manually Redeploy portals
on:
workflow_dispatch:
jobs:
collect_dirs:
runs-on: ubuntu-latest
outputs:
dirs: ${{ steps.collect-dirs.outputs.dirs }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Read Redeploy.txt and collect directories
id: collect-dirs
run: |
set -x
# Set the path to the Redeploy.txt file
REDEPLOY_FILE=".github/workflows/manually-deploy-portals.txt"
# Add a newline character to the end of the file
sed -i -e '$a\' "$REDEPLOY_FILE"
# Initialize an empty array for the valid directories
VALID_DIRS=()
# Loop over each line in the text file (without using a pipe)
while IFS= read -r dir; do
echo "Processing directory: $dir"
# Check if the directory exists in the root folder
if [ -d "$dir" ]; then
# Add the valid directory to the array
echo "Adding valid directory: $dir"
VALID_DIRS+=("$dir")
else
echo "Skipping non-existent directory: $dir"
fi
done < "$REDEPLOY_FILE" # Read directly from the file
# Debug output for VALID_DIRS
echo "VALID_DIRS before JSON conversion: ${VALID_DIRS[@]}"
# Convert to JSON format
if [ ${#VALID_DIRS[@]} -eq 0 ]; then
echo "No valid directories found."
DIRS_JSON='[]' # Set to empty JSON array if none found
else
DIRS_JSON=$(printf '%s\n' "${VALID_DIRS[@]}" | jq -R -s -c 'split("\n") | map(select(length > 0))')
fi
echo "Converted to JSON: $DIRS_JSON"
# Output the collected directories
echo "directories=$DIRS_JSON" >> $GITHUB_ENV
echo "::set-output name=dirs::$DIRS_JSON"
build_and_deploy:
needs: collect_dirs
runs-on: ubuntu-latest
strategy:
matrix:
directory: ${{ fromJson(needs.collect_dirs.outputs.dirs) }}
if: ${{ needs.collect_dirs.outputs.dirs != '[]' }} # Only run if directories are found
steps:
- name: Debug Changed Directories
run: |
echo "CHANGED_DIRS: ${{ needs.collect_dirs.outputs.dirs }}"
- name: Checkout code
uses: actions/checkout@v3
- name: Zip BuildFiles and send to APIMatic
continue-on-error: true # Allow deployment to fail without halting the action
run: |
cd ${{ matrix.directory }}
echo "Zipping BuildFiles in ${{ matrix.directory }}..."
zip -r portal-input.zip BuildFiles/* > /dev/null
echo "Zipped BuildFiles successfully."
echo "Sending zip to APIMatic..."
RESPONSE=$(curl -s --output response.zip --write-out '%{http_code}\t%{content_type}' --request POST \
--url 'https://api.apimatic.io/portal' \
-H 'Authorization: X-Auth-Key ${{ secrets.APIMATIC_API_KEY }}' \
-F "[email protected]")
echo "Raw curl response: $RESPONSE"
HTTP_CODE=$(echo "$RESPONSE" | cut -f1)
CONTENT_TYPE=$(echo "$RESPONSE" | cut -f2)
if [ "$HTTP_CODE" -ne 200 ]; then
echo "APIMatic transformer failed with HTTP_CODE=$HTTP_CODE. Exiting."
exit 1
fi
echo "Request successful. Unzipping response..."
mkdir -p Portal
unzip -qq response.zip -d Portal
ls Portal
- name: Add Random Waiting Time
uses: AliSajid/random-wait-action@f9109712daa7a8103f7be16b68634b9d376587a7 # v2.4.1
with:
minimum: 10
maximum: 40
- name: Check and Create Cloudflare Project if Needed
continue-on-error: true # Allow deployment to fail without halting the action
run: |
PROJECT_NAME=$(basename ${{ matrix.directory }})-apimatic-catalog
echo "Checking if Cloudflare project '$PROJECT_NAME' exists..."
PAGE=1
EXISTING_PROJECTS=""
while true; do
# Make the API request for the current page
RESPONSE=$(curl -s --request GET \
--url "https://api.cloudflare.com/client/v4/accounts/${{ secrets.CLOUDFLARE_ACCOUNT_ID }}/pages/projects?page=$PAGE" \
--header 'Content-Type: application/json' \
-H "Authorization: Bearer ${{ secrets.CLOUDFLARE_API_TOKEN }}")
# Append project names from the current page to the variable
PROJECTS=$(echo "$RESPONSE" | jq -r '.result[]?.name // empty')
EXISTING_PROJECTS+="$PROJECTS"$'\n'
# Check if more pages exist
TOTAL_PAGES=$(echo "$RESPONSE" | jq -r '.result_info.total_pages // 0')
if [ "$PAGE" -ge "$TOTAL_PAGES" ]; then
break
fi
PAGE=$((PAGE + 1))
done
# Output all projects
echo "$EXISTING_PROJECTS"
# Check if the current project name exists in the list
if echo "$EXISTING_PROJECTS" | grep -q "^$PROJECT_NAME$"; then
echo "Project '$PROJECT_NAME' already exists on Cloudflare."
else
echo "Project '$PROJECT_NAME' not found. Creating new Cloudflare project..."
# Create the new project
CREATE_RESPONSE=$(curl -s --request POST \
--url https://api.cloudflare.com/client/v4/accounts/${{ secrets.CLOUDFLARE_ACCOUNT_ID }}/pages/projects \
--header 'Content-Type: application/json' \
-H "Authorization: Bearer ${{ secrets.CLOUDFLARE_API_TOKEN }}" \
--data '{
"name": "'"$PROJECT_NAME"'",
"production_branch": "main"
}')
# Check if the project creation was successful
if echo "$CREATE_RESPONSE" | jq -e '.success' > /dev/null; then
echo "Successfully created Cloudflare project '$PROJECT_NAME'."
else
echo "Failed to create Cloudflare project '$PROJECT_NAME'. Response: $CREATE_RESPONSE"
exit 1
fi
fi
- name: Deploy to Cloudflare Pages
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: pages deploy ${{ matrix.directory }}/Portal --project-name=${{ matrix.directory }}-apimatic-catalog
continue-on-error: true # Allow deployment to fail without halting the action
update_readme:
needs: build_and_deploy
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Truncate README.md and Append Filtered Subdomains
run: |
# Truncate README.md after line 87
head -n 87 README.md > temp_README.md && mv temp_README.md README.md
PAGE=1
README_FILE="README.md"
# Add a new line (backslash) to README.md
echo "\\" >> $README_FILE
subdomain_lines=""
while true; do
# Fetch the current page from the API
RESPONSE=$(curl --silent --request GET \
--url "https://api.cloudflare.com/client/v4/accounts/${{ secrets.CLOUDFLARE_ACCOUNT_ID }}/pages/projects?page=$PAGE" \
--header 'Content-Type: application/json' \
-H "Authorization: Bearer ${{ secrets.CLOUDFLARE_API_TOKEN }}")
# Extract subdomains that contain "-apimatic-catalog" and format them
subdomain_lines+=$(echo "$RESPONSE" | \
jq -r '.result[] | select(.subdomain | contains("-apimatic-catalog")) | .subdomain' | \
sed 's/^/* <http:\/\//' | sed 's/$/>/'$'\n')
# Check pagination
TOTAL_PAGES=$(echo "$RESPONSE" | jq '.result_info.total_pages')
if [ "$PAGE" -ge "$TOTAL_PAGES" ]; then
break
fi
# Move to the next page
PAGE=$((PAGE + 1))
done
# Append to README.md without a trailing slash
echo -e "$subdomain_lines" >> $README_FILE
# Output the updated README for debugging purposes
echo "Updated README.md content:"
cat $README_FILE
- name: Commit and Push the Updated README.md
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git add README.md
git commit -m "Update README.md with filtered Cloudflare subdomains"
git push
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}