Update protomaps tiles on Cloudflare R2 #6
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: Update protomaps tiles on Cloudflare R2 | |
on: | |
schedule: | |
- cron: '45 21 1 */2 *' | |
workflow_dispatch: | |
jobs: | |
update-tiles: | |
runs-on: | |
labels: 8core-ubuntu | |
steps: | |
- name: Set AWS credentials | |
env: | |
R2_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }} | |
R2_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }} | |
run: | | |
echo "Setting AWS Credentials" | |
aws configure set aws_access_key_id "$R2_ACCESS_KEY_ID" | |
aws configure set aws_secret_access_key "$R2_SECRET_ACCESS_KEY" | |
aws configure set default.region auto | |
- name: Download latest tileset | |
run: | | |
TODAY="$(date +'%Y%m%d')" | |
echo "TODAY=$TODAY" >> $GITHUB_ENV | |
echo "Today's date for filename is $TODAY" | |
echo "Downloading protomaps pmtiles file." | |
wget https://build.protomaps.com/"${TODAY}".pmtiles | |
- name: Upload tileset to Cloudflare R2 | |
env: | |
R2_API_ENDPOINT: ${{ secrets.R2_API_ENDPOINT }} | |
run: | | |
echo "Getting existing tileset filename" | |
aws s3api list-objects-v2 --bucket protomaps --endpoint-url https://"$R2_API_ENDPOINT".r2.cloudflarestorage.com > output.json | |
OLD_MAP_TILES=$(jq -r '.Contents[0].Key' output.json) | |
echo "OLD_MAP_TILES=$OLD_MAP_TILES" >> $GITHUB_ENV | |
echo "Uploading file to Cloudflare R2" | |
aws s3 cp "${{ env.TODAY }}".pmtiles s3://protomaps/"${{ env.TODAY }}"/world.pmtiles --endpoint-url https://"$R2_API_ENDPOINT".r2.cloudflarestorage.com | |
- name: Checkout repo | |
uses: actions/checkout@v4 | |
with: | |
sparse-checkout: | | |
js | |
serverless/cloudflare | |
serverless/shared | |
sparse-checkout-cone-mode: false | |
- name: Update PMTILES_PATH environment variable in wrangler.toml | |
run: | | |
echo "Updating PMTILES_PATH environment variable in wrangler.toml" | |
PMTILES_PATH="${{ env.TODAY }}/{name}.pmtiles" | |
sed -i -E 's|PMTILES_PATH =.*|PMTILES_PATH = "'"$PMTILES_PATH"'"|g' serverless/cloudflare/wrangler.toml | |
- name: Install dependencies | |
run: | | |
echo "Installing JS dependencies" | |
cd js | |
npm install | |
echo "Installing cloudflare worker packages" | |
cd ../serverless/cloudflare | |
npm install | |
- name: Build & Deploy Worker | |
uses: cloudflare/wrangler-action@v3 | |
with: | |
apiToken: ${{ secrets.CLOUDFLARE_WORKERS_API_TOKEN }} | |
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | |
workingDirectory: "serverless/cloudflare" | |
- name: Push updated wrangler.toml to repo | |
uses: test-room-7/action-update-file@v1 | |
with: | |
file-path: serverless/cloudflare/wrangler.toml | |
commit-msg: Update wrangler.toml to latest version | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Delete old map tiles file from Cloudflare R2 | |
env: | |
R2_API_ENDPOINT: ${{ secrets.R2_API_ENDPOINT }} | |
run: | | |
echo "Deleting old map tiles from Cloudflare R2" | |
aws s3 rm s3://protomaps/"${{ env.OLD_MAP_TILES }}" --endpoint-url https://"$R2_API_ENDPOINT".r2.cloudflarestorage.com |