Files
skillshare/scripts/test_docker.sh
T
Willie c4a0c64911 feat(docker): add graceful shutdown, security hardening, production/CI images, and dev profile
Server improvements:
- Graceful shutdown with SIGTERM/SIGINT (10s grace period)
- HTTP timeouts (ReadHeader 5s, Read 15s, Write 30s, Idle 60s)
- Health endpoint now returns version and uptime

Compose security hardening:
- read_only filesystem, cap_drop ALL, no-new-privileges
- Resource limits (2 CPU / 2G memory), tmpfs for /tmp
- Playground healthcheck via /api/health

New Docker images:
- docker/production/ — lightweight debian-slim with tini, non-root user, healthcheck
- docker/ci/ — minimal image for skill validation in pipelines
- .github/workflows/docker-publish.yml — multi-arch GHCR publishing on tag push

Script improvements:
- DRY: test_docker.sh --online flag replaces duplicate test_docker_online.sh
- _sandbox_common.sh: Compose v2 version check with install suggestions
- .dockerignore: exclude website/, .github/, editor files

New targets:
- sandbox-status, dev-docker-up/down, docker-build, docker-build-multiarch

Docs updated: CLAUDE.md, README.md, website docker guide with use cases
2026-02-13 15:29:52 +08:00

84 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Run Go test pipeline inside the Docker sandbox.
# Supports both offline (default) and online modes.
set -euo pipefail
source "$(dirname "$0")/_sandbox_common.sh"
SKIP_BUILD=false
CUSTOM_CMD=""
ONLINE=false
usage() {
cat <<'EOF'
Usage: ./scripts/test_docker.sh [options]
Options:
--online Run network-dependent tests (online profile)
--skip-build Skip docker compose build
--cmd "<command>" Override default service command
-h, --help Show this help
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--online)
ONLINE=true
shift
;;
--skip-build)
SKIP_BUILD=true
shift
;;
--cmd)
if [[ $# -lt 2 ]]; then
echo "Error: --cmd requires a value" >&2
exit 1
fi
CUSTOM_CMD="$2"
shift 2
;;
--cmd=*)
CUSTOM_CMD="${1#--cmd=}"
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Error: unknown option '$1'" >&2
usage
exit 1
;;
esac
done
if [[ "$ONLINE" == "true" ]]; then
PROFILE="online"
SERVICE="sandbox-online"
DEFAULT_ONLINE_CMD="mkdir -p bin && go build -o bin/skillshare ./cmd/skillshare && SKILLSHARE_TEST_BINARY=/workspace/bin/skillshare go test -v -tags online ./tests/integration/... -timeout 120s"
else
PROFILE="offline"
SERVICE="sandbox-offline"
fi
require_docker
cd "$PROJECT_ROOT"
if [[ "$SKIP_BUILD" != "true" ]]; then
docker compose -f "$COMPOSE_FILE" --profile "$PROFILE" build "$SERVICE"
fi
# Ensure named cache volumes are writable when running as host UID/GID.
docker compose -f "$COMPOSE_FILE" --profile "$PROFILE" run --rm --user "0:0" "$SERVICE" bash -c "mkdir -p /go/pkg/mod /go/build-cache && chmod -R 0777 /go/pkg/mod /go/build-cache /tmp"
if [[ -n "$CUSTOM_CMD" ]]; then
docker compose -f "$COMPOSE_FILE" --profile "$PROFILE" run --rm --user "$(id -u):$(id -g)" "$SERVICE" bash -c "$CUSTOM_CMD"
elif [[ "$ONLINE" == "true" ]]; then
docker compose -f "$COMPOSE_FILE" --profile "$PROFILE" run --rm --user "$(id -u):$(id -g)" "$SERVICE" bash -c "$DEFAULT_ONLINE_CMD"
else
docker compose -f "$COMPOSE_FILE" --profile "$PROFILE" run --rm --user "$(id -u):$(id -g)" "$SERVICE"
fi