Skip to content

Commit 52a117a

Browse files
committed
Swift: optimize bazel caching in CI
Previously the cache would become stale. Now the same incremental cache mechanism in use for the QL cache is adopted (and factored out in a separate action). Namely, pushes on main will populate the cache using the commit hash as key, while PRs will try to use the cache of their merge base, read-only. To avoid the cache growing out of control, a simple cache eviction is done on pushes.
1 parent 0f87eb4 commit 52a117a

File tree

10 files changed

+116
-112
lines changed

10 files changed

+116
-112
lines changed

.github/actions/cache-query-compilation/action.yml

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,14 @@ outputs:
1414
runs:
1515
using: composite
1616
steps:
17-
# Cache the query compilation caches.
18-
# calculate the merge-base with main, in a way that works both on PRs and pushes to main.
19-
- name: Calculate merge-base
20-
shell: bash
21-
if: ${{ github.event_name == 'pull_request' }}
22-
env:
23-
BASE_BRANCH: ${{ github.base_ref }}
24-
run: |
25-
MERGE_BASE=$(git cat-file commit $GITHUB_SHA | grep '^parent ' | head -1 | cut -f 2 -d " ")
26-
echo "merge_base=$MERGE_BASE" >> $GITHUB_ENV
27-
- name: Read CodeQL query compilation - PR
28-
if: ${{ github.event_name == 'pull_request' }}
29-
uses: erik-krogh/actions-cache@a88d0603fe5fb5606db9f002dfcadeb32b5f84c6
17+
- name: Cache the query compilation caches
18+
uses: ./.github/actions/incremental-cache
3019
with:
3120
path: '**/.cache'
32-
read-only: true
33-
key: codeql-compile-${{ inputs.key }}-pr-${{ github.sha }} # deliberately not using the `compile-compile-main` keys here.
34-
restore-keys: |
35-
codeql-compile-${{ inputs.key }}-${{ github.base_ref }}-${{ env.merge_base }}
36-
codeql-compile-${{ inputs.key }}-${{ github.base_ref }}-
37-
codeql-compile-${{ inputs.key }}-main-
38-
- name: Fill CodeQL query compilation cache - main
39-
if: ${{ github.event_name != 'pull_request' }}
40-
uses: erik-krogh/actions-cache@a88d0603fe5fb5606db9f002dfcadeb32b5f84c6
41-
with:
42-
path: '**/.cache'
43-
key: codeql-compile-${{ inputs.key }}-${{ github.ref_name }}-${{ github.sha }} # just fill on main
44-
restore-keys: | # restore from another random commit, to speed up compilation.
45-
codeql-compile-${{ inputs.key }}-${{ github.ref_name }}-
46-
codeql-compile-${{ inputs.key }}-main-
21+
key: codeql-compile-${{ inputs.key }}
4722
- name: Fill compilation cache directory
4823
id: fill-compilation-dir
49-
shell: bash
24+
shell: bash
5025
run: |
5126
# Move all the existing cache into another folder, so we only preserve the cache for the current queries.
5227
mkdir -p ${COMBINED_CACHE_DIR}
@@ -57,5 +32,5 @@ runs:
5732
rm -rf **/.cache/*
5833
5934
echo "compdir=${COMBINED_CACHE_DIR}" >> $GITHUB_OUTPUT
60-
env:
61-
COMBINED_CACHE_DIR: ${{ github.workspace }}/compilation-dir
35+
env:
36+
COMBINED_CACHE_DIR: ${{ github.workspace }}/compilation-dir
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Setup an incremental cache
2+
description: Special cache wrapper to be run on pull requests and pushes, that will try to restore
3+
a cache as close as possible to the merge base
4+
5+
inputs:
6+
path:
7+
description: 'The path to cache'
8+
required: true
9+
key:
10+
description: 'The cache key to use - should be unique to the workflow'
11+
required: true
12+
13+
runs:
14+
using: composite
15+
steps:
16+
# calculate the merge-base with main, in a way that works both on PRs and pushes to main.
17+
- name: Calculate merge-base
18+
shell: bash
19+
if: ${{ github.event_name == 'pull_request' }}
20+
env:
21+
BASE_BRANCH: ${{ github.base_ref }}
22+
run: |
23+
MERGE_BASE=$(git cat-file commit $GITHUB_SHA | grep '^parent ' | head -1 | cut -f 2 -d " ")
24+
echo "merge_base=$MERGE_BASE" >> $GITHUB_ENV
25+
- name: Restore read-only cache (PR)
26+
if: ${{ github.event_name == 'pull_request' }}
27+
uses: erik-krogh/actions-cache@a88d0603fe5fb5606db9f002dfcadeb32b5f84c6
28+
with:
29+
path: ${{ inputs.path }}
30+
read-only: true
31+
key: ${{ inputs.key }}-pr-${{ github.sha }}
32+
restore-keys: |
33+
${{ inputs.key }}-${{ github.base_ref }}-${{ env.merge_base }}
34+
${{ inputs.key }}-${{ github.base_ref }}-
35+
${{ inputs.key }}-main-
36+
- name: Fill cache (push)
37+
if: ${{ github.event_name != 'pull_request' }}
38+
uses: erik-krogh/actions-cache@a88d0603fe5fb5606db9f002dfcadeb32b5f84c6
39+
with:
40+
path: ${{ inputs.path }}
41+
key: ${{ inputs.key }}-${{ github.ref_name }}-${{ github.sha }} # just fill on main
42+
restore-keys: | # restore from another random commit, to speed up compilation.
43+
${{ inputs.key }}-${{ github.ref_name }}-
44+
${{ inputs.key }}-main-

.github/workflows/go-tests-other-os.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ on:
55
- "go/**"
66
- "!go/ql/**" # don't run other-os if only ql/ files changed
77
- .github/workflows/go-tests-other-os.yml
8-
- .github/actions/fetch-codeql/action.yml
9-
- .github/actions/cache-query-compilation/action.yml
8+
- .github/actions/**
109
- codeql-workspace.yml
1110
jobs:
1211
test-mac:

.github/workflows/go-tests.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ on:
44
paths:
55
- "go/**"
66
- .github/workflows/go-tests.yml
7-
- .github/actions/fetch-codeql/action.yml
8-
- .github/actions/cache-query-compilation/action.yml
7+
- .github/actions/**
98
- codeql-workspace.yml
109
branches:
1110
- main
@@ -14,8 +13,7 @@ on:
1413
paths:
1514
- "go/**"
1615
- .github/workflows/go-tests.yml
17-
- .github/actions/fetch-codeql/action.yml
18-
- .github/actions/cache-query-compilation/action.yml
16+
- .github/actions/**
1917
- codeql-workspace.yml
2018
jobs:
2119
test-linux:
@@ -64,7 +62,7 @@ jobs:
6462
uses: ./.github/actions/cache-query-compilation
6563
with:
6664
key: go-qltest
67-
65+
6866
- name: Test
6967
run: |
7068
cd go

.github/workflows/swift.yml

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ on:
77
- "misc/bazel/**"
88
- "*.bazel*"
99
- .github/workflows/swift.yml
10-
- .github/actions/fetch-codeql/action.yml
11-
- .github/actions/cache-query-compilation/action.yml
10+
- .github/actions/**
1211
- codeql-workspace.yml
1312
- .pre-commit-config.yaml
1413
- "!**/*.md"
@@ -22,8 +21,7 @@ on:
2221
- "misc/bazel/**"
2322
- "*.bazel*"
2423
- .github/workflows/swift.yml
25-
- .github/actions/fetch-codeql/action.yml
26-
- .github/actions/cache-query-compilation/action.yml
24+
- .github/actions/**
2725
- codeql-workspace.yml
2826
- "!**/*.md"
2927
- "!**/*.qhelp"
@@ -35,20 +33,15 @@ jobs:
3533
# not using a matrix as you cannot depend on a specific job in a matrix, and we want to start linux checks
3634
# without waiting for the macOS build
3735
build-and-test-macos:
38-
if: ${{ github.event_name == 'pull_request' }}
3936
runs-on: macos-12-xl
4037
steps:
4138
- uses: actions/checkout@v3
42-
- uses: ./swift/actions/create-extractor-pack
43-
- uses: ./swift/actions/run-quick-tests
44-
- uses: ./swift/actions/print-unextracted
39+
- uses: ./swift/actions/build-and-test
4540
build-and-test-linux:
4641
runs-on: ubuntu-latest-xl
4742
steps:
4843
- uses: actions/checkout@v3
49-
- uses: ./swift/actions/create-extractor-pack
50-
- uses: ./swift/actions/run-quick-tests
51-
- uses: ./swift/actions/print-unextracted
44+
- uses: ./swift/actions/build-and-test
5245
qltests-linux:
5346
needs: build-and-test-linux
5447
runs-on: ubuntu-latest-xl
@@ -80,7 +73,10 @@ jobs:
8073
runs-on: ubuntu-latest
8174
steps:
8275
- uses: actions/checkout@v3
83-
- uses: ./swift/actions/setup-env
76+
- uses: bazelbuild/setup-bazelisk@v2
77+
- uses: actions/setup-python@v4
78+
with:
79+
python-version-file: 'swift/.python-version'
8480
- uses: pre-commit/[email protected]
8581
name: Check that python code is properly formatted
8682
with:
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: Build Swift CodeQL pack
2+
description: Builds the Swift CodeQL pack
3+
runs:
4+
using: composite
5+
steps:
6+
- uses: bazelbuild/setup-bazelisk@v2
7+
- uses: actions/setup-python@v4
8+
with:
9+
python-version-file: 'swift/.python-version'
10+
- name: Mount bazel cache
11+
uses: ./.github/actions/incremental-cache
12+
with:
13+
path: "~/.cache/bazel-repository-cache"
14+
key: bazel-cache-${{ runner.os }}-${{ runner.arch }}
15+
- name: Mount bazel disk cache
16+
uses: ./.github/actions/incremental-cache
17+
with:
18+
path: "~/.cache/bazel-disk-cache"
19+
key: bazel-disk-cache-${{ runner.os }}-${{ runner.arch }}
20+
- name: Configure bazel cache
21+
shell: bash
22+
run: |
23+
echo build --repository_cache=~/.cache/bazel-repository-cache --disk_cache=~/.cache/bazel-disk-cache > ~/.bazelrc
24+
echo test --test_output=errors >> ~/.bazelrc
25+
- name: Print unextracted entities
26+
shell: bash
27+
run: |
28+
bazel run //swift/extractor/print_unextracted
29+
- uses: ./swift/actions/share-extractor-pack
30+
- name: Build Swift extractor
31+
shell: bash
32+
run: |
33+
bazel run //swift:create-extractor-pack
34+
- name: Run xcode-autobuilder tests
35+
if : ${{ github.event_name == 'pull_request' && runner.os == 'macOS' }}
36+
shell: bash
37+
run: |
38+
bazel test //swift/xcode-autobuilder/tests
39+
- name: Run codegen tests
40+
if : ${{ github.event_name == 'pull_request' }}
41+
shell: bash
42+
run: |
43+
bazel test //swift/codegen/test
44+
- name: Run qltest tests
45+
if : ${{ github.event_name == 'pull_request' }}
46+
shell: bash
47+
run: |
48+
bazel test //swift/tools/test/qltest
49+
- name: Evict bazel cache
50+
if: ${{ github.event_name != 'pull_request' }}
51+
shell: bash
52+
run: |
53+
find "~/.cache/bazel-repository-cache" "~/.cache/bazel-disk-cache" -atime +0 -type f -delete
54+
du -sh "~/.cache/bazel-repository-cache" "~/.cache/bazel-disk-cache"

swift/actions/create-extractor-pack/action.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

swift/actions/print-unextracted/action.yml

Lines changed: 0 additions & 9 deletions
This file was deleted.

swift/actions/run-quick-tests/action.yml

Lines changed: 0 additions & 18 deletions
This file was deleted.

swift/actions/setup-env/action.yml

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)