Modify docker compose for remote image and local build

This commit is contained in:
hkfires
2025-09-07 10:39:29 +08:00
parent 1bb0d11f62
commit 47b5ebfc43
8 changed files with 98 additions and 57 deletions
+36 -22
View File
@@ -6,31 +6,45 @@
# Stop script execution on any error
$ErrorActionPreference = "Stop"
# --- Step 1: Get Version Information ---
# Get the latest git tag or commit hash as the version string.
$VERSION = (git describe --tags --always --dirty)
# --- Step 1: Choose Environment ---
Write-Host "Please select your environment:"
Write-Host "1) Local Development (Build and Run)"
Write-Host "2) Remote Deployment (Run from Image)"
$choice = Read-Host -Prompt "Enter choice [1-2]"
# Get the short commit hash.
$COMMIT = (git rev-parse --short HEAD)
# --- Step 2: Execute based on choice ---
switch ($choice) {
"1" {
Write-Host "--- Starting Local Development Environment ---"
# Get the current UTC date and time in ISO 8601 format.
$BUILD_DATE = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
# Get Version Information
$VERSION = (git describe --tags --always --dirty)
$COMMIT = (git rev-parse --short HEAD)
$BUILD_DATE = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
Write-Host "--- Building with the following info ---"
Write-Host "Version: $VERSION"
Write-Host "Commit: $COMMIT"
Write-Host "Build Date: $BUILD_DATE"
Write-Host "----------------------------------------"
Write-Host "Building with the following info:"
Write-Host " Version: $VERSION"
Write-Host " Commit: $COMMIT"
Write-Host " Build Date: $BUILD_DATE"
Write-Host "----------------------------------------"
# --- Step 2: Build the Docker Image ---
# Pass the version information as build arguments to 'docker compose build'.
# These arguments are then used by the Dockerfile to inject them into the Go binary.
docker compose build --build-arg VERSION=$VERSION --build-arg COMMIT=$COMMIT --build-arg BUILD_DATE=$BUILD_DATE
# Build the Docker Image
docker compose build --build-arg VERSION=$VERSION --build-arg COMMIT=$COMMIT --build-arg BUILD_DATE=$BUILD_DATE
# --- Step 3: Start the Services ---
# Start the services in detached mode using the newly built image.
# '--remove-orphans' cleans up any containers for services that are no longer defined.
docker compose up -d --remove-orphans
# Start the Services
docker compose up -d --remove-orphans
Write-Host "Build complete. Services are starting."
Write-Host "Run 'docker compose logs -f' to see the logs."
Write-Host "Build complete. Services are starting."
Write-Host "Run 'docker compose logs -f' to see the logs."
}
"2" {
Write-Host "--- Starting Remote Deployment Environment ---"
docker compose -f docker-compose.yml -f docker-compose.remote.yml up -d
Write-Host "Services are starting from remote image."
Write-Host "Run 'docker compose logs -f' to see the logs."
}
default {
Write-Host "Invalid choice. Please enter 1 or 2."
exit 1
}
}