Add exponential backoff to Cloudflare API calls #121
Workflow file for this run
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: dns | |
on: | |
pull_request: | |
branches: | |
- master | |
jobs: | |
dns: | |
env: | |
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }} | |
CLOUDFLARE_API_TOKEN: ${{ secrets.CF_ACCOUNT_TOKEN }} | |
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} | |
WRANGLER_SEND_METRICS: false | |
OWNER: ${{ github.repository_owner }} | |
REPOSITORY: ${{ github.repository }} | |
BRANCH: ${{ github.head_ref }} | |
CUSTOM_DOMAIN: nephelai.io | |
if: github.event.pull_request.type != 'closed' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Install NodeJS | |
uses: actions/setup-node@v3 | |
with: | |
node-version: 18 | |
- name: Install utilities | |
run: sudo apt-get install -y jq httpie make | |
- name: Build project | |
run: make build | |
- name: Set worker name envvar | |
run: echo "WORKER=${REPOSITORY/$OWNER\//}-${BRANCH}-${GITHUB_WORKFLOW}" >> $GITHUB_ENV | |
- name: Set worker fqdn envvar | |
run: echo "FQDN=${WORKER}.${CUSTOM_DOMAIN}" >> $GITHUB_ENV | |
- name: Set worker dns content envvar | |
run: echo "CONTENT=8.8.8.8" >> $GITHUB_ENV | |
- name: Clean previous records for custom domain | |
run: > | |
records=$(npx cfcli --domain ${CUSTOM_DOMAIN} --type A --format json find ${FQDN} ${CONTENT} | jq '. | length' -r); | |
if [[ $records -gt 0 ]]; then | |
npx cfcli --domain ${CUSTOM_DOMAIN} find ${FQDN} --format json | | |
jq '. | map(("--type " + .type + " " + .name + " " + .content)) | .[]' -r | | |
xargs npx cfcli --domain ${CUSTOM_DOMAIN} rm | |
fi | |
env: | |
CF_API_KEY: ${{ secrets.CF_ACCOUNT_TOKEN }} | |
- name: Deploy Cloudflare Worker | |
uses: mathiasvr/command-output@v2.0.0 | |
with: | |
run: | | |
echo Deploying worker "$WORKER"; \ | |
make run -- deploy \ | |
--verbose \ | |
--literal GITHUB_APPLY:true \ | |
--variable WORKER \ | |
--secret GITHUB_TOKEN \ | |
--name "$WORKER"; | |
- name: Update Cloudflare worker with custom domain | |
uses: mathiasvr/command-output@v2.0.0 | |
with: | |
run: | | |
echo Deploying worker "$WORKER"; \ | |
make run -- deploy \ | |
--verbose \ | |
--literal GITHUB_APPLY:true \ | |
--variable WORKER \ | |
--secret GITHUB_TOKEN \ | |
--name "$WORKER" \ | |
--route "$FQDN/*"; | |
- name: Test custom domain resolution | |
run: > | |
retries=6; | |
sleep=30; | |
while [[ $retries > 0 ]]; do | |
retries=$(( retries - 1 )); | |
address="$(dig +short $FQDN @1.1.1.1 | head -1)"; | |
if [ -z "$address" ]; then | |
echo "dns query for record $FQDN returned NXDOMAIN"; | |
sleep $sleep; | |
else | |
echo "dns query for record $FQDN returned $address"; | |
exit 0; | |
fi; | |
done; | |
echo "dns record $FQDN was not created on time"; | |
exit 1; | |
- name: Remove custom domain from Cloudflare worker | |
uses: mathiasvr/command-output@v2.0.0 | |
with: | |
run: | | |
echo Deploying worker "$WORKER"; \ | |
make run -- deploy \ | |
--verbose \ | |
--literal GITHUB_APPLY:true \ | |
--variable WORKER \ | |
--secret GITHUB_TOKEN \ | |
--name "$WORKER"; | |
- name: Test custom domain resolution | |
run: > | |
retries=6; | |
sleep=30; | |
while [[ $retries > 0 ]]; do | |
retries=$(( retries - 1 )); | |
address="$(dig +short $FQDN @1.1.1.1 | head -1)"; | |
if [ -z "$address" ]; then | |
echo "dns query for record $FQDN returned NXDOMAIN"; | |
exit 0; | |
else | |
echo "dns query for record $FQDN returned $address"; | |
sleep $sleep; | |
fi; | |
done; | |
echo "dns record $FQDN was not removed on time"; | |
exit 1; | |
- name: Delete Cloudflare Worker | |
uses: mathiasvr/command-output@v2.0.0 | |
with: | |
run: | | |
make run -- delete \ | |
--verbose \ | |
--name "$WORKER"; | |
- name: Delete origin record for custom domain | |
run: > | |
records=$(npx cfcli --domain ${CUSTOM_DOMAIN} --type A --format json find ${FQDN} ${CONTENT} | jq '. | length' -r); | |
if [[ $records -gt 0 ]]; then | |
npx cfcli --domain ${CUSTOM_DOMAIN} --type A rm ${FQDN} ${CONTENT}; | |
fi | |
env: | |
CF_API_KEY: ${{ secrets.CF_ACCOUNT_TOKEN }} | |
- name: Create origin record for custom domain | |
run: npx cfcli --domain ${CUSTOM_DOMAIN} --activate --type A add ${FQDN} ${CONTENT} | |
env: | |
CF_API_KEY: ${{ secrets.CF_ACCOUNT_TOKEN }} | |
- name: Check custom domain record | |
run: > | |
records=$(npx cfcli --domain ${CUSTOM_DOMAIN} --type A --format json find ${FQDN} ${CONTENT} | jq '. | length' -r); | |
if [[ $records -eq 1 ]]; then | |
exit 0; | |
else | |
exit 1; | |
fi | |
env: | |
CF_API_KEY: ${{ secrets.CF_ACCOUNT_TOKEN }} | |
- name: Deploy Cloudflare worker with custom domain | |
uses: mathiasvr/command-output@v2.0.0 | |
with: | |
run: | | |
echo Deploying worker "$WORKER"; \ | |
make run -- deploy \ | |
--verbose \ | |
--literal GITHUB_APPLY:true \ | |
--variable WORKER \ | |
--secret GITHUB_TOKEN \ | |
--name "$WORKER" \ | |
--route "$FQDN/*"; | |
- name: Test custom domain endpoint | |
run: > | |
address="$(dig +short $FQDN @1.1.1.1 | head -1)"; | |
retries=6; | |
while [ -z "$address" ]; do | |
if [[ $retries == 0 ]]; then | |
exit 1; | |
else | |
sleep 10; | |
address="$(dig +short $FQDN @1.1.1.1 | head -1)"; | |
retries=$((retries - 1)); | |
fi; | |
done; | |
return_header=$(curl -sk "https://$FQDN" --resolve "$FQDN:443:$address" -D- | head -1); | |
return_code=$(echo "$return_header" | awk '{print $2}'); | |
echo "return code is $return_code"; | |
if [[ "$return_code" != "200" ]]; then | |
exit 1; | |
fi; | |
curl -sk "https://$FQDN" --resolve "$FQDN:443:$address" | grep "^Hello world" | |
- name: Delete Cloudflare Worker | |
uses: mathiasvr/command-output@v2.0.0 | |
with: | |
run: | | |
make run -- delete \ | |
--verbose \ | |
--name "$WORKER"; | |
- name: Check custom domain record | |
run: > | |
records=$(npx cfcli --domain ${CUSTOM_DOMAIN} --type A --format json find ${FQDN} ${CONTENT} | jq '. | length' -r); | |
if [[ $records -eq 1 ]]; then | |
exit 0; | |
else | |
exit 1; | |
fi | |
env: | |
CF_API_KEY: ${{ secrets.CF_ACCOUNT_TOKEN }} | |
- name: Destroy origin record for custom domain | |
run: npx cfcli --domain ${CUSTOM_DOMAIN} --type A rm ${FQDN} ${CONTENT} | |
env: | |
CF_API_KEY: ${{ secrets.CF_ACCOUNT_TOKEN }} | |
- name: Check custom domain record | |
run: > | |
records=$(npx cfcli --domain ${CUSTOM_DOMAIN} --type A --format json find ${FQDN} ${CONTENT} | jq '. | length' -r); | |
if [[ $records -eq 0 ]]; then | |
exit 0; | |
else | |
exit 1; | |
fi | |
env: | |
CF_API_KEY: ${{ secrets.CF_ACCOUNT_TOKEN }} |