Skip to content

Commit

Permalink
chore(github): clean actions and fix docker action
Browse files Browse the repository at this point in the history
  • Loading branch information
developerfred committed Dec 2, 2024
1 parent 905852d commit ba05007
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 79 deletions.
54 changes: 9 additions & 45 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
name: Docker CI/CD
name: Docker Build and Push

on:
push:
branches: [ "main" ]
branches: [ main ]
tags: [ 'v*.*.*' ]
pull_request:
branches: [ "main" ]
branches: [ main ]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-test:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
Expand All @@ -21,9 +22,6 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4

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

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
Expand All @@ -43,48 +41,14 @@ jobs:
type=semver,pattern={{major}}.{{minor}}
type=sha
- name: Build and test Docker image
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64
push: false
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
target: builder
load: true

- name: Test the image
run: |
docker compose -f docker-compose.yml run test
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Build and push final multi-platform image
uses: docker/build-push-action@v5
if: github.event_name != 'pull_request'
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3


- name: Build final image for PR
- name: Build and push Docker image
uses: docker/build-push-action@v5
if: github.event_name == 'pull_request'
with:
context: .
platforms: linux/amd64
push: false
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
Expand Down
34 changes: 25 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Builder stage
FROM rustlang/rust:nightly AS builder


ENV CARGO_HOME=/usr/local/cargo
ENV RUSTUP_HOME=/usr/local/rustup
ENV PATH=/usr/local/cargo/bin:$PATH
ENV RUST_LOG=info

ENV RUST_LOG=debug
ENV RUST_BACKTRACE=full
ENV RUST_LIB_BACKTRACE=1

RUN apt-get update && \
apt-get install -y \
Expand All @@ -25,28 +25,26 @@ RUN apt-get update && \

WORKDIR /app

# Create logs directory
RUN mkdir -p /app/logs

COPY Cargo.toml Cargo.lock* ./


RUN mkdir src && \
echo 'fn main() { println!("dummy") }' > src/main.rs && \
cargo build --release && \
cargo build --tests && \
rm -rf src target/release/deps/titanium*


COPY src src/
COPY rust-toolchain.toml .


RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/app/target \
cargo build --release && \
cargo test --no-run && \
cp target/release/titanium /app/titanium


FROM debian:bookworm-slim

RUN apt-get update && \
Expand All @@ -65,12 +63,29 @@ RUN apt-get update && \
libxdo3 \
&& rm -rf /var/lib/apt/lists/*

# Create logs directory and set permissions
RUN mkdir -p /app/logs && \
chown -R nobody:nogroup /app/logs

COPY --from=builder /app/titanium /usr/local/bin/titanium

ENV RUST_LOG=debug
ENV RUST_BACKTRACE=full
ENV RUST_LIB_BACKTRACE=1

# Add logging script
COPY <<'EOF' /usr/local/bin/start.sh
#!/bin/bash
set -e

ENV RUST_LOG=info
# Redirect stdout/stderr to both console and file
exec &> >(tee -a "/app/logs/titanium.log")

# Start the application
exec titanium
EOF

RUN chmod +x /usr/local/bin/start.sh

COPY <<'EOF' /usr/local/bin/healthcheck.sh
#!/bin/bash
Expand All @@ -82,6 +97,7 @@ RUN chmod +x /usr/local/bin/healthcheck.sh
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD ["/usr/local/bin/healthcheck.sh"]

VOLUME ["/app/logs"]
EXPOSE 3000

CMD ["titanium"]
CMD ["/usr/local/bin/start.sh"]
42 changes: 36 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
services:
app:
image: ${REGISTRY:-ghcr.io}/${IMAGE_NAME:developerfred/titanium}:${TAG:-latest}
image: ${REGISTRY:-ghcr.io}/${IMAGE_NAME:-developerfred/titanium}:${TAG:-latest}
build:
context: .
args:
RUST_LOG: ${RUST_LOG:-info}
RUST_LOG: ${RUST_LOG:-debug}
ports:
- "3000:3000"
environment:
- RUST_LOG=${RUST_LOG:-info}
- RUST_BACKTRACE=1
- RUST_LOG=${RUST_LOG:-debug}
- RUST_BACKTRACE=full
- RUST_LIB_BACKTRACE=1
logging:
driver: "json-file"
options:
max-size: "100m"
max-file: "5"
compress: "true"
mode: "non-blocking"
tag: "{{.Name}}"
restart: unless-stopped
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
deploy:
resources:
limits:
memory: 1G
reservations:
memory: 512M
volumes:
- ./logs:/app/logs

test:
build:
Expand All @@ -24,11 +42,23 @@ services:
command: cargo test --all-features -- --nocapture
environment:
- RUST_LOG=debug
- RUST_BACKTRACE=1
- RUST_BACKTRACE=full
- RUST_LIB_BACKTRACE=1
volumes:
- .:/usr/src/app
- cargo-cache:/usr/local/cargo/registry
- ./logs:/app/logs
logging:
driver: "json-file"
options:
max-size: "100m"
max-file: "5"
compress: "true"
mode: "non-blocking"
tag: "{{.Name}}"
profiles: ["test"]

volumes:
cargo-cache: {}
cargo-cache: {}
logs:
driver: local
Loading

0 comments on commit ba05007

Please sign in to comment.