diff --git a/.github/workflows/publish-standalone.yml b/.github/workflows/publish-standalone.yml index 8e052b63c..db3e9b1a2 100644 --- a/.github/workflows/publish-standalone.yml +++ b/.github/workflows/publish-standalone.yml @@ -1,16 +1,18 @@ -name: Build and Publish Standalone Docker Image - +name: Build and Publish Stakgraph Standalone on: - release: - types: [published] + push: + branches: + - main + paths: + - "standalone/**" + - "ast/**" + - "lsp/**" + - "Dockerfile.standalone" jobs: - build-and-push: + build-and-publish: runs-on: ubuntu-latest - permissions: - contents: read - packages: write - + name: Build and Publish Docker Image steps: - name: Checkout code uses: actions/checkout@v4 @@ -18,23 +20,38 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - - name: Log in to GitHub Container Registry + - name: Login to GitHub Container Registry uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Build and push Docker image + # Setup cache + - name: Cache Docker layers + uses: actions/cache@v3 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + + # Build and push the image + - name: Build and Push Docker Image uses: docker/build-push-action@v5 with: context: . - file: Dockerfile.standalone - platforms: linux/amd64,linux/arm64 + file: ./Dockerfile.standalone push: true - # Use registry cache which is more reliable for multi-platform - cache-from: type=registry,ref=ghcr.io/stakwork/stakgraph-standalone:buildcache - cache-to: type=registry,ref=ghcr.io/stakwork/stakgraph-standalone:buildcache,mode=max tags: | - ghcr.io/stakwork/stakgraph-standalone:${{ github.ref_name }} ghcr.io/stakwork/stakgraph-standalone:latest + ghcr.io/stakwork/stakgraph-standalone:${{ github.sha }} + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max + platforms: linux/amd64,linux/arm64 + build-args: | + BUILDKIT_INLINE_CACHE=1 + - name: Move cache + run: | + rm -rf /tmp/.buildx-cache + mv /tmp/.buildx-cache-new /tmp/.buildx-cache diff --git a/Dockerfile.standalone b/Dockerfile.standalone index 09d1e91a0..55b84e80f 100644 --- a/Dockerfile.standalone +++ b/Dockerfile.standalone @@ -1,36 +1,48 @@ -FROM rust:1.87.0 AS builder - -WORKDIR /app - +FROM rust:1.87.0 AS planner +WORKDIR /plan # Create a temporary workspace with only the 3 crates we need RUN echo '[workspace]' > Cargo.toml && \ echo 'members = ["ast", "lsp", "standalone"]' >> Cargo.toml && \ echo 'resolver = "2"' >> Cargo.toml -# Copy ONLY the Cargo.toml files for our 3 crates +# Copy only files needed for dependency calculations to create a stable dependency layer COPY ast/Cargo.toml ast/Cargo.toml COPY lsp/Cargo.toml lsp/Cargo.toml COPY standalone/Cargo.toml standalone/Cargo.toml +COPY Cargo.lock . -# Create dummy src files for all 3 crates +# Create a dummy project structure (minimal files needed for cargo to calculate dependencies) RUN mkdir -p ast/src lsp/src standalone/src && \ - echo "fn main() {}" > ast/src/main.rs && \ - echo "fn main() {}" > lsp/src/main.rs && \ - echo "fn main() {}" > standalone/src/main.rs + echo 'fn main() {}' > ast/src/lib.rs && \ + echo 'fn main() {}' > lsp/src/lib.rs && \ + echo 'fn main() {}' > standalone/src/main.rs -# Build dependencies for standalone (this will pull in ast/lsp as workspace deps) -RUN cargo build --release --bin standalone --features neo4j +FROM rust:1.87.0 AS cacher +WORKDIR /app +COPY --from=planner /plan . + +# Build dependencies only (leveraging cargo's dependency caching) +RUN cargo build --release --bin standalone --features neo4j && \ + rm -rf ast/src lsp/src standalone/src + +FROM rust:1.87.0 AS builder +WORKDIR /app -# Remove dummy files and temp workspace -RUN rm -rf ast/src lsp/src standalone/src +COPY --from=cacher /app/target target +COPY --from=cacher /usr/local/cargo /usr/local/cargo +COPY --from=cacher /app/Cargo.toml . +COPY --from=cacher /app/Cargo.lock . +COPY --from=cacher /app/ast/Cargo.toml ./ast/ +COPY --from=cacher /app/lsp/Cargo.toml ./lsp/ +COPY --from=cacher /app/standalone/Cargo.toml ./standalone/ -# Copy actual source code +# Now copy the actual source code COPY ast/src ast/src COPY lsp/src lsp/src COPY standalone/src standalone/src COPY standalone/static standalone/static -# Build the actual application (only this rebuilds when source changes) +# Build the application with the actual source (much faster now with all dependencies pre-built) RUN cargo build --release --bin standalone --features neo4j FROM sphinxlightning/stakgraph-lsp:latest