Skip to content

Rotate Fly.io App Region #4599

Rotate Fly.io App Region

Rotate Fly.io App Region #4599

Workflow file for this run

name: Rotate Fly.io App Region
on:
schedule:
# Runs every 2 hours
- cron: '0 */2 * * *'
workflow_dispatch: # Allows manual triggering
env:
node_version: 19
FORCE_COLOR: 1
concurrency: # prevent concurrent releases
group: flyctl-roll-region
cancel-in-progress: true
jobs:
rotate-region:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: superfly/flyctl-actions/setup-flyctl@master
- name: Switch to a Different Region and Scale
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
run: |
APP_NAME="gumcast-api"
OLD_REGION=$(flyctl regions list | grep 'Regions \[app\]:' | awk '{print $3}')
# Ordered list of regions
REGIONS=("lax" "sea" "bos" "dfw" "den" "ewr" "iad" "ord" "sjc")
# Check if REGIONS is empty
if [ ${#REGIONS[@]} -eq 0 ]; then
echo "No regions defined. Exiting."
exit 1
fi
# If OLD_REGION is empty, bring up a machine in the first region
if [ -z "$OLD_REGION" ]; then
echo "No old region found. Bringing up a machine in the first region: ${REGIONS[0]}"
flyctl scale count --region ${REGIONS[0]} 1 --yes
exit 0
fi
# Find the next region
NEW_REGION=""
for i in "${!REGIONS[@]}"; do
if [[ "${REGIONS[$i]}" == "$OLD_REGION" ]]; then
NEW_REGION=${REGIONS[$(( (i + 1) % ${#REGIONS[@]} ))]}
break
fi
done
# Check if NEW_REGION is empty or the same as OLD_REGION
if [ -z "$NEW_REGION" ] || [ "$NEW_REGION" == "$OLD_REGION" ]; then
echo "No new region found or it's the same as the old region. Exiting."
exit 1
fi
# Scale the new region to 1
flyctl scale count --region $NEW_REGION 1 --yes
# Wait for the new region to be ready (optional, add more robust logic as needed)
sleep 60
# Scale the old region to 0
flyctl scale count --region $OLD_REGION 0 --yes