Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/actions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!--
SPDX-FileCopyrightText: 2026 The P4 Language Consortium

SPDX-License-Identifier: Apache-2.0
-->

# CI caches

Native CI uses `ccache-v2-` keys scoped to architecture, OS, compiler, and build
configuration. Bazel uses `bazel-v2-` keys scoped to architecture, runner image,
and Bazel version, so compiler upgrades get fresh toolchain detection.

Both append a `main` or `pr-<number>` scope followed by the run ID and attempt.
Successful jobs save a fresh cache. PRs first restore their newest compatible
cache, then fall back to a compatible main cache.

## Daily cleanup

The Cache maintenance workflow uses the shared
[`p4lang/.github` cache retention action](https://github.com/p4lang/.github/tree/main/CI/cache-retention)
to keep the newest main cache per key/version and prune duplicate or closed-PR
caches. It removes least-recently-used PR caches to target 2 GB of PR storage
and 9 GB total. Main caches are retained even if those targets cannot be met.
Only `ccache-v2-` and `bazel-v2-` keys are managed; other caches count toward the
total but are not deleted by this workflow.

Run the workflow manually with `dry-run` enabled to preview cleanup. Scheduled
runs apply the policy automatically.
27 changes: 20 additions & 7 deletions .github/actions/setup-native-ci/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ description: Restore ccache, install dependencies, and initialize ccache

inputs:
cache-key:
description: Cache key for ccache
required: true
cache-restore-keys:
description: Restore keys for ccache
description: ccache-v2 key prefix identifying the platform, compiler, and build configuration
required: true
install-clang:
description: Whether to install clang
Expand All @@ -28,12 +25,28 @@ inputs:
runs:
using: composite
steps:
- name: Set cache keys
id: cache-key
shell: bash
env:
CACHE_PREFIX: ${{ inputs.cache-key }}
CACHE_SCOPE: ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.pull_request.number) || 'main' }}
run: |
{
echo "key=${CACHE_PREFIX}-${CACHE_SCOPE}-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}"
echo "restore=${CACHE_PREFIX}-${CACHE_SCOPE}-"
echo "main=${CACHE_PREFIX}-main-"
} >> "$GITHUB_OUTPUT"

- name: Restore ccache
uses: actions/cache@v4
# The cache action saves this run's key after a successful job.
uses: actions/cache@v6
with:
path: ~/.cache/ccache
key: ${{ inputs.cache-key }}
restore-keys: ${{ inputs.cache-restore-keys }}
key: ${{ steps.cache-key.outputs.key }}
restore-keys: |
${{ steps.cache-key.outputs.restore }}
${{ steps.cache-key.outputs.main }}

- name: Install dependencies
shell: bash
Expand Down
34 changes: 29 additions & 5 deletions .github/workflows/bazel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,33 @@ jobs:
with:
submodules: recursive

- name: Identify runner image
id: runner-image
run: echo "version=${ImageOS}-${ImageVersion}" >> "$GITHUB_OUTPUT"

- name: Set cache keys
id: cache-key
env:
CACHE_PREFIX: bazel-v2-${{ runner.os }}-${{ runner.arch }}-${{ steps.runner-image.outputs.version }}-${{ hashFiles('.bazelversion') }}-build
CACHE_SCOPE: ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.pull_request.number) || 'main' }}
run: |
{
echo "key=${CACHE_PREFIX}-${CACHE_SCOPE}-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}"
echo "restore=${CACHE_PREFIX}-${CACHE_SCOPE}-"
echo "main=${CACHE_PREFIX}-main-"
} >> "$GITHUB_OUTPUT"

- name: Mount bazel cache
uses: actions/cache@v6
uses: actions/cache/restore@v6
with:
# See https://docs.bazel.build/versions/master/output_directories.html
path: "~/.cache/bazel"
# Create a new cache entry whenever Bazel files change.
# See https://docs.github.com/en/actions/guides/caching-dependencies-to-speed-up-workflows
key: bazel-${{ runner.os }}-build-${{ hashFiles('**/*.bzl', '**/*.bazel') }}
# The full cache includes the detected local C/C++ toolchain. Only
# reuse it on the same runner image and with the same Bazel version.
key: ${{ steps.cache-key.outputs.key }}
restore-keys: |
bazel-${{ runner.os }}-build-
${{ steps.cache-key.outputs.restore }}
${{ steps.cache-key.outputs.main }}

- name: Install Buildifier
run: |
Expand Down Expand Up @@ -64,3 +81,10 @@ jobs:
run: |
bazel test //targets/simple_switch/tests/...
bazel test //targets/simple_switch_grpc/tests/...

- name: Save bazel cache
uses: actions/cache/save@v6
if: success() && (github.ref == 'refs/heads/main' || github.event_name == 'pull_request')
with:
path: "~/.cache/bazel"
key: ${{ steps.cache-key.outputs.key }}
33 changes: 33 additions & 0 deletions .github/workflows/ci-cache-maintenance.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# SPDX-FileCopyrightText: 2026 The P4 Language Consortium
#
# SPDX-License-Identifier: Apache-2.0

name: Cache maintenance
on:
schedule:
- cron: "23 3 * * *"
workflow_dispatch:
inputs:
dry-run:
description: Preview cache deletions without applying them
type: boolean
default: false
permissions:
actions: write
pull-requests: read
concurrency:
group: cache-maintenance
cancel-in-progress: false
jobs:
prune:
runs-on: ubuntu-latest
steps:
- name: Keep main caches and clean up PR caches
uses: p4lang/.github/CI/cache-retention@4bec527fd3988345ec240ae8f4f000bc938e4259
with:
key-prefixes: |
ccache-v2-
bazel-v2-
pr-budget: "2000000000"
total-budget: "9000000000"
dry-run: ${{ inputs.dry-run || false }}
15 changes: 3 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ jobs:
- name: Setup native CI
uses: ./.github/actions/setup-native-ci
with:
cache-key: ccache-${{ matrix.os }}-${{ matrix.cc }}-${{ hashFiles('configure.ac', 'Makefile.am', '.github/workflows/test.yml', 'install_deps.sh') }}
cache-restore-keys: |
ccache-${{ matrix.os }}-${{ matrix.cc }}-
cache-key: ccache-v2-${{ runner.arch }}-${{ matrix.os }}-${{ matrix.cc }}-${{ hashFiles('configure.ac', 'Makefile.am', '.github/workflows/test.yml', 'install_deps.sh') }}
install-lcov: true
install-clang: ${{ matrix.cc == 'clang' }}
- name: Run tests
Expand Down Expand Up @@ -123,10 +121,7 @@ jobs:
- name: Setup native CI
uses: ./.github/actions/setup-native-ci
with:
cache-key: ccache-${{ matrix.os }}-no-pi-${{ hashFiles('configure.ac', 'Makefile.am', '.github/workflows/test.yml', 'install_deps.sh') }}
cache-restore-keys: |
ccache-${{ matrix.os }}-no-pi-
ccache-${{ matrix.os }}-
cache-key: ccache-v2-${{ runner.arch }}-${{ matrix.os }}-no-pi-${{ hashFiles('configure.ac', 'Makefile.am', '.github/workflows/test.yml', 'install_deps.sh') }}
- name: Run tests
env:
CC: ccache gcc
Expand Down Expand Up @@ -166,11 +161,7 @@ jobs:
- name: Setup native CI
uses: ./.github/actions/setup-native-ci
with:
cache-key: ccache-${{ matrix.os }}-cmake-${{ matrix.name }}-${{ hashFiles('CMakeLists.txt', '.github/workflows/test.yml', 'install_deps.sh') }}
cache-restore-keys: |
ccache-${{ matrix.os }}-cmake-${{ matrix.name }}-
ccache-${{ matrix.os }}-cmake-
ccache-${{ matrix.os }}-
cache-key: ccache-v2-${{ runner.arch }}-${{ matrix.os }}-cmake-${{ matrix.name }}-${{ hashFiles('CMakeLists.txt', '.github/workflows/test.yml', 'install_deps.sh') }}
- name: Build BMv2
run: |
mkdir -p build
Expand Down
Loading