Skip to content

Commit 6935a54

Browse files
committed
feat: enable sccache for cargo-pgrx builds in GitHub Actions
Speed up rust builds by enabling sccache caching for cargo-pgrx builds in GitHub Actions. We mount a persistent disk at /var/cache/sccache and configure sccache to use it.
1 parent f5be8f5 commit 6935a54

3 files changed

Lines changed: 57 additions & 1 deletion

File tree

.github/actions/nix-install-ephemeral/action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ inputs:
55
description: 'Whether to push build outputs to the Nix binary cache'
66
required: false
77
default: 'false'
8+
enable-sccache-sandbox-path:
9+
description: 'Whether to expose /var/cache/sccache in the Nix sandbox'
10+
required: false
11+
default: 'false'
812
aws-region:
913
description: 'AWS region for the Nix binary cache S3 bucket'
1014
required: false
@@ -48,4 +52,5 @@ runs:
4852
substituters = https://cache.nixos.org https://nix-postgres-artifacts.s3.amazonaws.com
4953
trusted-public-keys = nix-postgres-artifacts:dGZlQOvKcNEjvT7QEAJbcV6b6uk7VF/hWMjhYleiaLI= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
5054
${{ inputs.push-to-cache == 'true' && 'post-build-hook = /etc/nix/upload-to-cache.sh' || '' }}
55+
${{ inputs.enable-sccache-sandbox-path == 'true' && 'sandbox-paths = /var/cache/sccache?' || '' }}
5156
max-jobs = 4

.github/workflows/nix-build.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,21 @@ jobs:
3636
- name: Checkout Repo
3737
if: ${{ matrix.attr != '' }}
3838
uses: actions/checkout@v4
39+
- name: Mount sccache disk
40+
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
41+
uses: useblacksmith/stickydisk@v1
42+
with:
43+
key: ${{ github.repository }}-sccache-${{ runner.os }}-${{ runner.arch }}
44+
path: /var/cache/sccache
45+
- name: Allow sccache cache write access
46+
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
47+
run: sudo chmod 1777 /var/cache/sccache
3948
- name: Install nix (ephemeral)
4049
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
4150
uses: ./.github/actions/nix-install-ephemeral
4251
with:
4352
push-to-cache: 'true'
53+
enable-sccache-sandbox-path: 'true'
4454
env:
4555
DEV_AWS_ROLE: ${{ secrets.DEV_AWS_ROLE }}
4656
NIX_SIGN_SECRET_KEY: ${{ secrets.NIX_SIGN_SECRET_KEY }}
@@ -67,11 +77,21 @@ jobs:
6777
- name: Checkout Repo
6878
if: ${{ matrix.attr != '' }}
6979
uses: actions/checkout@v4
80+
- name: Mount sccache disk
81+
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
82+
uses: useblacksmith/stickydisk@v1
83+
with:
84+
key: ${{ github.repository }}-sccache-${{ runner.os }}-${{ runner.arch }}
85+
path: /var/cache/sccache
86+
- name: Allow sccache cache write access
87+
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
88+
run: sudo chmod 1777 /var/cache/sccache
7089
- name: Install nix (ephemeral)
7190
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
7291
uses: ./.github/actions/nix-install-ephemeral
7392
with:
7493
push-to-cache: 'true'
94+
enable-sccache-sandbox-path: 'true'
7595
env:
7696
DEV_AWS_ROLE: ${{ secrets.DEV_AWS_ROLE }}
7797
NIX_SIGN_SECRET_KEY: ${{ secrets.NIX_SIGN_SECRET_KEY }}
@@ -144,10 +164,20 @@ jobs:
144164
- name: Checkout Repo
145165
if: ${{ matrix.attr != '' }}
146166
uses: actions/checkout@v4
167+
- name: Mount sccache disk
168+
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
169+
uses: useblacksmith/stickydisk@v1
170+
with:
171+
key: ${{ github.repository }}-sccache-${{ runner.os }}-${{ runner.arch }}
172+
path: /var/cache/sccache
173+
- name: Allow sccache cache write access
174+
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
175+
run: sudo chmod 1777 /var/cache/sccache
147176
- name: Install nix
148177
if: ${{ matrix.attr != '' }}
149178
uses: ./.github/actions/nix-install-ephemeral
150179
with:
180+
enable-sccache-sandbox-path: 'true'
151181
push-to-cache: 'true'
152182
env:
153183
DEV_AWS_ROLE: ${{ secrets.DEV_AWS_ROLE }}
@@ -172,10 +202,20 @@ jobs:
172202
- name: Checkout Repo
173203
if: ${{ matrix.attr != '' }}
174204
uses: actions/checkout@v4
205+
- name: Mount sccache disk
206+
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
207+
uses: useblacksmith/stickydisk@v1
208+
with:
209+
key: ${{ github.repository }}-sccache-${{ runner.os }}-${{ runner.arch }}
210+
path: /var/cache/sccache
211+
- name: Allow sccache cache write access
212+
if: ${{ matrix.attr != '' && matrix.runs_on.group != 'self-hosted-runners-nix' }}
213+
run: sudo chmod 1777 /var/cache/sccache
175214
- name: Install nix
176215
if: ${{ matrix.attr != '' }}
177216
uses: ./.github/actions/nix-install-ephemeral
178217
with:
218+
enable-sccache-sandbox-path: 'true'
179219
push-to-cache: 'true'
180220
env:
181221
DEV_AWS_ROLE: ${{ secrets.DEV_AWS_ROLE }}

nix/cargo-pgrx/buildPgrxExtension.nix

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
darwin,
3636
writeShellScriptBin,
3737
defaultBindgenHook,
38+
sccache,
3839
}:
3940

4041
# The idea behind: Use it mostly like rustPlatform.buildRustPackage and so
@@ -56,7 +57,7 @@
5657
postgresql,
5758
# enable override to generate bindings using bindgenHook.
5859
# Some older versions of cargo-pgrx use a bindgenHook that is not compatible with the
59-
# current clang version present in stdenv
60+
# current clang version present in stdenv
6061
bindgenHook ? defaultBindgenHook,
6162
# cargo-pgrx calls rustfmt on generated bindings, this is not strictly necessary, so we avoid the
6263
# dependency here. Set to false and provide rustfmt in nativeBuildInputs, if you need it, e.g.
@@ -135,12 +136,22 @@ let
135136
postgresql
136137
pkg-config
137138
bindgenHook
139+
sccache
138140
]
139141
++ lib.optionals useFakeRustfmt [ fakeRustfmt ];
140142

141143
buildPhase = ''
142144
runHook preBuild
143145
146+
if [[ -d "/var/cache/sccache" && -w "/var/cache/sccache" ]]; then
147+
echo "sccache: cache directory available, enabling"
148+
export RUSTC_WRAPPER="${sccache}/bin/sccache"
149+
export SCCACHE_DIR="/var/cache/sccache"
150+
export SCCACHE_CACHE_SIZE="100G"
151+
else
152+
echo "sccache: cache directory not available, skipping"
153+
fi
154+
144155
echo "Executing cargo-pgrx buildPhase"
145156
${preBuildAndTest}
146157
${maybeEnterBuildAndTestSubdir}

0 commit comments

Comments
 (0)