diff --git a/.github/workflows/build-and-push-docker.yaml b/.github/workflows/build-and-push-docker.yaml index 2371709..0200509 100644 --- a/.github/workflows/build-and-push-docker.yaml +++ b/.github/workflows/build-and-push-docker.yaml @@ -24,13 +24,20 @@ jobs: - name: 🏗️ Set Build Variables run: | echo "GIT_COMMIT_SHA=${{ github.sha }}" >> $GITHUB_ENV - echo "GIT_VERSION=${{ github.ref_name }}" >> $GITHUB_ENV + echo "GIT_VERSION=$(git describe --tags --always --long --dirty)" >> $GITHUB_ENV + DATE_TAG=$(date +"%d%m%Y") + echo "DATE_TAG=${DATE_TAG}" >> $GITHUB_ENV + if [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == "refs/heads/main" ]]; then - echo "IMAGE_TAG=latest" >> $GITHUB_ENV + echo "IMAGE_TAGS=latest,${DATE_TAG}" >> $GITHUB_ENV + echo "CACHE_TO=" >> $GITHUB_ENV elif [[ "${{ github.event_name }}" == "pull_request" ]]; then - echo "IMAGE_TAG=rc-${{ github.event.pull_request.number }}" >> $GITHUB_ENV + PR_NUMBER=${{ github.event.pull_request.number }} + echo "IMAGE_TAGS=rc-${PR_NUMBER}-${DATE_TAG}" >> $GITHUB_ENV + echo "CACHE_TO=type=inline" >> $GITHUB_ENV else - echo "IMAGE_TAG=unknown" >> $GITHUB_ENV + echo "IMAGE_TAGS=unknown" >> $GITHUB_ENV + echo "CACHE_TO=" >> $GITHUB_ENV fi - name: 🛠️ Set up Docker Buildx @@ -49,15 +56,17 @@ jobs: context: . file: ./Dockerfile push: true - tags: ghcr.io/kek-sec/gopherdrop:${{ env.IMAGE_TAG }} + tags: | + ghcr.io/kek-sec/gopherdrop:${{ env.IMAGE_TAGS }} annotations: | org.opencontainers.image.description=GopherDrop, a secure one-time secret sharing service build-args: | GIT_COMMIT_SHA=${{ env.GIT_COMMIT_SHA }} GIT_VERSION=${{ env.GIT_VERSION }} + DATE_TAG=${{ env.DATE_TAG }} labels: | org.opencontainers.image.source=https://github.com/kek-sec/gopherdrop org.opencontainers.image.revision=${{ env.GIT_COMMIT_SHA }} org.opencontainers.image.version=${{ env.GIT_VERSION }} cache-from: type=registry,ref=ghcr.io/kek-sec/gopherdrop:latest - cache-to: type=inline + cache-to: ${{ env.CACHE_TO }} diff --git a/Dockerfile b/Dockerfile index b7ab8d9..054b22b 100755 --- a/Dockerfile +++ b/Dockerfile @@ -4,29 +4,39 @@ FROM golang:1.22-alpine AS backend-builder WORKDIR /app COPY . . +# Generate Go version tag based on the current date in ddMMyyyy format +ARG DATE_TAG +ENV DATE_TAG=${DATE_TAG} + # Add build arguments for debug mode and versioning ARG DEBUG=false +ARG GIN_MODE=release ARG GIT_COMMIT_SHA ARG GIT_VERSION +ENV GIN_MODE=${GIN_MODE} -# Set build tags based on the DEBUG flag +# Set build tags based on the DEBUG flag and include versioning information RUN if [ "$DEBUG" = "true" ]; then \ - go mod download && go build -o server -tags debug ./cmd/server/main.go; \ + go mod download && go build -o server -tags debug -ldflags="-X main.version=DEBUG" ./cmd/server/main.go; \ else \ - go mod download && go build -o server ./cmd/server/main.go; \ + go mod download && go build -o server -ldflags="-X main.version=${DATE_TAG}-${GIT_VERSION}-${GIT_COMMIT_SHA}" ./cmd/server/main.go; \ fi # Stage 2: Build the Vue.js Frontend FROM node:18-alpine AS frontend-builder WORKDIR /app -COPY ui/package.json ui/package-lock.json ./ +COPY ui/package.json ui/package-lock.json ./ RUN npm install --legacy-peer-deps +# Add build argument for the API URL and versioning ARG VITE_API_URL="/api" +ARG GIT_VERSION +ARG GIT_COMMIT_SHA ENV VITE_API_URL=${VITE_API_URL} +ENV VITE_APP_VERSION=${GIT_VERSION}-${GIT_COMMIT_SHA} -COPY ui ./ +COPY ui ./ RUN npm run build # Stage 3: Combine Backend and Frontend into a Single Image diff --git a/Makefile b/Makefile index 9bfad4b..3227cd9 100755 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ build: # Build in debug mode build-debug: - docker-compose build --build-arg DEBUG=true + docker-compose build --build-arg DEBUG=true --build-arg GIN_MODE=debug up: docker-compose up -d @@ -35,6 +35,3 @@ ui-build: ui-up: cd ui && npm run dev - -ui-down: - docker-compose stop ui diff --git a/cmd/server/main.go b/cmd/server/main.go index dd2eef3..50d6b2a 100755 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -11,8 +11,11 @@ import ( "github.com/kek-Sec/gopherdrop/internal/routes" ) +var version = "dev" + // main initializes configuration, database, and the HTTP server. func main() { + log.Printf("Starting GopherDrop: %s", version) cfg := config.LoadConfig() db := database.InitDB(cfg) db.AutoMigrate(&models.Send{})