Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

ci: trim redundant compile steps and rework the S3 sccache (#14492)

Hrithikeshca2802e7e142
diff --git a/.github/scripts/sccache-cache-scope.sh b/.github/scripts/sccache-cache-scope.sh
new file mode 100755
index 0000000000..4143d75446
--- /dev/null
+++ b/.github/scripts/sccache-cache-scope.sh
@@ -0,0 +1,47 @@
+#! /usr/bin/env bash
+
+set -euo pipefail
+
+if [[ "${CI:-false}" != "true" && "${GITHUB_ACTIONS:-false}" != "true" ]]; then
+ echo "This script is to be run in a GitHub Actions runner only. Exiting."
+ exit 1
+fi
+
+if [ "$#" -ne 3 ]; then
+ echo "::error::Usage: $(basename "$0") <base-sha> <pr-number> <same-repo>"
+ exit 1
+fi
+base_sha="$1"
+pr_number="$2"
+same_repo="$3"
+
+# Writes `scope` to GITHUB_OUTPUT: the PR number for a PR-scoped cache, or
+# empty for the shared one. Passed as the optional second argument to
+# sccache-restore-s3.sh and sccache-save-s3.sh.
+#
+# PRs changing third-party crates get their own key: the shared cache is built
+# from the base branch, so it has none of the new dependency artifacts — scoped
+# keys let reruns reuse that work. Compared against the base tip, not the merge
+# base, since the shared cache tracks the base tip.
+#
+# Never scoped for forks: untrusted checkouts must not write cache objects.
+
+scope=''
+
+# Only fetch when the base commit is absent: `git fetch --depth=1` on an
+# unshallow clone marks it shallow, and CI-pr's `test` job later pushes
+# Cargo.lock — shallow pushes are rejected.
+if [[ "${same_repo}" != 'true' ]]; then
+ echo "Fork pull request; using the shared cache read-only"
+elif ! git cat-file -e "${base_sha}^{commit}" 2>/dev/null \
+ && ! git fetch --no-tags --depth=1 origin "${base_sha}" 2>/dev/null; then
+ # Force-pushed base or shallow checkout; the shared cache is a valid fallback.
+ echo "::warning::Could not fetch the base commit ${base_sha}; using the shared cache"
+elif git diff --quiet "${base_sha}" HEAD -- Cargo.lock; then
+ echo "Cargo.lock matches the base branch; using the shared cache"
+else
+ echo "Cargo.lock differs from the base branch; using a PR-scoped cache"
+ scope="${pr_number}"
+fi
+
+echo "scope=${scope}" >> "${GITHUB_OUTPUT}"
diff --git a/.github/scripts/sccache-restore-s3.sh b/.github/scripts/sccache-restore-s3.sh
index 5b4a7ea5fb..e2702a829f 100755
--- a/.github/scripts/sccache-restore-s3.sh
+++ b/.github/scripts/sccache-restore-s3.sh
@@ -14,8 +14,6 @@ fi
cache_name="$1"
pr_number="${2:-}"
-shared_key="sccache-cache/${cache_name}-${RUNNER_OS}-${RUNNER_ARCH}.tar.gz"
-
mkdir -p "$SCCACHE_DIR"
if [ -z "${CACHE_S3_BUCKET:-}" ]; then
@@ -23,25 +21,54 @@ if [ -z "${CACHE_S3_BUCKET:-}" ]; then
exit 0
fi
-# Streamed, not written to disk first — avoids doubling disk usage.
+# Downloaded in full, then unpacked — not piped — so a download that dies
+# partway can't leave a half-populated SCCACHE_DIR for the fallback key to
+# unpack on top of, and each phase stays separately timed.
+archive="$(dirname "${SCCACHE_DIR}")/.sccache-restore-$$.tar"
+trap 'rm -f "${archive}"' EXIT
+
+key_for() { printf 'sccache-cache/%s-%s-%s%s.tar' "${cache_name}" "${RUNNER_OS}" "${RUNNER_ARCH}" "${1:+-pr$1}"; }
+human() { numfmt --to=iec --suffix=B -- "$1"; }
+rate() { awk -v b="$1" -v ns="$2" 'BEGIN { s = ns / 1e9; printf "%5.1fs", s; if (s > 0) printf " %7.0f MiB/s", b / 1048576 / s }'; }
+
+# `set -e` is suspended in the `if`/`||` contexts below, so failures here must
+# return explicitly, and the trailing `return 0` keeps the exit status of a
+# reporting command from being read as a cache miss.
restore() {
- local key="$1"
+ local key="$1" t0 download_ns unpack_ns archive_bytes tree_bytes
+
echo "Restoring sccache cache, key: ${key}"
- aws s3 cp \
- "s3://${CACHE_S3_BUCKET}/${CACHE_S3_KEY_PREFIX}${key}" \
- - \
- --region "${CACHE_S3_REGION}" --no-progress --only-show-errors \
- | tar xzf - -C "$SCCACHE_DIR"
+
+ # `--no-progress`: aws writes carriage-return updates that a non-TTY CI log
+ # renders as thousands of lines.
+ t0="$(date +%s%N)"
+ aws s3 cp "s3://${CACHE_S3_BUCKET}/${CACHE_S3_KEY_PREFIX}${key}" "${archive}" \
+ --region "${CACHE_S3_REGION}" --no-progress --only-show-errors || return 1
+ download_ns=$(( $(date +%s%N) - t0 ))
+ archive_bytes="$(stat -c%s "${archive}")"
+
+ # Uncompressed: sccache already zstd-compresses its entries, so gzip measured
+ # 1.02x here while costing more wall time than the download it was shrinking.
+ t0="$(date +%s%N)"
+ tar xf "${archive}" -C "${SCCACHE_DIR}" || return 1
+ unpack_ns=$(( $(date +%s%N) - t0 ))
+
+ rm -f "${archive}"
+ tree_bytes="$(du -sb "${SCCACHE_DIR}" | cut -f1)"
+
+ echo " archive $(human "${archive_bytes}") -> tree $(human "${tree_bytes}")"
+ echo " download $(rate "${archive_bytes}" "${download_ns}")"
+ echo " unpack $(rate "${tree_bytes}" "${unpack_ns}")"
+ return 0
}
-# PR-scoped first (isolates concurrent PRs from each other), falling back to
-# the shared merge_group/main cache — mainly so a PR's first push isn't cold.
-if [ -n "$pr_number" ]; then
- pr_key="sccache-cache/${cache_name}-${RUNNER_OS}-${RUNNER_ARCH}-pr${pr_number}.tar.gz"
- if restore "$pr_key"; then
+# PR-scoped first (isolates concurrent PRs from each other), falling back to the
+# shared merge_group/main cache — mainly so a PR's first push isn't cold.
+if [ -n "${pr_number}" ]; then
+ if restore "$(key_for "${pr_number}")"; then
exit 0
fi
echo "No PR-scoped cache found, falling back to shared cache"
fi
-restore "$shared_key" || echo "::warning::No sccache cache found; starting cold"
+restore "$(key_for)" || echo "::warning::No sccache cache found; starting cold"
diff --git a/.github/scripts/sccache-save-s3.sh b/.github/scripts/sccache-save-s3.sh
index 152fa5e22f..5bbd345056 100755
--- a/.github/scripts/sccache-save-s3.sh
+++ b/.github/scripts/sccache-save-s3.sh
@@ -16,11 +16,34 @@ pr_number="${2:-}"
# PR-scoped when a PR number is given, so concurrent PRs don't clobber each
# other's — or the shared merge_group/main — cache.
-key="sccache-cache/${cache_name}-${RUNNER_OS}-${RUNNER_ARCH}${pr_number:+-pr${pr_number}}.tar.gz"
+key="$(printf 'sccache-cache/%s-%s-%s%s.tar' "${cache_name}" "${RUNNER_OS}" "${RUNNER_ARCH}" "${pr_number:+-pr${pr_number}}")"
+
+# Written out in full, then uploaded — not piped — so the pack and the transfer
+# stay separately timed.
+archive="$(dirname "${SCCACHE_DIR}")/.sccache-save-$$.tar"
+trap 'rm -f "${archive}"' EXIT
+
+human() { numfmt --to=iec --suffix=B -- "$1"; }
+rate() { awk -v b="$1" -v ns="$2" 'BEGIN { s = ns / 1e9; printf "%5.1fs", s; if (s > 0) printf " %7.0f MiB/s", b / 1048576 / s }'; }
+
echo "Saving sccache cache, key: ${key}"
-# Streamed, not written to disk first — avoids doubling disk usage.
-tar czf - -C "$SCCACHE_DIR" . \
- | aws s3 cp - \
- "s3://${CACHE_S3_BUCKET}/${CACHE_S3_KEY_PREFIX}${key}" \
- --region "${CACHE_S3_REGION}" --no-progress --only-show-errors
+# Uncompressed: sccache already zstd-compresses its entries, so gzip measured
+# 1.02x here while costing more wall time than the transfer it was shrinking.
+t0="$(date +%s%N)"
+tar cf "${archive}" -C "${SCCACHE_DIR}" .
+pack_ns=$(( $(date +%s%N) - t0 ))
+
+tree_bytes="$(du -sb "${SCCACHE_DIR}" | cut -f1)"
+archive_bytes="$(stat -c%s "${archive}")"
+
+# `--no-progress`: aws writes carriage-return updates that a non-TTY CI log
+# renders as thousands of lines.
+t0="$(date +%s%N)"
+aws s3 cp "${archive}" "s3://${CACHE_S3_BUCKET}/${CACHE_S3_KEY_PREFIX}${key}" \
+ --region "${CACHE_S3_REGION}" --no-progress --only-show-errors
+upload_ns=$(( $(date +%s%N) - t0 ))
+
+echo " tree $(human "${tree_bytes}") -> archive $(human "${archive_bytes}")"
+echo " pack $(rate "${tree_bytes}" "${pack_ns}")"
+echo " upload $(rate "${archive_bytes}" "${upload_ns}")"
diff --git a/.github/workflows/CI-pr.yml b/.github/workflows/CI-pr.yml
index 0848797209..f7d5a47009 100644
--- a/.github/workflows/CI-pr.yml
+++ b/.github/workflows/CI-pr.yml
@@ -11,21 +11,10 @@ concurrency:
cancel-in-progress: true
env:
- # Disable incremental compilation.
- #
- # Incremental compilation is useful as part of an edit-build-test-edit cycle,
- # as it lets the compiler avoid recompiling code that hasn't changed. However,
- # on CI, we're not making small edits; we're almost always building the entire
- # project from scratch. Thus, incremental compilation on CI actually
- # introduces *additional* overhead to support making future builds
- # faster...but no future builds will ever occur in any given CI environment.
- #
- # See https://matklad.github.io/2021/09/04/fast-rust-builds.html#ci-workflow
- # for details.
+ # Disable incremental compilation: CI builds from scratch, so it only adds
+ # overhead. See https://matklad.github.io/2021/09/04/fast-rust-builds.html#ci-workflow
CARGO_INCREMENTAL: 0
- # Allow more retries for network requests in cargo (downloading crates) and
- # rustup (installing toolchains). This should help to reduce flaky CI failures
- # from transient network timeouts or other issues.
+ # More retries for cargo/rustup network requests to reduce flaky CI failures.
CARGO_NET_RETRY: 10
RUSTUP_MAX_RETRIES: 10
# Don't emit giant backtraces in the CI logs.
@@ -87,8 +76,7 @@ jobs:
if: ${{ ! startsWith(github.event.pull_request.base.ref, 'hotfix-') }}
env:
- # sccache on local disk via actions/cache, not SCCACHE_GHA_ENABLED
- # (proven slower here before — see git log for check-msrv).
+ # sccache on local disk via actions/cache, not SCCACHE_GHA_ENABLED (slower here; see git log).
RUSTC_WRAPPER: sccache
SCCACHE_DIR: ${{ github.workspace }}/.sccache
SCCACHE_CACHE_SIZE: "2G"
@@ -153,11 +141,10 @@ jobs:
runs-on: hyperswitch-runners
env:
- # sccache on local disk, restored as one archive instead of its native
- # S3 backend — avoids per-object request round trips under load.
+ # sccache restored as one archive instead of S3 — avoids per-object round trips.
RUSTC_WRAPPER: sccache
SCCACHE_DIR: ${{ github.workspace }}/.sccache
- SCCACHE_CACHE_SIZE: "4G"
+ SCCACHE_CACHE_SIZE: "1536M"
SCCACHE_IDLE_TIMEOUT: "0"
RUSTFLAGS: "-D warnings"
@@ -209,10 +196,20 @@ jobs:
tool: sccache
checksum: true
+ # PRs changing third-party crates get their own cache key; others share. See the script.
+ - name: Decide the sccache scope for this PR
+ id: cache_scope
+ shell: bash
+ run: |
+ .github/scripts/sccache-cache-scope.sh \
+ "${{ github.event.pull_request.base.sha }}" \
+ "${{ github.event.pull_request.number }}" \
+ "${{ github.event.pull_request.head.repo.full_name == github.repository }}"
+
- name: Restore sccache cache from S3
continue-on-error: true
shell: bash
- run: .github/scripts/sccache-restore-s3.sh test
+ run: .github/scripts/sccache-restore-s3.sh test "${{ steps.cache_scope.outputs.scope }}"
- name: Install just
uses: taiki-e/install-action@v2
@@ -245,17 +242,19 @@ jobs:
git push
fi
- - name: Run cargo check
- shell: bash
- env:
- CARGO_BUILD_JOBS: 9
- run: cargo check --features "release"
-
- name: Show sccache stats
if: always()
shell: bash
run: sccache --show-stats
+ # Only for PRs that changed Cargo.lock (see the scope step). Writes a PR-scoped
+ # key, never clobbering the shared cache. `!cancelled()`: cache on failure, skip on cancel.
+ - name: Save sccache cache to S3
+ if: ${{ !cancelled() && steps.cache_scope.outputs.scope != '' }}
+ continue-on-error: true
+ shell: bash
+ run: .github/scripts/sccache-save-s3.sh test "${{ steps.cache_scope.outputs.scope }}"
+
typos:
name: Spell check
runs-on: ubuntu-latest
@@ -293,11 +292,10 @@ jobs:
runs-on: hyperswitch-runners
env:
- # sccache on local disk, restored as a single compressed archive —
- # see `test` for why.
+ # sccache restored as a single archive — see `test` for why.
RUSTC_WRAPPER: sccache
SCCACHE_DIR: ${{ github.workspace }}/.sccache
- SCCACHE_CACHE_SIZE: "4G"
+ SCCACHE_CACHE_SIZE: "1536M"
SCCACHE_IDLE_TIMEOUT: "0"
steps:
@@ -331,10 +329,20 @@ jobs:
tool: sccache
checksum: true
+ # PRs changing third-party crates get their own cache key; others share. See the script.
+ - name: Decide the sccache scope for this PR
+ id: cache_scope
+ shell: bash
+ run: |
+ .github/scripts/sccache-cache-scope.sh \
+ "${{ github.event.pull_request.base.sha }}" \
+ "${{ github.event.pull_request.number }}" \
+ "${{ github.event.pull_request.head.repo.full_name == github.repository }}"
+
- name: Restore sccache cache from S3
continue-on-error: true
shell: bash
- run: .github/scripts/sccache-restore-s3.sh check-v2
+ run: .github/scripts/sccache-restore-s3.sh check-v2 "${{ steps.cache_scope.outputs.scope }}"
- name: Install just
uses: taiki-e/install-action@v2
@@ -354,21 +362,15 @@ jobs:
CARGO_BUILD_JOBS: 9
run: just clippy_v2 fred
- - name: Run cargo check enabling only the release and v2 features (redis-rs backend)
- shell: bash
- env:
- RUSTFLAGS: "-A warnings"
- CARGO_BUILD_JOBS: 9
- run: cargo check --no-default-features --features "release,v2,redis-rs"
-
- - name: Run cargo check enabling only the release and v2 features (fred backend)
- shell: bash
- env:
- RUSTFLAGS: "-A warnings"
- CARGO_BUILD_JOBS: 9
- run: cargo check --no-default-features --features "release,v2,fred"
-
- name: Show sccache stats
if: always()
shell: bash
run: sccache --show-stats
+
+ # Only for PRs that changed Cargo.lock (see the scope step). Writes a PR-scoped
+ # key, never clobbering the shared cache. `!cancelled()`: cache on failure, skip on cancel.
+ - name: Save sccache cache to S3
+ if: ${{ !cancelled() && steps.cache_scope.outputs.scope != '' }}
+ continue-on-error: true
+ shell: bash
+ run: .github/scripts/sccache-save-s3.sh check-v2 "${{ steps.cache_scope.outputs.scope }}"
diff --git a/.github/workflows/CI-push.yml b/.github/workflows/CI-push.yml
index f2453d7eb8..7469935aca 100644
--- a/.github/workflows/CI-push.yml
+++ b/.github/workflows/CI-push.yml
@@ -130,16 +130,16 @@ jobs:
shell: bash
run: .github/scripts/install-jq.sh
+ - name: Run cargo check
+ if: ${{ github.event_name == 'merge_group' }}
+ shell: bash
+ run: cargo check --features "release"
+
- name: Cargo hack
if: ${{ github.event_name == 'push' }}
shell: bash
run: just ci_hack
- - name: Cargo build release
- if: ${{ github.event_name == 'merge_group' }}
- shell: bash
- run: cargo check --features "release"
-
- name: Show sccache stats
if: always()
shell: bash
@@ -192,7 +192,7 @@ jobs:
# populates the shared cache: PRs restore-only, this saves on `push`.
RUSTC_WRAPPER: sccache
SCCACHE_DIR: ${{ github.workspace }}/.sccache
- SCCACHE_CACHE_SIZE: "4G"
+ SCCACHE_CACHE_SIZE: "1536M"
SCCACHE_IDLE_TIMEOUT: "0"
RUSTFLAGS: "-D warnings"
@@ -217,12 +217,6 @@ jobs:
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
- - name: Install cargo-hack
- uses: taiki-e/[email protected]
- with:
- tool: cargo-hack
- checksum: true
-
- name: Install just
uses: taiki-e/install-action@v2
with:
@@ -261,20 +255,6 @@ jobs:
CARGO_BUILD_JOBS: 9
run: just clippy fred
- - name: Cargo hack
- if: ${{ github.event_name == 'push' }}
- shell: bash
- env:
- CARGO_BUILD_JOBS: 9
- run: just ci_hack
-
- - name: Cargo build release
- if: ${{ github.event_name == 'merge_group' }}
- shell: bash
- env:
- CARGO_BUILD_JOBS: 9
- run: cargo check --features "release"
-
- name: Show sccache stats
if: always()
shell: bash
@@ -324,11 +304,11 @@ jobs:
runs-on: hyperswitch-runners-merge
env:
- # sccache on local disk, restored/saved as a single compressed archive
+ # sccache on local disk, restored/saved as a single uncompressed archive
# — see `test` for why.
RUSTC_WRAPPER: sccache
SCCACHE_DIR: ${{ github.workspace }}/.sccache
- SCCACHE_CACHE_SIZE: "4G"
+ SCCACHE_CACHE_SIZE: "1536M"
SCCACHE_IDLE_TIMEOUT: "0"
steps:
@@ -345,12 +325,23 @@ jobs:
uses: dtolnay/rust-toolchain@master
with:
toolchain: stable 2 weeks ago
+ components: clippy
- name: Install Protoc
uses: arduino/setup-protoc@v3
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
+ - name: Install just
+ uses: taiki-e/install-action@v2
+ with:
+ tool: just
+ checksum: true
+
+ - name: Install jq
+ shell: bash
+ run: .github/scripts/install-jq.sh
+
- name: Mask S3 cache location in job logs
shell: bash
run: .github/scripts/sccache-mask-s3-env.sh
@@ -366,17 +357,17 @@ jobs:
shell: bash
run: .github/scripts/sccache-restore-s3.sh check-v2
- - name: Run cargo check enabling only the release and v2 features (redis-rs backend)
+ - name: Run cargo clippy with v2 features enabled (redis-rs backend)
shell: bash
env:
CARGO_BUILD_JOBS: 9
- run: cargo check --no-default-features --features "release,v2,redis-rs"
+ run: just clippy_v2
- - name: Run cargo check enabling only the release and v2 features (fred backend)
+ - name: Run cargo clippy with v2 features enabled (fred backend)
shell: bash
env:
CARGO_BUILD_JOBS: 9
- run: cargo check --no-default-features --features "release,v2,fred"
+ run: just clippy_v2 fred
- name: Show sccache stats
if: always()