name: Build and Deploy Container
on:
push:
branches:
- main
- master
workflow_dispatch:
env:
IMAGE_NAME: el-chavo
REGISTRY: us-central1-docker.pkg.dev
permissions:
contents: write
packages: write
attestations: write
id-token: write
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
attestations: write
id-token: write
steps:
- name: Checkout this repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Check if source code already imported
id: check-import
run: |
# Check if src/ or package.json exists (indicating source already imported)
if [ -f "package.json" ] || [ -f "pyproject.toml" ] || [ -f "Cargo.toml" ] || [ -f "go.mod" ] || [ -d "src" ]; then
echo "SOURCE_IMPORTED=true" >> $GITHUB_OUTPUT
echo "Source code already imported, skipping clone"
else
echo "SOURCE_IMPORTED=false" >> $GITHUB_OUTPUT
echo "Source code not yet imported, will clone from upstream"
fi
- name: Clone original source repository
if: steps.check-import.outputs.SOURCE_IMPORTED == 'false'
uses: actions/checkout@v4
with:
repository: FitoDomik/gitlab-mcp-server
ref: main
path: upstream-source
- name: Import source code to repository
if: steps.check-import.outputs.SOURCE_IMPORTED == 'false'
run: |
echo "=== Importing source code from FitoDomik/gitlab-mcp-server ==="
# Copy all source files (excluding .git and .github to avoid workflow permission issues)
cd upstream-source
find . -maxdepth 1 ! -name '.' ! -name '.git' ! -name '.github' -exec cp -r {} ../ \;
cd ..
# Remove the upstream-source directory
rm -rf upstream-source
# Configure git
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Commit the source code
git add -A
git commit -m "feat: Import source code from FitoDomik/gitlab-mcp-server@main" -m "Source: https://github.com/FitoDomik/gitlab-mcp-server | Branch: main | Imported: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
# Push the commit
git push origin main
echo "=== Source code imported successfully ==="
- name: Detect Dockerfile or generate fallback
id: dockerfile
run: |
echo "=== Checking for Dockerfile ==="
if [ -f "Dockerfile" ]; then
echo "DOCKERFILE_SOURCE=existing" >> $GITHUB_OUTPUT
echo "Using existing Dockerfile from source repository"
cat Dockerfile
else
echo "DOCKERFILE_SOURCE=generated" >> $GITHUB_OUTPUT
echo "No Dockerfile found, generating fallback..."
if [ -f "package.json" ]; then
echo "Detected: Node.js project"
if [ -f "pnpm-lock.yaml" ]; then
PKG_MGR="pnpm"
INSTALL_CMD="corepack enable && pnpm install --frozen-lockfile"
elif [ -f "yarn.lock" ]; then
PKG_MGR="yarn"
INSTALL_CMD="corepack enable && yarn install --frozen-lockfile"
elif [ -f "bun.lockb" ]; then
PKG_MGR="bun"
INSTALL_CMD="npm install -g bun && bun install"
else
PKG_MGR="npm"
INSTALL_CMD="npm ci || npm install"
fi
MAIN=$(jq -r '.main // "index.js"' package.json 2>/dev/null || echo "index.js")
HAS_BUILD=$(jq -r '.scripts.build // empty' package.json 2>/dev/null)
HAS_START=$(jq -r '.scripts.start // empty' package.json 2>/dev/null)
{
echo "FROM node:20-alpine"
echo "WORKDIR /app"
echo "COPY package*.json pnpm-lock.yaml* yarn.lock* bun.lockb* ./"
echo "RUN $INSTALL_CMD"
echo "COPY . ."
if [ -n "$HAS_BUILD" ]; then
if [ "$PKG_MGR" = "pnpm" ]; then echo "RUN pnpm run build"
elif [ "$PKG_MGR" = "yarn" ]; then echo "RUN yarn build"
elif [ "$PKG_MGR" = "bun" ]; then echo "RUN bun run build"
else echo "RUN npm run build"; fi
fi
if [ -n "$HAS_START" ]; then
if [ "$PKG_MGR" = "pnpm" ]; then echo 'CMD ["pnpm", "start"]'
elif [ "$PKG_MGR" = "yarn" ]; then echo 'CMD ["yarn", "start"]'
elif [ "$PKG_MGR" = "bun" ]; then echo 'CMD ["bun", "start"]'
else echo 'CMD ["npm", "start"]'; fi
else
echo "CMD [\"node\", \"$MAIN\"]"
fi
} > Dockerfile
elif [ -f "pyproject.toml" ] || [ -f "requirements.txt" ]; then
echo "Detected: Python project"
if [ -f "main.py" ]; then ENTRY="main.py"
elif [ -f "app.py" ]; then ENTRY="app.py"
elif [ -f "server.py" ]; then ENTRY="server.py"
else ENTRY="main.py"; fi
{
echo "FROM python:3.11-slim"
echo "WORKDIR /app"
echo "COPY requirements.txt* pyproject.toml* ./"
echo "RUN pip install --no-cache-dir -r requirements.txt 2>/dev/null || pip install --no-cache-dir . 2>/dev/null || true"
echo "COPY . ."
echo "CMD [\"python\", \"$ENTRY\"]"
} > Dockerfile
elif [ -f "go.mod" ]; then
echo "Detected: Go project"
{
echo "FROM golang:1.21-alpine AS builder"
echo "WORKDIR /app"
echo "COPY go.mod go.sum* ./"
echo "RUN go mod download"
echo "COPY . ."
echo "RUN CGO_ENABLED=0 GOOS=linux go build -o /app/server ."
echo "FROM alpine:latest"
echo "WORKDIR /app"
echo 'COPY --from=builder /app/server .'
echo 'CMD ["./server"]'
} > Dockerfile
elif [ -f "Cargo.toml" ]; then
echo "Detected: Rust project"
{
echo "FROM rust:1.75-alpine AS builder"
echo "RUN apk add --no-cache musl-dev"
echo "WORKDIR /app"
echo "COPY . ."
echo "RUN cargo build --release"
echo "FROM alpine:latest"
echo "WORKDIR /app"
echo 'COPY --from=builder /app/target/release/* ./'
echo 'CMD ["./app"]'
} > Dockerfile
else
echo "Unknown project type, creating minimal Dockerfile"
{
echo "FROM alpine:latest"
echo "WORKDIR /app"
echo "COPY . ."
echo 'CMD ["/bin/sh", "-c", "echo Container started"]'
} > Dockerfile
fi
echo "=== Generated Dockerfile ==="
cat Dockerfile
fi
echo "=== Build ready ==="
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Authenticate to Google Cloud
id: auth
uses: google-github-actions/auth@v2
with:
workload_identity_provider: projects/${{ secrets.GCP_PROJECT_NUMBER }}/locations/global/workloadIdentityPools/github-pool/providers/github-provider
service_account: mcp-deployer@biomindtalks.iam.gserviceaccount.com
project_id: biomindtalks
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v2
with:
project_id: biomindtalks
- name: Configure Docker for Artifact Registry
run: gcloud auth configure-docker us-central1-docker.pkg.dev --quiet
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: us-central1-docker.pkg.dev/biomindtalks/docker-images/el-chavo
tags: |
type=ref,event=branch
type=sha,prefix=
type=raw,value=latest,enable={{is_default_branch}}
labels: |
org.opencontainers.image.title=gitlab-mcp-server
org.opencontainers.image.description=MCP Server: gitlab-mcp-server - Containerized deployment
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.licenses=MIT
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile
platforms: linux/amd64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
GITLAB_ACCESS_TOKEN=${{ secrets.GITLAB_ACCESS_TOKEN }}
GITLAB_PROJECT_ID=${{ secrets.GITLAB_PROJECT_ID }}
GITLAB_URL=${{ secrets.GITLAB_URL }}