Files
nerd-monitor/.gitea/workflows/release.yml
Ducky SSH User 2075cd2901 Fix Docker builds in Gitea runner with proper Docker-in-Docker detection
- Use 'docker info' instead of 'command -v docker' for reliable detection
- Add 30-second wait for Docker daemon startup (for DinD startup delay)
- Improve Docker build step with better error handling
- Build Docker images when available, skip gracefully if not
- Add comprehensive GITEA_RUNNER_DOCKER.md setup guide
- Document Docker socket mounting for runners
- Include troubleshooting and complete docker-compose example
2025-12-20 06:16:10 +00:00

271 lines
10 KiB
YAML

name: Build and Release
on:
push:
branches:
- main
- master
tags:
- 'v*'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
run: |
cd $GITHUB_WORKSPACE
git init
git remote add origin ${{ github.server_url }}/${{ github.repository }}.git
git fetch origin ${{ github.ref }}
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
git checkout ${{ github.ref_name }}
else
git checkout -b ${{ github.ref_name }} origin/${{ github.ref_name }}
fi
- name: Set up Go
run: |
wget https://go.dev/dl/go1.24.4.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.24.4.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
go version
- name: Generate version
id: version
run: |
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
VERSION=${{ github.ref_name }}
else
VERSION=dev-${{ github.sha }}
fi
echo "version=${VERSION}" >> $GITHUB_ENV
- name: Build all binaries
run: |
export PATH=$PATH:/usr/local/go/bin
mkdir -p bin
# Generate templ first
go run github.com/a-h/templ/cmd/templ@latest generate
# Linux AMD64
echo "Building Linux AMD64..."
GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o bin/nerd-monitor-server-linux-amd64 ./cmd/server
GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o bin/nerd-monitor-agent-linux-amd64 ./cmd/agent
# Linux ARM64
echo "Building Linux ARM64..."
GOOS=linux GOARCH=arm64 go build -ldflags="-w -s" -o bin/nerd-monitor-server-linux-arm64 ./cmd/server
GOOS=linux GOARCH=arm64 go build -ldflags="-w -s" -o bin/nerd-monitor-agent-linux-arm64 ./cmd/agent
# macOS AMD64
echo "Building macOS AMD64..."
GOOS=darwin GOARCH=amd64 go build -ldflags="-w -s" -o bin/nerd-monitor-server-darwin-amd64 ./cmd/server
GOOS=darwin GOARCH=amd64 go build -ldflags="-w -s" -o bin/nerd-monitor-agent-darwin-amd64 ./cmd/agent
# macOS ARM64
echo "Building macOS ARM64..."
GOOS=darwin GOARCH=arm64 go build -ldflags="-w -s" -o bin/nerd-monitor-server-darwin-arm64 ./cmd/server
GOOS=darwin GOARCH=arm64 go build -ldflags="-w -s" -o bin/nerd-monitor-agent-darwin-arm64 ./cmd/agent
# Windows AMD64
echo "Building Windows AMD64..."
GOOS=windows GOARCH=amd64 go build -ldflags="-w -s" -o bin/nerd-monitor-server-windows-amd64.exe ./cmd/server
GOOS=windows GOARCH=amd64 go build -ldflags="-w -s" -o bin/nerd-monitor-agent-windows-amd64.exe ./cmd/agent
echo "Build complete! Files:"
ls -lh bin/
- name: Create checksums
run: |
cd bin
sha256sum * > SHA256SUMS
cd ..
echo "Checksums:"
cat bin/SHA256SUMS
- name: Create Release and Upload
if: startsWith(github.ref, 'refs/tags/')
run: |
export GITEA_TOKEN="${{ secrets.GITEA_TOKEN }}"
export GITEA_URL="${{ github.server_url }}"
export REPO_OWNER="${{ github.repository_owner }}"
export REPO_NAME="${{ github.repository }}"
export REPO_NAME=${REPO_NAME#*/}
TAG=${{ github.ref_name }}
echo "Creating release for tag: $TAG"
echo "Repository: $REPO_OWNER/$REPO_NAME"
# Get or create release
RELEASE_JSON=$(curl -s -X GET \
-H "Authorization: token $GITEA_TOKEN" \
"$GITEA_URL/api/v1/repos/$REPO_OWNER/$REPO_NAME/releases/tags/$TAG" 2>/dev/null || echo "{}")
RELEASE_ID=$(echo "$RELEASE_JSON" | jq -r '.id // empty' 2>/dev/null)
if [ -z "$RELEASE_ID" ]; then
echo "Creating new release..."
RELEASE_JSON=$(curl -s -X POST \
-H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"tag_name\":\"$TAG\",\"name\":\"Release $TAG\",\"draft\":false,\"prerelease\":false}" \
"$GITEA_URL/api/v1/repos/$REPO_OWNER/$REPO_NAME/releases")
RELEASE_ID=$(echo "$RELEASE_JSON" | jq -r '.id')
echo "Created release ID: $RELEASE_ID"
else
echo "Using existing release ID: $RELEASE_ID"
fi
# Upload all binaries
echo "Uploading release artifacts..."
for file in bin/*; do
if [ -f "$file" ]; then
filename=$(basename "$file")
echo " Uploading: $filename"
curl -s -X POST \
-H "Authorization: token $GITEA_TOKEN" \
-F "attachment=@$file" \
"$GITEA_URL/api/v1/repos/$REPO_OWNER/$REPO_NAME/releases/$RELEASE_ID/assets" > /dev/null
if [ $? -eq 0 ]; then
echo " ✓ $filename uploaded"
else
echo " ✗ Failed to upload $filename"
fi
fi
done
echo ""
echo "Release completed!"
echo "View at: $GITEA_URL/$REPO_OWNER/$REPO_NAME/releases/tag/$TAG"
docker-build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
run: |
cd $GITHUB_WORKSPACE
git init
git remote add origin ${{ github.server_url }}/${{ github.repository }}.git
git fetch origin ${{ github.ref }}
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
git checkout ${{ github.ref_name }}
else
git checkout -b ${{ github.ref_name }} origin/${{ github.ref_name }}
fi
- name: Check Docker availability
run: |
# Wait for Docker daemon to be ready (Docker-in-Docker)
echo "Waiting for Docker daemon..."
MAX_ATTEMPTS=30
ATTEMPT=0
while [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do
if docker info &>/dev/null; then
echo "✓ Docker is ready"
docker --version
exit 0
fi
ATTEMPT=$((ATTEMPT + 1))
echo "Attempt $ATTEMPT/$MAX_ATTEMPTS - Docker not ready yet, waiting..."
sleep 1
done
echo "⚠️ Warning: Docker daemon is not available"
echo "Docker images will not be built"
- name: Generate version
id: version
run: |
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
VERSION=${{ github.ref_name }}
else
VERSION=dev-${{ github.sha }}
fi
echo "version=${VERSION}" >> $GITHUB_ENV
- name: Build Docker images
run: |
# Check if Docker is available
if ! docker info &>/dev/null; then
echo "⚠️ Docker daemon is not available, skipping Docker image build"
echo "Binary builds were completed successfully"
exit 0
fi
mkdir -p /tmp/docker-images
echo "Building server image..."
docker build -t nerd-monitor-server:${{ env.version }} -f Dockerfile.server . 2>&1
if [ $? -eq 0 ]; then
docker save nerd-monitor-server:${{ env.version }} -o /tmp/docker-images/nerd-monitor-server-${{ env.version }}.tar
echo "✓ Server image built and saved"
else
echo "✗ Failed to build server image"
exit 1
fi
echo "Building agent image..."
docker build -t nerd-monitor-agent:${{ env.version }} -f Dockerfile.agent . 2>&1
if [ $? -eq 0 ]; then
docker save nerd-monitor-agent:${{ env.version }} -o /tmp/docker-images/nerd-monitor-agent-${{ env.version }}.tar
echo "✓ Agent image built and saved"
else
echo "✗ Failed to build agent image"
exit 1
fi
echo ""
echo "Docker images built successfully:"
ls -lh /tmp/docker-images/
- name: Upload Docker images to release
if: startsWith(github.ref, 'refs/tags/')
run: |
# Skip if no Docker images were built
if [ ! -d /tmp/docker-images ] || [ -z "$(ls -A /tmp/docker-images/)" ]; then
echo "No Docker images to upload (Docker not available on runner)"
exit 0
fi
export GITEA_TOKEN="${{ secrets.GITEA_TOKEN }}"
export GITEA_URL="${{ github.server_url }}"
export REPO_OWNER="${{ github.repository_owner }}"
export REPO_NAME="${{ github.repository }}"
export REPO_NAME=${REPO_NAME#*/}
TAG=${{ github.ref_name }}
# Get existing release
RELEASE_JSON=$(curl -s -X GET \
-H "Authorization: token $GITEA_TOKEN" \
"$GITEA_URL/api/v1/repos/$REPO_OWNER/$REPO_NAME/releases/tags/$TAG" 2>/dev/null || echo "{}")
RELEASE_ID=$(echo "$RELEASE_JSON" | jq -r '.id // empty' 2>/dev/null)
if [ -z "$RELEASE_ID" ]; then
echo "Release not found, skipping Docker image upload"
else
echo "Uploading Docker images to release $RELEASE_ID..."
for file in /tmp/docker-images/*; do
if [ -f "$file" ]; then
filename=$(basename "$file")
echo " Uploading: $filename"
curl -s -X POST \
-H "Authorization: token $GITEA_TOKEN" \
-F "attachment=@$file" \
"$GITEA_URL/api/v1/repos/$REPO_OWNER/$REPO_NAME/releases/$RELEASE_ID/assets" > /dev/null
if [ $? -eq 0 ]; then
echo " ✓ $filename uploaded"
else
echo " ✗ Failed to upload $filename"
fi
fi
done
fi