run migrations when deploying container

This commit is contained in:
2025-01-11 03:01:40 +01:00
parent 062e055813
commit ca29a92ae0
2 changed files with 36 additions and 12 deletions

View File

@@ -5,6 +5,14 @@ ENV CGO_ENABLED=0
ENV GOOS=linux ENV GOOS=linux
ENV GOARCH=amd64 ENV GOARCH=amd64
# Install goose
WORKDIR /goose.git
RUN git clone https://github.com/pressly/goose.git .
RUN go build -tags='no_clickhouse no_libsql no_mssql no_mysql no_sqlite3 no_vertica no_ydb' \
-o goose ./cmd/goose
# Build app
WORKDIR /app WORKDIR /app
# Copy the Go modules and dependencies # Copy the Go modules and dependencies
@@ -20,22 +28,17 @@ COPY ./src/internal ./internal
RUN go build -o main cmd/frontend/* RUN go build -o main cmd/frontend/*
# --- Stage 2: Run the application --- # --- Stage 3: Run the application ---
FROM alpine:latest FROM alpine:latest
ENV DB_HOST="10.99.0.3"
ENV DB_PORT="5432"
ENV DB_NAME="crowsnest"
ENV DB_USER="crow"
ENV DB_PASS=""
WORKDIR /app WORKDIR /app
# Copy the built binary from the builder stage # Copy the built binary from the builder stage
COPY --from=builder /app/main . COPY --from=builder /app/main .
COPY --from=builder /goose.git/goose .
COPY ./src/assets ./assets COPY ./src/assets ./assets
EXPOSE 8080 EXPOSE 8080
# Command to run the application # Command to run the application
CMD ["/app/main"] CMD ["sh", "-c", "/app/goose -dir=/app/assets/migrations/ postgres postgresql://$DB_USER:$DB_PASS@$DB_HOST:$DB_PORT/$DB_NAME up && /app/main"]

View File

@@ -1,9 +1,30 @@
DB_HOST="10.99.0.3"
DB_PORT="5432"
DB_NAME="crowsnest"
DB_USER="crow"
DB_PASS="4LlKpnQ2RZPzL13BSpkW4k"
go-run: go-run:
cd src; DB_USER=crow DB_PASS=4LlKpnQ2RZPzL13BSpkW4k DB_NAME=crowsnest DB_HOST=10.99.0.3 go run cmd/frontend/* cd src; DB_USER=$(DB_USER) DB_PASS=$(DB_PASS) DB_NAME=$(DB_NAME) DB_HOST=$(DB_HOST) go run cmd/frontend/*
docker-push: docker-build
docker push git.kohout-dev.de/crowsnest/crowsnest:latest
docker-run: docker-build docker-run: docker-build
docker run --rm -p 8080:8080 --env DB_PASS="4LlKpnQ2RZPzL13BSpkW4k" git.kohout-dev.de/crowsnest/crowsnest:latest docker run \
--env DB_HOST=$(DB_HOST) \
--env DB_PORT=$(DB_PORT) \
--env DB_NAME=$(DB_NAME) \
--env DB_USER=$(DB_USER) \
--env DB_PASS=$(DB_PASS) \
--rm -p 8080:8080 git.kohout-dev.de/crowsnest/crowsnest:latest
docker-build: docker-build:
docker build --platform linux/amd64 -t git.kohout-dev.de/crowsnest/crowsnest:latest . \ docker build \
&& docker push git.kohout-dev.de/crowsnest/crowsnest:latest --build-arg DB_HOST=$(DB_HOST) \
--build-arg DB_PORT=$(DB_PORT) \
--build-arg DB_NAME=$(DB_NAME) \
--build-arg DB_USER=$(DB_USER) \
--build-arg DB_PASS=$(DB_PASS) \
--platform linux/amd64 \
-t git.kohout-dev.de/crowsnest/crowsnest:latest .