Skip to content

Commit

Permalink
add docker build on main
Browse files Browse the repository at this point in the history
  • Loading branch information
thevilledev committed Nov 2, 2024
1 parent f89f98a commit 973e844
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 23 deletions.
57 changes: 57 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Docker build

permissions:
contents: write
packages: write

on:
pull_request:
branches: [ "main" ]
push:
branches: [ "main" ]

jobs:

docker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: |
ghcr.io/thevilledev/single-page-web-server-rs
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=raw,value=latest,enable={{is_default_branch}}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
with:
platforms: linux/amd64,linux/arm64

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- 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
uses: docker/build-push-action@v5
with:
context: .
file: docker/Dockerfile.static
platforms: linux/amd64,linux/arm64
push: false
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
39 changes: 16 additions & 23 deletions docker/Dockerfile.static
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
# Build stage
FROM --platform=$BUILDPLATFORM rust:1.75-slim AS builder
WORKDIR /usr/src/app

# Install build dependencies
RUN set -x && \
apt-get update && \
apt-get install -y musl-tools

# Copy source files
COPY . .

# Install cross-compilation tools based on target architecture
# Set up target architecture variables
ARG TARGETARCH
ARG RUST_TARGET=""
RUN case "${TARGETARCH}" in \
"amd64") TARGET="x86_64-unknown-linux-musl" ;; \
"arm64") TARGET="aarch64-unknown-linux-musl" ;; \
*) TARGET="x86_64-unknown-linux-musl" ;; \
"amd64") echo "x86_64-unknown-linux-musl" > /tmp/rust_target ;; \
"arm64") echo "aarch64-unknown-linux-musl" > /tmp/rust_target ;; \
*) echo "x86_64-unknown-linux-musl" > /tmp/rust_target ;; \
esac && \
rustup target add $TARGET

# Build for the target architecture
ARG TARGETARCH
RUN case "${TARGETARCH}" in \
"amd64") cargo build --release --target x86_64-unknown-linux-musl ;; \
"arm64") cargo build --release --target aarch64-unknown-linux-musl ;; \
*) cargo build --release --target x86_64-unknown-linux-musl ;; \
esac
RUST_TARGET=$(cat /tmp/rust_target) && \
rustup target add $RUST_TARGET && \
cargo build --release --target $RUST_TARGET

# Runtime stage
FROM scratch

# Copy the binary using a simpler architecture-specific pattern
ARG TARGETARCH
COPY --from=builder \
/usr/src/app/target/$( \
if [ "$TARGETARCH" = "amd64" ]; then \
echo "x86_64-unknown-linux-musl"; \
elif [ "$TARGETARCH" = "arm64" ]; then \
echo "aarch64-unknown-linux-musl"; \
else \
echo "x86_64-unknown-linux-musl"; \
fi \
)/release/single-page-web-server-rs /
COPY --from=builder /usr/src/app/target/$([ "$TARGETARCH" = "arm64" ] && echo "aarch64" || echo "x86_64")-unknown-linux-musl/release/single-page-web-server-rs /
COPY index.html /

ENTRYPOINT ["/single-page-web-server-rs"]
CMD ["--addr", "0.0.0.0"]

0 comments on commit 973e844

Please sign in to comment.