feat(ci): add pre-commit check script using wirelos/pre-commit image
This commit is contained in:
68
scripts/pre-commit-check.sh
Executable file
68
scripts/pre-commit-check.sh
Executable file
@@ -0,0 +1,68 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Pre-commit check script that runs lint, fmt-check, test, and build in gitea-runner container
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||||
|
IMAGE_NAME="wirelos/pre-commit"
|
||||||
|
CONTAINER_NAME="goplt-pre-commit-check"
|
||||||
|
|
||||||
|
echo "🔍 Checking for Docker image: $IMAGE_NAME"
|
||||||
|
if ! docker images --format "{{.Repository}}:{{.Tag}}" | grep -q "^${IMAGE_NAME}:latest$"; then
|
||||||
|
echo "📦 Image not found. Building $IMAGE_NAME from ci/pre-commit/Dockerfile..."
|
||||||
|
docker build -t "$IMAGE_NAME:latest" -f "$PROJECT_ROOT/ci/pre-commit/Dockerfile" "$PROJECT_ROOT/ci/pre-commit" || {
|
||||||
|
echo "❌ Failed to build Docker image"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
echo "✅ Image built successfully"
|
||||||
|
else
|
||||||
|
echo "✅ Image found locally"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "🧹 Cleaning up any existing container..."
|
||||||
|
docker rm -f "$CONTAINER_NAME" 2>/dev/null || true
|
||||||
|
|
||||||
|
echo "🚀 Starting pre-commit container..."
|
||||||
|
docker run --rm \
|
||||||
|
--name "$CONTAINER_NAME" \
|
||||||
|
-v "$PROJECT_ROOT:/workspace" \
|
||||||
|
-w /workspace \
|
||||||
|
"$IMAGE_NAME:latest" \
|
||||||
|
sh -c "
|
||||||
|
echo '📦 Installing Go and required tools...'
|
||||||
|
apk add --no-cache go protobuf protobuf-dev bash git || exit 1
|
||||||
|
|
||||||
|
echo '📥 Installing Go protobuf plugins...'
|
||||||
|
export PATH=\$PATH:/root/go/bin
|
||||||
|
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest || exit 1
|
||||||
|
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest || exit 1
|
||||||
|
|
||||||
|
echo '📥 Installing golangci-lint...'
|
||||||
|
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /root/go/bin || exit 1
|
||||||
|
|
||||||
|
echo '📋 Running make fmt-check...'
|
||||||
|
make fmt-check || exit 1
|
||||||
|
|
||||||
|
echo '🔍 Running make lint...'
|
||||||
|
make lint || exit 1
|
||||||
|
|
||||||
|
echo '🧪 Running make test...'
|
||||||
|
make test || exit 1
|
||||||
|
|
||||||
|
echo '🔨 Running make build...'
|
||||||
|
make build || exit 1
|
||||||
|
|
||||||
|
echo '✅ All checks passed!'
|
||||||
|
"
|
||||||
|
|
||||||
|
EXIT_CODE=$?
|
||||||
|
|
||||||
|
if [ $EXIT_CODE -eq 0 ]; then
|
||||||
|
echo "✅ All pre-commit checks passed!"
|
||||||
|
else
|
||||||
|
echo "❌ Pre-commit checks failed. Please fix the issues above."
|
||||||
|
fi
|
||||||
|
|
||||||
|
exit $EXIT_CODE
|
||||||
|
|
||||||
Reference in New Issue
Block a user