From d05f91b56d1a78d2ab40d685e945cb89554646ec Mon Sep 17 00:00:00 2001 From: Tordarus Date: Fri, 6 Jun 2025 14:08:21 +0200 Subject: [PATCH] initial commit --- Dockerfile | 20 ++++++++++++++++++++ README.md | 39 +++++++++++++++++++++++++++++++++++++++ entrypoint.sh | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 entrypoint.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b485515 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM golang:alpine + +# Install required tools +RUN apk add --no-cache git ca-certificates bash + +# Set working directory +WORKDIR /app + +# Copy entrypoint script +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh + +# Default environment values (can be overridden at runtime) +ENV GIT_REPO="" +ENV APP_PATH="." +ENV GIT_USERNAME="" +ENV GIT_TOKEN="" +ENV PACKAGES="" + +ENTRYPOINT ["/entrypoint.sh"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..fa89124 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# ๐Ÿณ Go Builder & Runner Docker Image + +This Docker image is designed to **download**, **compile**, and **run** a Go program from a Git repository entirely at **container runtime**. It supports both public and private repositories and works seamlessly across multiple architectures (`amd64`, `arm64`). + +--- + +## โœ… Features + +- โœ… Clones a Go repository at runtime +- โœ… Supports **private Git repositories** using `.netrc`-based auth +- โœ… Builds and runs your Go application +- โœ… Multi-architecture: `linux/amd64`, `linux/arm64` +- โœ… Minimal and reproducible container setup + +--- + +## ๐Ÿงช Example Usage + +```bash +docker run --rm \ + -e GIT_REPO=https://github.com/youruser/your-go-app.git \ + -e GIT_USERNAME=yourusername \ + -e GIT_TOKEN=yourtoken \ + gorunner:latest +``` + +## ๐Ÿ”ง Environment Variables + +| Variable | Required | Description | +| ---------------------- | -------- | ----------------------------------------------------------------------- | +| `GIT_REPO` | โœ… Yes | The HTTPS Git URL of your Go project | +| `GIT_USERNAME` | Optional | Your Git username (required for private repos) | +| `GIT_TOKEN` | Optional | A personal access token or password (required for private repos) | +| `APP_PATH` | Optional | Relative path to the Go module directory inside the repo (default: `.`) | +| `PACKAGES` | Optional | Comma-separated list of packages to install with `apk` before execution | + +## ๐Ÿ“„ License + +MIT License โ€“ feel free to modify and use. diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..690fd06 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,50 @@ +#!/bin/bash +set -e + +error() { + echo "[ERROR] $1" >&2 + exit 1 +} + +if [ -z "$GIT_REPO" ]; then + error "GIT_REPO is not set." +fi + +# Install runtime packages if specified +if [ -n "$PACKAGES" ]; then + echo "[PKG] Installing additional APK packages: $PACKAGES" + apk update >/dev/null + apk add --no-cache $(echo "$PACKAGES" | tr ',' ' ') +fi + +# Extract Git host +GIT_HOST=$(echo "$GIT_REPO" | awk -F/ '{print $3}') + +# Configure .netrc +if [ -n "$GIT_USERNAME" ] && [ -n "$GIT_TOKEN" ]; then + echo "[AUTH] Configuring .netrc for Git authentication..." + cat < ~/.netrc +machine $GIT_HOST +login $GIT_USERNAME +password $GIT_TOKEN +EOF + chmod 600 ~/.netrc + + # Prevent Git from prompting and force it to use .netrc + export GIT_ASKPASS=true + export GIT_TERMINAL_PROMPT=0 +fi + +# Clean previous checkout +rm -rf /app/repo + +echo "[CLONE] Cloning repository: $GIT_REPO" +git clone "$GIT_REPO" repo + +cd repo/"${APP_PATH:-.}" + +echo "[BUILD] Building..." +go build -o /app/app . + +echo "[RUN] Starting application..." +exec /app/app