Evict caches #11
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
# Code generated internal/ci/ci_tool.cue; DO NOT EDIT. | |
name: Evict caches | |
"on": | |
schedule: | |
- cron: 0 2 * * * | |
jobs: | |
test: | |
if: ${{github.repository == 'cue-lang/libcue'}} | |
runs-on: ubuntu-22.04 | |
defaults: | |
run: | |
shell: bash | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
ref: ${{ github.event.pull_request.head.sha }} | |
fetch-depth: 0 | |
- name: Reset git directory modification times | |
run: touch -t 202211302355 $(find * -type d) | |
- name: Restore git file modification times | |
uses: chetan/git-restore-mtime-action@075f9bc9d159805603419d50f794bd9f33252ebe | |
- id: DispatchTrailer | |
name: Try to extract Dispatch-Trailer | |
run: |- | |
x="$(git log -1 --pretty='%(trailers:key=Dispatch-Trailer,valueonly)')" | |
if [[ "$x" == "" ]] | |
then | |
# Some steps rely on the presence or otherwise of the Dispatch-Trailer. | |
# We know that we don't have a Dispatch-Trailer in this situation, | |
# hence we use the JSON value null in order to represent that state. | |
# This means that GitHub expressions can determine whether a Dispatch-Trailer | |
# is present or not by checking whether the fromJSON() result of the | |
# output from this step is the JSON value null or not. | |
x=null | |
fi | |
echo "value<<EOD" >> $GITHUB_OUTPUT | |
echo "$x" >> $GITHUB_OUTPUT | |
echo "EOD" >> $GITHUB_OUTPUT | |
- if: |- | |
((github.ref == 'refs/heads/main') && (! (contains(github.event.head_commit.message, ' | |
Dispatch-Trailer: {"type":"')))) && (contains(github.event.head_commit.message, ' | |
Dispatch-Trailer: {"type":"')) | |
name: Check we don't have Dispatch-Trailer on a protected branch | |
run: |- | |
echo "github.event.head_commit.message contains Dispatch-Trailer but we are on a protected branch" | |
false | |
- name: Delete caches | |
run: |- | |
set -x | |
echo ${{ secrets.CUECKOO_GITHUB_PAT }} | gh auth login --with-token | |
gh extension install actions/gh-actions-cache | |
for i in https://github.com/cue-lang/libcue https://github.com/cue-lang/libcue-trybot | |
do | |
echo "Evicting caches for $i" | |
cd $(mktemp -d) | |
git init -b initialbranch | |
git remote add origin $i | |
for j in $(gh actions-cache list -L 100 | grep refs/ | awk '{print $1}') | |
do | |
gh actions-cache delete --confirm $j | |
done | |
done | |
- name: Trigger workflow runs to repopulate caches | |
run: |- | |
# Prepare git for pushes to trybot repo. Note | |
# because we have already checked out code we don't | |
# need origin. Fetch origin default branch for later use | |
git config user.name cueckoo | |
git config user.email cueckoo@gmail.com | |
git config http.https://github.com/.extraheader "AUTHORIZATION: basic $(echo -n cueckoo:${{ secrets.CUECKOO_GITHUB_PAT }} | base64)" | |
git remote add trybot https://github.com/cue-lang/libcue-trybot | |
# Now trigger the most recent workflow run on each of the default branches. | |
# We do this by listing all the branches on the main repo and finding those | |
# which match the protected branch patterns (globs). | |
for j in $(curl -s -L -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.CUECKOO_GITHUB_PAT }}" -H "X-GitHub-Api-Version: 2022-11-28" -f https://api.github.com/repos/cue-lang/libcue/branches | jq -r '.[] | .name') | |
do | |
for i in main | |
do | |
if [[ "$j" != $i ]]; then | |
continue | |
fi | |
echo Branch: $j | |
sha=$(curl -s -L -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.CUECKOO_GITHUB_PAT }}" -H "X-GitHub-Api-Version: 2022-11-28" "https://api.github.com/repos/cue-lang/libcue/commits/$j" | jq -r '.sha') | |
echo Latest commit: $sha | |
echo "Trigger workflow on cue-lang/libcue" | |
curl -s -L -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.CUECKOO_GITHUB_PAT }}" -H "X-GitHub-Api-Version: 2022-11-28" --fail-with-body -X POST https://api.github.com/repos/cue-lang/libcue/actions/workflows/trybot.yaml/dispatches -d "{\"ref\":\"$j\"}" | |
# Ensure that the trybot repo has the latest commit for | |
# this branch. If the force-push results in a commit | |
# being pushed, that will trigger the trybot workflows | |
# so we don't need to do anything, otherwise we need to | |
# trigger the most recent commit on that branch | |
git remote -v | |
git fetch origin refs/heads/$j | |
git log -1 FETCH_HEAD | |
success=false | |
for try in {1..20}; do | |
echo "Push to trybot try $try" | |
exitCode=0; push="$(git push -f trybot FETCH_HEAD:$j 2>&1)" || exitCode=$? | |
echo "$push" | |
if [[ $exitCode -eq 0 ]]; then | |
success=true | |
break | |
fi | |
sleep 1 | |
done | |
if ! $success; then | |
echo "Giving up" | |
exit 1 | |
fi | |
if echo "$push" | grep up-to-date | |
then | |
# We are up-to-date, i.e. the push did nothing, hence we need to trigger a workflow_dispatch | |
# in the trybot repo. | |
echo "Trigger workflow on cue-lang/libcue-trybot" | |
curl -s -L -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.CUECKOO_GITHUB_PAT }}" -H "X-GitHub-Api-Version: 2022-11-28" --fail-with-body -X POST https://api.github.com/repos/cue-lang/libcue-trybot/actions/workflows/trybot.yaml/dispatches -d "{\"ref\":\"$j\"}" | |
else | |
echo "Force-push to cue-lang/libcue-trybot did work; nothing to do" | |
fi | |
done | |
done |