Attempt 2 for loop action #2
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: 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 before change | ||
run: | | ||
for var in "ALTERNATE_BIOMES" "APOTHEOSIS" "APOTHEOSIS_NEW_GAME_PLUS" "APOTHEOSIS_TUONELA" "NEW_GAME_PLUS_HD" "NIGHTMARE_HD" "NOITAVANIA" "NOITAVANIA_NEW_GAME_PLUS" "PURGATORY" "REGULAR_LEFT_PW" "REGULAR_MAIN_BRANCH" "REGULAR_MIDDLE" "REGULAR_RIGHT_PW"; do | ||
echo "${var}_CACHEKEY" # Placeholder echo, actual dynamic access would need scripting support. | ||
- name: Fetch and update cache keys | ||
id: fetch_and_update_cache_keys | ||
run: | | ||
get_modified_on() { | ||
project_name=$1 | ||
var_name=${2}_CACHEKEY | ||
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") | ||
modified_on=$(echo $response | jq -r '.result[0].modified_on' | date -u -f - +%s) | ||
echo "${var_name}=$modified_on" >> $GITHUB_ENV # Use environment variable to pass dynamic value. | ||
} | ||
get_modified_on "project1" "ALTERNATE_BIOMES" | ||
get_modified_on "project2" "APOTHEOSIS" | ||
# Add more calls as necessary. | ||
- name: Update index.html with new cache keys | ||
run: | | ||
new_keys=$(env | grep _CACHEKEY | sed 's/=/":"/g; s/^/{ "/; s/$/" },/') | ||
# Assuming new_keys is formatted as JSON objects. Adjust the string manipulation as necessary. | ||
perl -pi -e "s#<script id=\"cachekeys\">.*?</script>#<script id=\"cachekeys\">const tileCacheKeys = {$new_keys};</script>#s" public/index.html | ||
- name: Commit and push changes if any | ||
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" | ||
else | ||
git commit -m "Update index.html with new cache keys" | ||
git push |