Skip to content

Commit 33c7261

Browse files
authored
CI: kache compile cache, cargo-chef layer for the server image (#78)
* CI: kache as the compile cache in place of rust-cache (only main writes it; the R2 bucket once KACHE_BUCKET is set, else the GitHub cache); the server image builds dependencies in a cargo-chef layer; the app job's Postgres user is visualci * CI: kache's read-write key is a secret of the kache-write environment, which only main may use; kache-action pinned to a commit * CI: drop the old cache key the kache step left behind (a duplicated with:, which stopped the workflow from starting)
1 parent 45e4911 commit 33c7261

3 files changed

Lines changed: 94 additions & 11 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,25 @@ env:
2323
jobs:
2424
lint:
2525
runs-on: ${{ fromJSON(vars.CI_RUNNER || '"ubuntu-latest"') }}
26+
environment: ${{ github.ref == 'refs/heads/main' && 'kache-write' || '' }}
2627
timeout-minutes: 30
2728
steps:
2829
- uses: actions/checkout@v4
29-
- uses: Swatinem/rust-cache@v2
30+
# Compile cache (kache, as Cargo's rustc wrapper): the R2 bucket every machine shares (KACHE_BUCKET), else the
31+
# GitHub Actions cache. Only main writes it: the read-write key is a secret of the kache-write environment,
32+
# which only main may use, so a pull request (whatever its ci.yml says) gets the read-only key.
33+
- uses: kunobi-ninja/kache-action@061011817fd04b0e460f79d6bb3c115b1fc8f029 # v1
34+
with:
35+
version: "2.4.0"
36+
cache-key-prefix: kache-${{ github.job }}
37+
save-cache: ${{ github.ref == 'refs/heads/main' }}
38+
pr-comment: false
39+
s3-bucket: ${{ vars.KACHE_BUCKET }}
40+
s3-endpoint: ${{ vars.KACHE_ENDPOINT }}
41+
s3-region: auto
42+
s3-prefix: visualci
43+
s3-access-key-id: ${{ github.ref == 'refs/heads/main' && secrets.KACHE_RW_ACCESS_KEY_ID || secrets.KACHE_RO_ACCESS_KEY_ID }}
44+
s3-secret-access-key: ${{ github.ref == 'refs/heads/main' && secrets.KACHE_RW_SECRET_ACCESS_KEY || secrets.KACHE_RO_SECRET_ACCESS_KEY }}
3045
- run: rustup show active-toolchain || rustup toolchain install
3146
- run: cargo fmt --all --check
3247
- run: cargo clippy --locked --workspace --all-targets -- -D warnings
@@ -36,13 +51,14 @@ jobs:
3651
# The Laravel Rust app (app/, sc-8900): its own workspace until it replaces server/ at cutover. Not deployed yet.
3752
app:
3853
runs-on: ${{ fromJSON(vars.CI_RUNNER || '"ubuntu-latest"') }}
54+
environment: ${{ github.ref == 'refs/heads/main' && 'kache-write' || '' }}
3955
timeout-minutes: 40
4056
services:
4157
postgres:
4258
image: postgres:17
4359
env:
44-
POSTGRES_USER: loco
45-
POSTGRES_PASSWORD: loco
60+
POSTGRES_USER: visualci
61+
POSTGRES_PASSWORD: visualci
4662
POSTGRES_DB: visualci_app_test
4763
ports: ["55433:5432"]
4864
options: --health-cmd pg_isready --health-interval 5s --health-timeout 5s --health-retries 10
@@ -56,9 +72,21 @@ jobs:
5672
docker run -d --name s3 -p 59000:9000 \
5773
-e RUSTFS_ACCESS_KEY=visualci -e RUSTFS_SECRET_KEY=visualci-dev-secret rustfs/rustfs:1.0.6
5874
for _ in $(seq 1 30); do curl -s -o /dev/null http://127.0.0.1:59000/ && break; sleep 1; done
59-
- uses: Swatinem/rust-cache@v2
75+
# Compile cache (kache, as Cargo's rustc wrapper): the R2 bucket every machine shares (KACHE_BUCKET), else the
76+
# GitHub Actions cache. Only main writes it: the read-write key is a secret of the kache-write environment,
77+
# which only main may use, so a pull request (whatever its ci.yml says) gets the read-only key.
78+
- uses: kunobi-ninja/kache-action@061011817fd04b0e460f79d6bb3c115b1fc8f029 # v1
6079
with:
61-
workspaces: app
80+
version: "2.4.0"
81+
cache-key-prefix: kache-${{ github.job }}
82+
save-cache: ${{ github.ref == 'refs/heads/main' }}
83+
pr-comment: false
84+
s3-bucket: ${{ vars.KACHE_BUCKET }}
85+
s3-endpoint: ${{ vars.KACHE_ENDPOINT }}
86+
s3-region: auto
87+
s3-prefix: visualci
88+
s3-access-key-id: ${{ github.ref == 'refs/heads/main' && secrets.KACHE_RW_ACCESS_KEY_ID || secrets.KACHE_RO_ACCESS_KEY_ID }}
89+
s3-secret-access-key: ${{ github.ref == 'refs/heads/main' && secrets.KACHE_RW_SECRET_ACCESS_KEY || secrets.KACHE_RO_SECRET_ACCESS_KEY }}
6290
- run: rustup show active-toolchain || rustup toolchain install
6391
- run: cargo fmt --check
6492
- run: cargo clippy --locked --all-targets -- -D warnings
@@ -87,6 +115,7 @@ jobs:
87115

88116
test:
89117
runs-on: ${{ fromJSON(vars.CI_RUNNER || '"ubuntu-latest"') }}
118+
environment: ${{ github.ref == 'refs/heads/main' && 'kache-write' || '' }}
90119
timeout-minutes: 40
91120
services:
92121
postgres:
@@ -99,7 +128,21 @@ jobs:
99128
options: --health-cmd pg_isready --health-interval 5s --health-timeout 5s --health-retries 10
100129
steps:
101130
- uses: actions/checkout@v4
102-
- uses: Swatinem/rust-cache@v2
131+
# Compile cache (kache, as Cargo's rustc wrapper): the R2 bucket every machine shares (KACHE_BUCKET), else the
132+
# GitHub Actions cache. Only main writes it: the read-write key is a secret of the kache-write environment,
133+
# which only main may use, so a pull request (whatever its ci.yml says) gets the read-only key.
134+
- uses: kunobi-ninja/kache-action@061011817fd04b0e460f79d6bb3c115b1fc8f029 # v1
135+
with:
136+
version: "2.4.0"
137+
cache-key-prefix: kache-${{ github.job }}
138+
save-cache: ${{ github.ref == 'refs/heads/main' }}
139+
pr-comment: false
140+
s3-bucket: ${{ vars.KACHE_BUCKET }}
141+
s3-endpoint: ${{ vars.KACHE_ENDPOINT }}
142+
s3-region: auto
143+
s3-prefix: visualci
144+
s3-access-key-id: ${{ github.ref == 'refs/heads/main' && secrets.KACHE_RW_ACCESS_KEY_ID || secrets.KACHE_RO_ACCESS_KEY_ID }}
145+
s3-secret-access-key: ${{ github.ref == 'refs/heads/main' && secrets.KACHE_RW_SECRET_ACCESS_KEY || secrets.KACHE_RO_SECRET_ACCESS_KEY }}
103146
# The request tests also write the MCP tools' and REST API's answers as JSON snapshots, for the visual job.
104147
- run: cargo test --locked --workspace
105148
env:
@@ -120,6 +163,7 @@ jobs:
120163
visual:
121164
needs: [test]
122165
runs-on: ${{ fromJSON(vars.CI_RUNNER || '"ubuntu-latest"') }}
166+
environment: ${{ github.ref == 'refs/heads/main' && 'kache-write' || '' }}
123167
timeout-minutes: 30
124168
services:
125169
postgres:
@@ -141,7 +185,21 @@ jobs:
141185
docker run -d --name s3 -p 59000:9000 \
142186
-e RUSTFS_ACCESS_KEY=visualci -e RUSTFS_SECRET_KEY=visualci-dev-secret rustfs/rustfs:1.0.6
143187
for _ in $(seq 1 30); do curl -s -o /dev/null http://127.0.0.1:59000/ && break; sleep 1; done
144-
- uses: Swatinem/rust-cache@v2
188+
# Compile cache (kache, as Cargo's rustc wrapper): the R2 bucket every machine shares (KACHE_BUCKET), else the
189+
# GitHub Actions cache. Only main writes it: the read-write key is a secret of the kache-write environment,
190+
# which only main may use, so a pull request (whatever its ci.yml says) gets the read-only key.
191+
- uses: kunobi-ninja/kache-action@061011817fd04b0e460f79d6bb3c115b1fc8f029 # v1
192+
with:
193+
version: "2.4.0"
194+
cache-key-prefix: kache-${{ github.job }}
195+
save-cache: ${{ github.ref == 'refs/heads/main' }}
196+
pr-comment: false
197+
s3-bucket: ${{ vars.KACHE_BUCKET }}
198+
s3-endpoint: ${{ vars.KACHE_ENDPOINT }}
199+
s3-region: auto
200+
s3-prefix: visualci
201+
s3-access-key-id: ${{ github.ref == 'refs/heads/main' && secrets.KACHE_RW_ACCESS_KEY_ID || secrets.KACHE_RO_ACCESS_KEY_ID }}
202+
s3-secret-access-key: ${{ github.ref == 'refs/heads/main' && secrets.KACHE_RW_SECRET_ACCESS_KEY || secrets.KACHE_RO_SECRET_ACCESS_KEY }}
145203
- run: cargo build --locked -p visualci_server -p visualci-cli
146204
- uses: pnpm/action-setup@v4
147205
with:
@@ -182,6 +240,7 @@ jobs:
182240

183241
clients:
184242
runs-on: ${{ fromJSON(vars.CI_RUNNER || '"ubuntu-latest"') }}
243+
environment: ${{ github.ref == 'refs/heads/main' && 'kache-write' || '' }}
185244
timeout-minutes: 15
186245
steps:
187246
- uses: actions/checkout@v4
@@ -198,9 +257,21 @@ jobs:
198257
node-version: "24"
199258
# The Compare two PDFs page runs crates/visualci-wasm in the browser (scripts/wasm.mjs builds it).
200259
- run: rustup target add wasm32-unknown-unknown
201-
- uses: Swatinem/rust-cache@v2
260+
# Compile cache (kache, as Cargo's rustc wrapper): the R2 bucket every machine shares (KACHE_BUCKET), else the
261+
# GitHub Actions cache. Only main writes it: the read-write key is a secret of the kache-write environment,
262+
# which only main may use, so a pull request (whatever its ci.yml says) gets the read-only key.
263+
- uses: kunobi-ninja/kache-action@061011817fd04b0e460f79d6bb3c115b1fc8f029 # v1
202264
with:
203-
key: wasm
265+
version: "2.4.0"
266+
cache-key-prefix: kache-${{ github.job }}
267+
save-cache: ${{ github.ref == 'refs/heads/main' }}
268+
pr-comment: false
269+
s3-bucket: ${{ vars.KACHE_BUCKET }}
270+
s3-endpoint: ${{ vars.KACHE_ENDPOINT }}
271+
s3-region: auto
272+
s3-prefix: visualci
273+
s3-access-key-id: ${{ github.ref == 'refs/heads/main' && secrets.KACHE_RW_ACCESS_KEY_ID || secrets.KACHE_RO_ACCESS_KEY_ID }}
274+
s3-secret-access-key: ${{ github.ref == 'refs/heads/main' && secrets.KACHE_RW_SECRET_ACCESS_KEY || secrets.KACHE_RO_SECRET_ACCESS_KEY }}
204275
# The production site: with the Plausible snippets (analytics.ts) and, required, the WebAssembly.
205276
- run: pnpm install --frozen-lockfile && VISUALCI_ANALYTICS=1 VISUALCI_REQUIRE_WASM=1 pnpm build
206277
working-directory: server/frontend

‎app/src/testing.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use laravel::Application;
88
use laravel::foundation::ApplicationBuilder;
99

10-
const CI_DATABASE: &str = "postgres://loco:loco@127.0.0.1:55433/visualci_app_test";
10+
const CI_DATABASE: &str = "postgres://visualci:visualci@127.0.0.1:55433/visualci_app_test";
1111

1212
pub fn create() -> Application {
1313
configure().create()

‎docker/server.Dockerfile‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,20 @@ ARG VISUALCI_ANALYTICS=0
1010
ENV VISUALCI_ANALYTICS=${VISUALCI_ANALYTICS}
1111
RUN if [ -f pnpm-lock.yaml ]; then pnpm build; else npm run build; fi
1212

13-
FROM rust:1.99.1-trixie AS build
13+
# Dependencies build in a layer of their own (cargo-chef), rebuilt only when Cargo.toml or Cargo.lock change: a commit
14+
# that only changes our code compiles only our crates.
15+
FROM rust:1.99.1-trixie AS chef
16+
RUN cargo install cargo-chef --locked --version 0.1.78
1417
WORKDIR /src
18+
19+
FROM chef AS planner
20+
COPY . .
21+
RUN cargo chef prepare --recipe-path recipe.json
22+
23+
FROM chef AS build
24+
COPY --from=planner /src/recipe.json recipe.json
25+
COPY rust-toolchain.toml ./
26+
RUN cargo chef cook --release --locked -p visualci_server --recipe-path recipe.json
1527
COPY . .
1628
ARG BUILD_SHA=dev
1729
ENV BUILD_SHA=${BUILD_SHA}

0 commit comments

Comments
 (0)