From abb00b768864a3bb2655e7844bf03bcd79c75fef Mon Sep 17 00:00:00 2001 From: Ben Binder Date: Fri, 7 Mar 2025 21:10:03 -0600 Subject: [PATCH] Implement deploy strategy for local development; refactor container to use startup script. --- .gitignore | 1 + .woodpecker.yml | 12 ++++++++--- Dockerfile | 32 ----------------------------- docker/Dockerfile | 38 +++++++++++++++++++++++++++++++++++ docker/docker-compose.dev.yml | 17 ++++++++++++++++ scripts/dev.sh | 12 +++++++++++ scripts/startup.sh | 6 ++++++ main.go => src/main.go | 0 8 files changed, 83 insertions(+), 35 deletions(-) create mode 100644 .gitignore delete mode 100644 Dockerfile create mode 100644 docker/Dockerfile create mode 100644 docker/docker-compose.dev.yml create mode 100755 scripts/dev.sh create mode 100755 scripts/startup.sh rename main.go => src/main.go (100%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1c2f433 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tmp \ No newline at end of file diff --git a/.woodpecker.yml b/.woodpecker.yml index cb72ac4..69741bc 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -7,8 +7,11 @@ steps: password: from_secret: REGISTRY_PASSWORD registry: registry.binderlab.io - repo: registry.binderlab.io/harbormaster + repo: registry.binderlab.io/harbormaster-server tags: latest + file: docker/Dockerfile + context: . + push: true when: event: manual @@ -19,19 +22,22 @@ steps: from_secret: REGISTRY_USERNAME REGISTRY_PASSWORD: from_secret: REGISTRY_PASSWORD + RESET_DB: + from_secret: RESET_DB settings: host: binderlab.io username: binderb envs: - REGISTRY_USERNAME - REGISTRY_PASSWORD + - RESET_DB key: from_secret: SSH_PRIVATE_KEY script: - echo "$REGISTRY_PASSWORD" | docker login registry.binderlab.io --username "$REGISTRY_USERNAME" --password-stdin - - docker pull registry.binderlab.io/harbormaster:latest + - docker pull registry.binderlab.io/harbormaster-server:latest - docker stop harbormaster || true - docker rm harbormaster || true - - docker run -d --name harbormaster -p 7080:8080 registry.binderlab.io/harbormaster:latest + - docker run -d --name harbormaster -p 7080:8080 -e RESET_DB $RESET_DB registry.binderlab.io/harbormaster:latest when: event: manual \ No newline at end of file diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 66149b4..0000000 --- a/Dockerfile +++ /dev/null @@ -1,32 +0,0 @@ -# Use a small Go base image -FROM golang:1.24 as builder - -# Set working directory -WORKDIR /app - -# Copy go mod and sum files -COPY go.mod go.sum ./ - -# Download dependencies -RUN go mod download - -# Copy the rest of the application -COPY . . - -# Build the application -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o harbormaster . - -# Use a small base image for the final container -FROM alpine:latest - -# Set working directory -WORKDIR /root/ - -# Copy the compiled binary from the builder stage -COPY --from=builder /app/harbormaster . - -# Expose the port the app runs on -EXPOSE 8080 - -# Run the binary -CMD ["/root/harbormaster"] \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..a2074cb --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,38 @@ +# Use a small Go base image +FROM golang:1.24 AS builder + +# Set working directory +WORKDIR /root + +# Copy go mod and sum files +COPY ../go.mod ../go.sum ./ + +# Download dependencies +RUN go mod download + +# Copy the rest of the application +COPY ../src/ ./src/ +COPY ../scripts/ ./scripts/ +RUN chmod +x /root/scripts/startup.sh + +# Build the application +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o harbormaster ./src/ + +# Use a small base image for the final container +FROM alpine:latest + +# Set working directory +WORKDIR /root + +# Copy the compiled binary from the builder stage +COPY --from=builder /root/harbormaster . +#COPY database/gold.db /root/database/gold.db +#COPY migrations /root/migrations +COPY ../scripts/ /root/scripts/ +RUN chmod +x /root/scripts/startup.sh + +# Expose the port the app runs on +EXPOSE 8080 + +# Run database setup and the binary +CMD ["/bin/ash", "-c", "/root/scripts/startup.sh"] \ No newline at end of file diff --git a/docker/docker-compose.dev.yml b/docker/docker-compose.dev.yml new file mode 100644 index 0000000..942e8f7 --- /dev/null +++ b/docker/docker-compose.dev.yml @@ -0,0 +1,17 @@ +services: + app: + container_name: harbormaster-server + image: harbormaster-server + build: + context: .. + dockerfile: docker/Dockerfile + target: builder + volumes: + - ../src:/root/src + - ../scripts:/root/scripts + ports: + - "7080:8080" + environment: + - ENV=development + # command: ["./harbormaster"] + command: ["/bin/bash","/root/scripts/startup.sh"] \ No newline at end of file diff --git a/scripts/dev.sh b/scripts/dev.sh new file mode 100755 index 0000000..4973adc --- /dev/null +++ b/scripts/dev.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# cleanup() { +# echo "Stopping Docker containers..." +# docker-compose down +# } + +# trap cleanup EXIT SIGINT SIGTERM +cd ../ +echo "Starting application..." +docker compose -p harbormaster-server -f docker/docker-compose.dev.yml up -d --build +echo "Tailing logs..." +docker logs harbormaster-server -f \ No newline at end of file diff --git a/scripts/startup.sh b/scripts/startup.sh new file mode 100755 index 0000000..b0a7ed7 --- /dev/null +++ b/scripts/startup.sh @@ -0,0 +1,6 @@ +#!/bin/bash +cd /root + +echo "Starting application..." +chmod +x ./harbormaster +./harbormaster \ No newline at end of file diff --git a/main.go b/src/main.go similarity index 100% rename from main.go rename to src/main.go