more caching in github ci

This commit is contained in:
Graham McIntire 2026-01-31 16:53:25 -06:00
parent 5636b00f33
commit be054323a6
No known key found for this signature in database
3 changed files with 68 additions and 24 deletions

View file

@ -14,9 +14,15 @@ on:
branches: branches:
- main - main
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env: env:
REGISTRY: ghcr.io REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }} IMAGE_NAME: ${{ github.repository }}
CARGO_TERM_COLOR: always
RUSTFLAGS: -C link-arg=-fuse-ld=lld
jobs: jobs:
test: test:
@ -32,19 +38,18 @@ jobs:
toolchain: "1.91" toolchain: "1.91"
components: rustfmt, clippy components: rustfmt, clippy
- name: Install protobuf compiler - name: Cache apt packages
run: sudo apt-get update && sudo apt-get install -y protobuf-compiler uses: awalsh128/cache-apt-pkgs-action@latest
- name: Cache cargo registry
uses: actions/cache@v4
with: with:
path: | packages: protobuf-compiler lld
~/.cargo/registry version: 1.0
~/.cargo/git
target - name: Rust cache
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} uses: Swatinem/rust-cache@v2
restore-keys: | with:
${{ runner.os }}-cargo- shared-key: "rust-cache"
cache-on-failure: true
cache-all-crates: true
- name: Check - name: Check
run: cargo check --release run: cargo check --release
@ -83,18 +88,29 @@ jobs:
- name: Extract branch name - name: Extract branch name
id: branch id: branch
run: echo "name=${GITHUB_REF#refs/heads/}" >> $GITHUB_OUTPUT run: |
BRANCH_NAME=${GITHUB_REF#refs/heads/}
# Sanitize branch name for Docker tag (replace / with -)
SAFE_BRANCH=$(echo "$BRANCH_NAME" | sed 's/\//-/g')
echo "name=$SAFE_BRANCH" >> $GITHUB_OUTPUT
- name: Build and push - name: Build and push
uses: docker/build-push-action@v6 uses: docker/build-push-action@v6
with: with:
context: . context: .
push: true push: true
provenance: false
build-args: |
BUILDKIT_INLINE_CACHE=1
tags: | tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.branch.outputs.name }} ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.branch.outputs.name }}
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }} ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:sha-${{ github.sha }}
cache-from: type=gha cache-from: |
cache-to: type=gha,mode=max type=gha,scope=build-branch
type=gha,scope=build-main
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
cache-to: type=gha,scope=build-branch,mode=max
build-main: build-main:
name: Build (Main) name: Build (Main)
@ -137,16 +153,24 @@ jobs:
context: . context: .
platforms: linux/amd64 platforms: linux/amd64
push: true push: true
provenance: false
build-args: | build-args: |
VERSION=${{ steps.version.outputs.version }} VERSION=${{ steps.version.outputs.version }}
BUILDKIT_INLINE_CACHE=1
tags: | tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:main ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:main
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.version }} ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.version }}
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:main-${{ steps.version.outputs.short_sha }} ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:main-${{ steps.version.outputs.short_sha }}
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:main-${{ steps.version.outputs.timestamp }} ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:main-${{ steps.version.outputs.timestamp }}
cache-from: type=gha ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache
cache-to: type=gha,mode=max cache-from: |
type=gha,scope=build-main
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
cache-to: |
type=gha,scope=build-main,mode=max
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache,mode=max
- name: Summary - name: Summary
run: | run: |
@ -197,14 +221,22 @@ jobs:
context: . context: .
platforms: linux/amd64,linux/arm64 platforms: linux/amd64,linux/arm64
push: true push: true
provenance: false
build-args: | build-args: |
VERSION=${{ steps.version.outputs.version }} VERSION=${{ steps.version.outputs.version }}
BUILDKIT_INLINE_CACHE=1
tags: | tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.version }} ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.version }}
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.tag }} ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.tag }}
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
cache-from: type=gha cache-from: |
cache-to: type=gha,mode=max type=gha,scope=build-main
type=gha,scope=release
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
cache-to: |
type=gha,scope=release,mode=max
type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache,mode=max
- name: Summary - name: Summary
run: | run: |

3
.gitignore vendored
View file

@ -22,3 +22,6 @@ data/
# OS # OS
.DS_Store .DS_Store
Thumbs.db Thumbs.db
# Local scripts
monitor-deploy.sh

View file

@ -1,8 +1,10 @@
# syntax=docker/dockerfile:1.4
# Build stage # Build stage
FROM rust:1.83-alpine AS builder FROM rust:1.83-alpine AS builder
# Build arguments provided by Docker buildx # Build arguments provided by Docker buildx
ARG TARGETPLATFORM ARG TARGETPLATFORM
ARG TARGETARCH
ARG VERSION=0.1.0-unknown ARG VERSION=0.1.0-unknown
WORKDIR /app WORKDIR /app
@ -26,8 +28,12 @@ COPY proto ./proto
# Create a dummy main.rs to build dependencies # Create a dummy main.rs to build dependencies
RUN mkdir src && echo "fn main() {}" > src/main.rs RUN mkdir src && echo "fn main() {}" > src/main.rs
# Build dependencies (cached layer) # Build dependencies (cached layer) with BuildKit cache mounts
RUN RUST_TARGET=$(cat /tmp/rust-target) && \ # Cache is separated by target architecture for multi-platform builds
RUN --mount=type=cache,id=cargo-registry-${TARGETARCH},target=/usr/local/cargo/registry \
--mount=type=cache,id=cargo-git-${TARGETARCH},target=/usr/local/cargo/git \
--mount=type=cache,id=cargo-target-${TARGETARCH},target=/app/target \
RUST_TARGET=$(cat /tmp/rust-target) && \
BUILD_VERSION="$VERSION" cargo build --release --target "$RUST_TARGET" BUILD_VERSION="$VERSION" cargo build --release --target "$RUST_TARGET"
# Remove dummy src # Remove dummy src
@ -36,8 +42,11 @@ RUN rm -rf src
# Copy actual source code # Copy actual source code
COPY src ./src COPY src ./src
# Build the actual application # Build the actual application with BuildKit cache mounts
RUN RUST_TARGET=$(cat /tmp/rust-target) && \ RUN --mount=type=cache,id=cargo-registry-${TARGETARCH},target=/usr/local/cargo/registry \
--mount=type=cache,id=cargo-git-${TARGETARCH},target=/usr/local/cargo/git \
--mount=type=cache,id=cargo-target-${TARGETARCH},target=/app/target \
RUST_TARGET=$(cat /tmp/rust-target) && \
touch src/main.rs && \ touch src/main.rs && \
BUILD_VERSION="$VERSION" cargo build --release --target "$RUST_TARGET" && \ BUILD_VERSION="$VERSION" cargo build --release --target "$RUST_TARGET" && \
cp "target/$RUST_TARGET/release/towerops-agent" /tmp/towerops-agent cp "target/$RUST_TARGET/release/towerops-agent" /tmp/towerops-agent