Update update-cloudflare-proxies.yml #2
Workflow file for this run
This file contains hidden or 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: Update Cloudflare Proxies | |
| on: | |
| push: | |
| branches: | |
| - feature/autoupdate-cf-proxy-list | |
| schedule: | |
| - cron: '0 0 1 * *' # runs at 00:00 UTC on the 1st day of every month | |
| workflow_dispatch: | |
| env: | |
| TARGET_FILE: Common/Utils/TrustedProxiesFetcher.cs | |
| jobs: | |
| update-proxies: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| ref: feature/autoupdate-cf-proxy-list | |
| - name: Fetch Cloudflare IPs and Update File | |
| env: | |
| IPV4_URL: https://www.cloudflare.com/ips-v4 | |
| IPV6_URL: https://www.cloudflare.com/ips-v6 | |
| run: | | |
| set -euo pipefail | |
| echo "Fetching Cloudflare IP lists" | |
| IPV4=$(curl -s $IPV4_URL) | |
| IPV6=$(curl -s $IPV6_URL) | |
| GENERATED_IPS=" // AUTOGENERATED CLOUDFLARE PROXY IP LIST START\n" | |
| GENERATED_IPS+=" // The following IP ranges are automatically updated by GitHub Actions every month.\n" | |
| GENERATED_IPS+=" // Do not manually edit between the markers.\n\n" | |
| GENERATED_IPS+=" // Fetched $(date -u '+%H:%M:%S %d/%m/%Y UTC')\n" | |
| GENERATED_IPS+=" private static readonly IPNetwork[] PrefetchedCloudflareProxies =\n{\n" | |
| GENERATED_IPS+=" // IPv4\n" | |
| for ip in $IPV4; do | |
| GENERATED_IPS+=" IPNetwork.Parse(\"$ip\"),\n" | |
| done | |
| GENERATED_IPS+="\n // IPv6\n" | |
| for ip in $IPV6; do | |
| GENERATED_IPS+=" IPNetwork.Parse(\"$ip\"),\n" | |
| done | |
| GENERATED_IPS+=" };\n" | |
| GENERATED_IPS+=" // AUTOGENERATED CLOUDFLARE PROXY IP LIST END" | |
| # Escape newlines for sed | |
| ESCAPED_IPS=$(printf '%s\n' "$GENERATED_IPS" | sed ':a;N;$!ba;s/\n/\\n/g') | |
| # Replace section in target file | |
| sed -i '/\/\/ AUTOGENERATED CLOUDFLARE PROXY IP LIST START/,/\/\/ AUTOGENERATED CLOUDFLARE PROXY IP LIST END/c\'"$ESCAPED_IPS"'' "$TARGET_FILE" | |
| - name: Commit and Push Changes | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git add ${{ env.TARGET_FILE }} | |
| if git diff --cached --quiet; then | |
| echo "No changes detected." | |
| else | |
| git commit -m "chore: Update Cloudflare proxies [auto-generated]" | |
| git push | |
| fi |