Skip to content

Commit

Permalink
use cargo chef for dockerfile builds + switch to docker actions for c…
Browse files Browse the repository at this point in the history
…ache storage
  • Loading branch information
jakewmeyer committed Sep 27, 2023
1 parent df00bdc commit 4808f9d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
36 changes: 16 additions & 20 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,19 @@ jobs:
needs: check
if: github.ref == 'refs/heads/master'
steps:
- uses: actions/checkout@v4
- name: Build image
run: docker build . --file Dockerfile --tag $IMAGE_NAME
- name: Log into registry
run: echo ${{ secrets.PACKAGES_ACCESS_TOKEN }} | docker login ghcr.io -u $GITHUB_ACTOR --password-stdin
- name: Push image
run: |
IMAGE_ID=ghcr.io/${{ github.repository }}
# Change all uppercase to lowercase
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
# Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Strip "v" prefix from tag name
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
# Use Docker `latest` tag convention
[ "$VERSION" == "master" ] && VERSION=latest
echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
docker push $IMAGE_ID:$VERSION
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.PACKAGES_ACCESS_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ghcr.io/jakewmeyer/sandbox:latest
cache-from: type=registry,ref=ghcr.io/jakewmeyer/sandbox:latest
cache-to: type=inline
14 changes: 9 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
FROM rust:1.72.0-alpine AS chef
FROM rust:1.72.0-alpine AS base
RUN apk add musl-dev musl-utils
RUN cargo install cargo-chef

FROM base AS chef
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

FROM chef AS builder
COPY --from=chef /recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
COPY . .
RUN cargo build --target x86_64-unknown-linux-musl --release
RUN cargo build --target x86_64-unknown-linux-musl --release --bin sandbox-api

FROM alpine:latest
COPY --from=builder /target/x86_64-unknown-linux-musl/release/sandbox-api .
ENTRYPOINT ["/sandbox-api"]
FROM alpine AS runtime
RUN addgroup -S app && adduser -S app -G app
COPY --from=builder /target/x86_64-unknown-linux-musl/release/sandbox-api /usr/local/bin/
USER app
CMD ["/usr/local/bin/sandbox-api"]

0 comments on commit 4808f9d

Please sign in to comment.