Skip to content

Loop cacheKeys workflow #17

Loop cacheKeys workflow

Loop cacheKeys workflow #17

Workflow file for this run

name: Loop cacheKeys workflow
on:
workflow_dispatch:
jobs:
getRepoCacheVars:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
- name: Print all var names with values before change
run: |
PROJECT_NAMES=("ALTERNATE_BIOMES" "APOTHEOSIS"{,"_NEW_GAME_PLUS","_TUONELA"} "NEW_GAME_PLUS_HD" "NIGHTMARE"{"_MIDDLE","_LEFT_PW","_RIGHT_PW"} "NOITAVANIA"{,"_NEW_GAME_PLUS"} "PURGATORY" "REGULAR"{"_LEFT_PW","_MAIN_BRANCH","_MIDDLE","_RIGHT_PW"})
# curl get repo variables into $response
response=$(curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GH_NOITAMAP_VARS_ACCESS }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/acidflow-noita/noitamap/actions/variables)
for name in "${PROJECT_NAMES[@]}"; do
cachekey_value=$(jq '.variables[] | select(.name == "${name}_CACHEKEY") | .value' <<< "$response")
echo "${name}_CACHEKEY=${cachekey_value}"
done
echo "PROJECT_NAMES=${PROJECT_NAMES[@]}" >> $GITHUB_ENV
- name: Fetch and update cache keys
id: fetch_and_update_cache_keys
run: |
get_modified_on() {
project_name=$1
var_name=${2}_CACHEKEY
echo "Trying for project_name=$project_name and Cache Key=$var_name"
response=$(curl --request GET \
--header "Authorization: Bearer ${{ secrets.CF_PAGES_READ_ALL_API }}" \
--header 'Content-Type: application/json' \
--url "https://api.cloudflare.com/client/v4/accounts/${{ secrets.CF_ACCOUNT_ID }}/pages/projects/${project_name}/deployments")
project_alias=$(jq -r '.result[0].aliases[0]' <<< "$response" | sed 's#https://##')
modified_on=$(jq -r '.result[0].modified_on' <<< "$response" | date -u -f - +%s)
echo "${var_name}=${project_alias}=${modified_on}" >> $GITHUB_ENV # Use environment variable to pass dynamic value.
}
for name in ${PROJECT_NAMES[@]}; do
CF_PROJECT_NAME=$(echo "$name" | tr '[:upper:]' '[:lower:]' | tr '_' '-')
get_modified_on "$CF_PROJECT_NAME" "$name"
done
- name: Github Cachekeys Update
id: gh_cachekeys_update
run: |
update_modified_on() {
var_name=${1}_CACHEKEY
var_value=${!var_name#*=}
curl -L \
-X PATCH \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GH_NOITAMAP_VARS_ACCESS }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/acidflow-noita/noitamap/actions/variables/${var_name}\
-d "{\"name\":\"${var_name}\",\"value\":\"${var_value}\"}"
}
for name in ${PROJECT_NAMES[@]}; do
update_modified_on "$name"
done
- name: Update index.html with new cache keys
run: |
new_keys=$(env | grep _CACHEKEY | sed 's/^[^=]*=//; s/=/":"/; s/^/"/; s/$/",/')
perl -0777 -pi -e "s#<script id=\"cachekeys\">.*?</script>#<script id=\"cachekeys\">const tileCacheKeys = {$new_keys};</script>#s" public/index.html
- name: Commit index.html changes
id: commit
run: |
git config --global user.email "action@github.com"
git config --global user.name "GitHub Actions"
git add public/index.html
if [ -z "$(git status --porcelain)" ]; then
echo "No changes to commit"
echo "push=false" >> $GITHUB_OUTPUT
else
git commit -m "Update index.html with new cache keys"
echo "push=true" >> $GITHUB_OUTPUT
fi
shell: bash
- name: Push changes
if: steps.commit.outputs.push == 'true'
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GH_ACIDFRLOW_COMMIT_TO_REPO }}