Skip to content

Commit

Permalink
Merge pull request Julien-cpsn#105 from alan910127/main
Browse files Browse the repository at this point in the history
feat: continuous deployment for docker image
  • Loading branch information
Julien-cpsn authored Aug 22, 2024
2 parents 97ac550 + 5ff4360 commit cfb15bc
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.dockerignore
Dockerfile

.idea
/target

*.ps1

.gitignore

/.github
/base_collections
/gifs
/import-tests
/.typos.toml
LICENSE
README.md
61 changes: 61 additions & 0 deletions .github/workflows/build-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Build Docker Image

on:
workflow_dispatch:
pull_request:
branches: [ "main" ]
paths:
- .github/workflows/build-docker.yml
- Dockerfile
push:
branches: [ "main" ]
release:
types: [ "published" ]

env:
PUSH_IMAGE: false
IMAGE_NAME: Julien-cpsn/ATAC
RUST_VERSION: "1.79"

jobs:
build-and-publish:
name: Build and Publish Docker Image
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.IMAGE_NAME }}
tags: |
# When the main branch gets updated, don't tag it as 'main'; instead, tag it as 'latest'
type=ref,event=branch,enable=false
type=raw,value=latest,enable={{is_default_branch}}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to Docker Hub
if: ${{ env.PUSH_IMAGE == true && github.event_name != 'pull_request' }}
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
build-args: RUST_VERSION=${{ env.RUST_VERSION }}
push: ${{ env.PUSH_IMAGE == 'true' && github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
# Reuse docker build cache
cache-from: type=gha
cache-to: type=gha,mode=max


28 changes: 28 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
ARG RUST_VERSION
FROM --platform=$BUILDPLATFORM lukemathwalker/cargo-chef:latest-rust-$RUST_VERSION-alpine3.20 AS base
WORKDIR /app

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

FROM base AS builder
ARG TARGETPLATFORM
RUN case "$TARGETPLATFORM" in \
"linux/amd64") echo "x86_64-unknown-linux-musl" > rust_target.txt ;; \
"linux/arm64") echo "aarch64-unknown-linux-musl" > rust_target.txt ;; \
esac && \
# Install musl target
rustup target add $(cat rust_target.txt) && \
apk add zig && \
cargo install cargo-zigbuild

COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --target $(cat rust_target.txt) --recipe-path recipe.json --zigbuild
COPY . .
RUN cargo zigbuild --release --target $(cat rust_target.txt) --bin atac && cp /app/target/$(cat rust_target.txt)/release/atac /atac

FROM alpine:3.20 AS runtime
COPY --from=builder /atac /atac
WORKDIR /app
ENTRYPOINT [ "/atac" ]

0 comments on commit cfb15bc

Please sign in to comment.