Skip to content

Commit

Permalink
internal/ci: rerun trybot workflows post eviction
Browse files Browse the repository at this point in the history
We currently rely on a scheduled run of the trybot workflow to
repopulate the actions caches post eviction. This works for the default
branch, against which the scheduled trybot run will happen. However it
does not work for other protected branches, e.g. those branches that
match release-branch.*

Therefore, write a bit of shell that reruns the latest trybot workflow
for each branch that matches the protected branch patterns.

Tested by pushing manually to ci/test prior to this CL.

Signed-off-by: Paul Jolly <paul@myitcv.io>
Change-Id: I2e38b5665d0a41be9876f71511af2f9b588ed696
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/551256
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
myitcv committed Mar 20, 2023
1 parent f4d0802 commit a632d16
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 20 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/evict_caches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,24 @@ jobs:
gh actions-cache delete --confirm $j
done
done
# 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/cue/branches | jq -r '.[] | .name')
do
for i in master release-branch.*
do
if [[ "$j" != $i ]]; then
continue
fi
echo "$j is a match with $i"
id=$(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/cue/actions/workflows/trybot.yml/runs?branch=$j&event=push&per_page=1" | jq '.workflow_runs[] | .id')
curl -s -L -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.CUECKOO_GITHUB_PAT }}" -H "X-GitHub-Api-Version: 2022-11-28" -X POST https://api.github.com/repos/cue-lang/cue/actions/runs/$id/rerun
id=$(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/cue-trybot/actions/workflows/trybot.yml/runs?branch=$j&event=push&per_page=1" | jq '.workflow_runs[] | .id')
curl -s -L -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.CUECKOO_GITHUB_PAT }}" -H "X-GitHub-Api-Version: 2022-11-28" -X POST https://api.github.com/repos/cue-lang/cue-trybot/actions/runs/$id/rerun
done
done
2 changes: 0 additions & 2 deletions .github/workflows/trybot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ name: TryBot
tags-ignore:
- v*
pull_request: {}
schedule:
- cron: 15 2 * * *
jobs:
test:
strategy:
Expand Down
60 changes: 46 additions & 14 deletions internal/ci/github/evict_caches.cue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package github

import (
"strings"

"cuelang.org/go/internal/ci/core"

"github.com/SchemaStore/schemastore/src/schemas/json"
Expand Down Expand Up @@ -45,7 +47,6 @@ evict_caches: _base.#bashWorkflow & {

on: {
schedule: [
// We will run a schedule trybot build 15 minutes later to repopulate the caches
{cron: "0 2 * * *"},
]
}
Expand All @@ -57,23 +58,54 @@ evict_caches: _base.#bashWorkflow & {
"runs-on": _#linuxMachine
steps: [
json.#step & {
let branchPatterns = strings.Join(_#protectedBranchPatterns, " ")

// rerunLatestWorkflow runs the latest trybot workflow in the
// specified repo for branches that match the specified branch.
let rerunLatestWorkflow = {
#repo: string
#branch: string
"""
id=$(\(_base.#curlGitHubAPI) "https://api.github.com/repos/\(#repo)/actions/workflows/trybot.yml/runs?branch=\(#branch)&event=push&per_page=1" | jq '.workflow_runs[] | .id')
\(_base.#curlGitHubAPI) -X POST https://api.github.com/repos/\(#repo)/actions/runs/$id/rerun
"""
}

run: """
set -eux
set -eux
echo ${{ secrets.CUECKOO_GITHUB_PAT }} | gh auth login --with-token
gh extension install actions/gh-actions-cache
for i in \(core.#githubRepositoryURL) \(core.#githubRepositoryURL)-trybot
do
echo "Evicting caches for $i"
cd $(mktemp -d)
git init
git remote add origin $i
for j in $(gh actions-cache list -L 100 | grep refs/ | awk '{print $1}')
echo ${{ secrets.CUECKOO_GITHUB_PAT }} | gh auth login --with-token
gh extension install actions/gh-actions-cache
for i in \(core.#githubRepositoryURL) \(core.#githubRepositoryURL)-trybot
do
gh actions-cache delete --confirm $j
echo "Evicting caches for $i"
cd $(mktemp -d)
git init
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
# 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 $(\(_base.#curlGitHubAPI) -f https://api.github.com/repos/\(core.#githubRepositoryPath)/branches | jq -r '.[] | .name')
do
for i in \(branchPatterns)
do
if [[ "$j" != $i ]]; then
continue
fi
echo "$j is a match with $i"
\(rerunLatestWorkflow & {#repo: core.#githubRepositoryPath, #branch: "$j", _})
\(rerunLatestWorkflow & {#repo: core.#githubRepositoryPath + "-trybot", #branch: "$j", _})
done
done
done
"""
"""
},
]
}
Expand Down
4 changes: 0 additions & 4 deletions internal/ci/github/trybot.cue
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ trybot: _base.#bashWorkflow & {
"tags-ignore": [core.#releaseTagPattern]
}
pull_request: {}
schedule: [
// Run at 0215 each day, 15 mins after the cache eviction
{cron: "15 2 * * *"},
]
}

jobs: {
Expand Down
2 changes: 2 additions & 0 deletions internal/ci/github/workflows.cue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ workflows: [
// gerritstatusupdater is running for this repository.
//
// This name is also used by the CI badge in the top-level README.
//
// This name is also used in the evict_caches lookups.
file: "trybot.yml"
schema: trybot
},
Expand Down

0 comments on commit a632d16

Please sign in to comment.