Here simple Github Action workflow to deploy Rust web application easily. First create the .github/workflows/deploy.yml
yml
name: deploy
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@master
- name: Build 📦
run: |
docker build -t app -f ./Dockerfile .
# after this you can push your image to Container Registry
# docker push app:latest
And so the Dockerfile
on the root directory of your project or copy from here:
dockerfile
FROM clux/muslrust:stable AS chef
USER root
RUN cargo install cargo-chef
WORKDIR /app
FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
FROM chef AS builder
COPY --from=planner /app/recipe.json recipe.json
# Notice that we are specifying the --target flag!
RUN cargo chef cook --release --target x86_64-unknown-linux-musl --recipe-path recipe.json
COPY . .
RUN cargo build --release --target x86_64-unknown-linux-musl --bin app
FROM alpine AS runtime
RUN addgroup -S myuser && adduser -S myuser -G myuser
COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/app /usr/local/bin/
USER myuser
ENV PORT=5000
ENV RUST_LOG=info
CMD ["/usr/local/bin/app"]
Before pushing your code to Github, you need to create recipe.json
using command:
bash
cargo chef prepare --recipe-path recipe.json
That's it!