From a632d16ec443659bec96e6a0f0409a510867dd49 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Mon, 20 Mar 2023 06:09:05 +0000 Subject: [PATCH] internal/ci: rerun trybot workflows post eviction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Change-Id: I2e38b5665d0a41be9876f71511af2f9b588ed696 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/551256 TryBot-Result: CUEcueckoo Reviewed-by: Daniel Martí --- .github/workflows/evict_caches.yml | 21 ++++++++++ .github/workflows/trybot.yml | 2 - internal/ci/github/evict_caches.cue | 60 ++++++++++++++++++++++------- internal/ci/github/trybot.cue | 4 -- internal/ci/github/workflows.cue | 2 + 5 files changed, 69 insertions(+), 20 deletions(-) diff --git a/.github/workflows/evict_caches.yml b/.github/workflows/evict_caches.yml index 9160c8feac6..7498d3c7e8a 100644 --- a/.github/workflows/evict_caches.yml +++ b/.github/workflows/evict_caches.yml @@ -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 diff --git a/.github/workflows/trybot.yml b/.github/workflows/trybot.yml index dede944fdba..7aba7551a37 100644 --- a/.github/workflows/trybot.yml +++ b/.github/workflows/trybot.yml @@ -11,8 +11,6 @@ name: TryBot tags-ignore: - v* pull_request: {} - schedule: - - cron: 15 2 * * * jobs: test: strategy: diff --git a/internal/ci/github/evict_caches.cue b/internal/ci/github/evict_caches.cue index c570b92d19e..331255dbce1 100644 --- a/internal/ci/github/evict_caches.cue +++ b/internal/ci/github/evict_caches.cue @@ -15,6 +15,8 @@ package github import ( + "strings" + "cuelang.org/go/internal/ci/core" "github.com/SchemaStore/schemastore/src/schemas/json" @@ -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 * * *"}, ] } @@ -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 - """ + """ }, ] } diff --git a/internal/ci/github/trybot.cue b/internal/ci/github/trybot.cue index 1714af1c3a9..5c9d3d23485 100644 --- a/internal/ci/github/trybot.cue +++ b/internal/ci/github/trybot.cue @@ -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: { diff --git a/internal/ci/github/workflows.cue b/internal/ci/github/workflows.cue index 50430c0aa8f..e88caf43eda 100644 --- a/internal/ci/github/workflows.cue +++ b/internal/ci/github/workflows.cue @@ -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 },