-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: dockerfile * ci: dockerfile test * fix: dockerfile no Cargo.lock * feat(dockerfile): labels * fix(docker): binary name * fix(docker): higher sleep time * ci(docker): debug * fix(dockefile): let's do musl * ci(docker): publish to ghcr * fix(readme): add docker instructions
- Loading branch information
Showing
4 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
name: Build and Deploy Docker to GHCR | ||
|
||
# This workflow runs when any of the following occur: | ||
# - A push is made to a branch called `main` | ||
# - A tag starting with "v" is created | ||
on: | ||
push: | ||
branches: | ||
- main | ||
tags: | ||
- v* | ||
env: | ||
IMAGE_NAME: stoic_quotes | ||
# | ||
jobs: | ||
# This publishes the image to GitHub Packages. | ||
publish: | ||
runs-on: ubuntu-latest | ||
|
||
permissions: | ||
packages: write | ||
contents: read | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Build image | ||
run: docker build . --file Dockerfile --tag $IMAGE_NAME --label "runnumber=${GITHUB_RUN_ID}" | ||
|
||
- name: Log in to registry | ||
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $ --password-stdin | ||
|
||
- name: Push image | ||
run: | | ||
IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME | ||
# This changes all uppercase characters to lowercase. | ||
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') | ||
# This strips the git ref prefix from the version. | ||
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') | ||
# This strips the "v" prefix from the tag name. | ||
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') | ||
# This uses the Docker `latest` tag convention. | ||
[ "$VERSION" == "main" ] && VERSION=latest | ||
echo IMAGE_ID=$IMAGE_ID | ||
echo VERSION=$VERSION | ||
docker tag $IMAGE_NAME $IMAGE_ID:$VERSION | ||
docker push $IMAGE_ID:$VERSION | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: Build and Test Docker Image | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: {} | ||
workflow_dispatch: null | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Build Docker image | ||
run: docker build -t stoic_quotes . | ||
|
||
- name: Run Docker | ||
run: docker run -d --name stoic_quotes -p 3000:3000 stoic_quotes | ||
|
||
- name: Display Docker container logs | ||
run: docker logs stoic_quotes | ||
|
||
- name: Test Docker | ||
run: curl --fail localhost:3000/ || exit 1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Start from the official Rust image | ||
FROM rust:latest as builder | ||
|
||
# Install musl-tools | ||
RUN apt-get update && apt-get install -y musl-tools && rm -rf /var/lib/apt/lists/* | ||
|
||
# Target the musl architecture for a fully static binary | ||
RUN rustup target add x86_64-unknown-linux-musl | ||
|
||
# Set the working directory | ||
WORKDIR /usr/src/app | ||
|
||
# Add labels for OCI annotations | ||
LABEL org.opencontainers.image.source="https://github.com/storopoli/stoic-quotes" \ | ||
org.opencontainers.image.description="Stoic Quotes" \ | ||
org.opencontainers.image.licenses="MIT" | ||
|
||
# Copy project's Cargo.toml file | ||
COPY ./Cargo.toml ./ | ||
|
||
# This dummy build is to cache dependencies so they don't need to be rebuilt | ||
# every time your source changes | ||
RUN mkdir src/ && \ | ||
echo "fn main() {println!(\"if you see this, the build broke\")}" > src/main.rs && \ | ||
cargo build --release --target x86_64-unknown-linux-musl && \ | ||
rm -f target/x86_64-unknown-linux-musl/release/deps/stoic* | ||
|
||
# Copy project's source code and other relevant folders to the Docker image | ||
COPY ./src ./src | ||
COPY ./assets ./assets | ||
COPY ./data ./data | ||
COPY ./templates ./templates | ||
|
||
# Build application for release target musl | ||
RUN cargo build --release --target x86_64-unknown-linux-musl | ||
|
||
# Start a new stage from a slim version of Debian to reduce the size of the final image | ||
FROM debian:buster-slim | ||
|
||
# Copy the binary from the builder stage to the new stage | ||
COPY --from=builder /usr/src/app/target/x86_64-unknown-linux-musl/release/stoic-quotes /usr/local/bin/stoic-quotes | ||
|
||
# Expose port 3000 | ||
EXPOSE 3000 | ||
|
||
# Command to run the binary | ||
CMD ["stoic-quotes"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters