Skip to content

Commit

Permalink
Feat/go versioning (#6)
Browse files Browse the repository at this point in the history
* Enhance Dockerfile to include versioning information in build tags and environment variables

* Log application version on startup

* Update GitHub Actions workflow to enhance Docker image tagging with versioning

* Add GIN_MODE build argument for debug mode in Makefile

* Add GIN_MODE build argument to Dockerfile for release mode

* Update GitHub Actions workflow to dynamically set CACHE_TO for Docker caching

* Update Docker workflow and Dockerfile to include date-based version tagging

* Refactor Dockerfile to use ARG for date-based Go version tagging

* Update Dockerfile and workflow to use DATE_TAG for versioning
  • Loading branch information
kek-Sec authored Dec 19, 2024
1 parent aa716b2 commit 04232b5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
21 changes: 15 additions & 6 deletions .github/workflows/build-and-push-docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}
20 changes: 15 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -35,6 +35,3 @@ ui-build:

ui-up:
cd ui && npm run dev

ui-down:
docker-compose stop ui
3 changes: 3 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down

0 comments on commit 04232b5

Please sign in to comment.