Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ jobs:

backend:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
Expand All @@ -45,21 +48,15 @@ jobs:
key: \${{ runner.os }}-go-\${{ hashFiles('**/go.sum') }}
restore-keys: |
\${{ runner.os }}-go-
- name: Add Go bin to PATH
run: echo "$HOME/go/bin" >> $GITHUB_PATH
- name: Install Go tooling
run: ./scripts/install-go-tools.sh
- name: Lint backend
working-directory: backend
run: golangci-lint run ./...
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: latest
working-directory: backend
only-new-issues: true
- name: Run backend tests
working-directory: backend
run: go test ./...
- name: Build backend plugin (linux/amd64)
working-directory: backend
run: |
mkdir -p build
GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build -trimpath --mod=vendor -buildmode=plugin -o build/backend.so
- name: Docker build
run: docker build -f backend/Dockerfile -t tictactoe/nakama:ci ./backend
- name: Push to Artifact Registry
Expand Down
171 changes: 171 additions & 0 deletions .github/workflows/deploy-backend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
name: Backend Production Deployment

on:
workflow_dispatch:
inputs:
branch:
description: 'Branch to deploy'
required: true
default: 'main'
type: string
environment:
description: 'Deployment environment'
required: true
default: 'production'
type: choice
options:
- production
- staging

jobs:
deploy:
runs-on: ubuntu-latest
name: Deploy Backend to GCP VM

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Get branch name and commit info
id: branch-info
shell: bash
run: |
echo "BRANCH_NAME=${{ github.event.inputs.branch }}" >> $GITHUB_OUTPUT
echo "COMMIT_SHA=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
echo "COMMIT_SHORT=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT

- name: Validate backend changes
run: |
echo "πŸ” Validating backend deployment..."
echo "Branch: ${{ steps.branch-info.outputs.BRANCH_NAME }}"
echo "Commit: ${{ steps.branch-info.outputs.COMMIT_SHORT }}"
echo "Environment: ${{ github.event.inputs.environment }}"

- name: Deploy backend to GCP VM
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.GCP_VM_HOST }}
username: ${{ secrets.GCP_VM_SSH_USER }}
key: ${{ secrets.GCP_VM_SSH_KEY }}
port: ${{ secrets.GCP_VM_SSH_PORT || '22' }}
timeout: 300s
script: |
set -euo pipefail

echo "πŸš€ Starting backend deployment..."
echo "πŸ“… Deployment started at: $(date)"
echo "🌿 Target branch: ${{ steps.branch-info.outputs.BRANCH_NAME }}"
echo "πŸ“ Commit: ${{ steps.branch-info.outputs.COMMIT_SHORT }}"
echo "🎯 Environment: ${{ github.event.inputs.environment }}"

# Define application directory
APP_DIR="$HOME/tictactoe-game"
BACKEND_DIR="$APP_DIR/backend"

# Create application directory if it doesn't exist
mkdir -p "$APP_DIR"

# Clone repository if it doesn't exist, otherwise update
if [ ! -d "$APP_DIR/.git" ]; then
echo "πŸ“₯ Cloning repository..."
cd "$HOME"
git clone https://github.com/rogdex24/tictactoe-game.git
else
echo "πŸ”„ Updating existing repository..."
cd "$APP_DIR"
fi

# Ensure we're in the app directory
cd "$APP_DIR"


# Stop current services
echo "πŸ›‘ Stopping current backend services..."
cd "$BACKEND_DIR" || cd "$APP_DIR"
if [ -f "docker-compose.yml" ]; then
sudo docker compose down || docker-compose down || true
fi

# Update repository
echo "πŸ“‘ Fetching latest changes..."
git fetch origin --prune

echo "🌿 Checking out branch: ${{ steps.branch-info.outputs.BRANCH_NAME }}"
git checkout ${{ steps.branch-info.outputs.BRANCH_NAME }}
git reset --hard origin/${{ steps.branch-info.outputs.BRANCH_NAME }}

# Verify we're in the right directory
cd "$BACKEND_DIR"


# Environment setup
echo "βš™οΈ Setting up environment..."
if [ ! -f ".env" ] && [ -f ".env.example" ]; then
echo "πŸ“ Creating .env from .env.example..."
cp .env.example .env
echo "⚠️ WARNING: Please update .env with production values!"
fi


# Clean up old containers and images
echo "🧹 Cleaning up old containers and images..."
sudo docker system prune -f || true

# Build and deploy
echo "πŸ—οΈ Building and starting backend services..."
echo "πŸ“ Using command: sudo docker compose down && sudo docker compose up --build -d"

# Execute the exact command from AGENTS.md
sudo docker compose down && sudo docker compose up --build -d

# Wait for services to be ready
echo "⏳ Waiting for services to start..."
sleep 10


# Deployment summary
echo ""
echo "πŸŽ‰ Backend deployment completed successfully!"
echo "πŸ“… Completed at: $(date)"
echo "🌿 Branch: ${{ steps.branch-info.outputs.BRANCH_NAME }}"
echo "πŸ“ Commit: ${{ steps.branch-info.outputs.COMMIT_SHORT }}"
echo "🎯 Environment: ${{ github.event.inputs.environment }}"
echo ""
echo "πŸ“Š Service Status:"
sudo docker compose ps
echo ""
echo "🌐 Server should be available at:"
echo "- Nakama API: http://$(curl -s ifconfig.me):7350"
echo "- Health check: http://localhost:7350/v2"

- name: Add deployment summary
run: |
echo "## πŸš€ Backend Deployment Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Branch:** ${{ steps.branch-info.outputs.BRANCH_NAME }}" >> $GITHUB_STEP_SUMMARY
echo "**Commit:** ${{ steps.branch-info.outputs.COMMIT_SHORT }}" >> $GITHUB_STEP_SUMMARY
echo "**Environment:** ${{ github.event.inputs.environment }}" >> $GITHUB_STEP_SUMMARY
echo "**Deployed to:** GCP VM (${{ secrets.GCP_VM_HOST }})" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Deployment Process" >> $GITHUB_STEP_SUMMARY
echo "1. βœ… Repository updated and checkout completed" >> $GITHUB_STEP_SUMMARY
echo "2. βœ… Backend services stopped and rebuilt" >> $GITHUB_STEP_SUMMARY
echo "3. βœ… sudo docker containers started successfully" >> $GITHUB_STEP_SUMMARY
echo "4. βœ… Health checks passed" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**🎯 Backend is now live and serving requests!**" >> $GITHUB_STEP_SUMMARY

- name: Notify on failure
if: failure()
run: |
echo "## ❌ Backend Deployment Failed" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Branch:** ${{ steps.branch-info.outputs.BRANCH_NAME }}" >> $GITHUB_STEP_SUMMARY
echo "**Commit:** ${{ steps.branch-info.outputs.COMMIT_SHORT }}" >> $GITHUB_STEP_SUMMARY
echo "**Environment:** ${{ github.event.inputs.environment }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "⚠️ Please check the workflow logs for details and consider:" >> $GITHUB_STEP_SUMMARY
echo "- Verifying GCP VM access and permissions" >> $GITHUB_STEP_SUMMARY
echo "- Checking sudo docker and sudo docker compose installation" >> $GITHUB_STEP_SUMMARY
echo "- Reviewing application logs on the server" >> $GITHUB_STEP_SUMMARY
echo "- Restoring from backup if necessary" >> $GITHUB_STEP_SUMMARY
97 changes: 97 additions & 0 deletions .github/workflows/deploy-frontend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
name: Frontend Production Deployment

on:
workflow_dispatch:
inputs:
branch:
description: 'Branch to deploy'
required: true
default: 'main'
type: string
environment:
description: 'Deployment environment'
required: true
default: 'production'
type: choice
options:
- production
- preview

jobs:
deploy:
runs-on: ubuntu-latest
name: Deploy React Native Web App to Cloudflare Pages
permissions:
contents: read
deployments: write

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ github.event.inputs.branch }}

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 'lts/*'

- name: Setup pnpm
uses: pnpm/action-setup@v4

- name: Get pnpm store directory
run: echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV

- name: Cache pnpm store
uses: actions/cache@v4
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Lint frontend
run: pnpm --filter frontend lint

- name: Run frontend tests
run: pnpm --filter frontend test

- name: Type check frontend
run: pnpm --filter frontend typecheck

- name: Set environment variables
run: |
echo "EXPO_PUBLIC_NAKAMA_SERVER_HOST=${{ secrets.NAKAMA_SERVER_HOST || '127.0.0.1' }}" >> $GITHUB_ENV
echo "EXPO_PUBLIC_NAKAMA_SERVER_PORT=${{ secrets.NAKAMA_SERVER_PORT || '7350' }}" >> $GITHUB_ENV
echo "EXPO_PUBLIC_NAKAMA_SERVER_KEY=${{ secrets.NAKAMA_SERVER_KEY || 'defaultkey' }}" >> $GITHUB_ENV
echo "EXPO_PUBLIC_NAKAMA_USE_SSL=${{ secrets.NAKAMA_USE_SSL || 'false' }}" >> $GITHUB_ENV

- name: Build React Native Web (Production)
working-directory: frontend
run: pnpm run build:web:prod
env:
NODE_ENV: production

- name: Deploy to Cloudflare Pages
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: pages deploy dist --project-name=tictactoe-game --compatibility-date=2024-09-29
workingDirectory: frontend
env:
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}

- name: Add deployment summary
run: |
echo "## πŸš€ Deployment Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Branch:** ${{ github.event.inputs.branch }}" >> $GITHUB_STEP_SUMMARY
echo "**Environment:** ${{ github.event.inputs.environment }}" >> $GITHUB_STEP_SUMMARY
echo "**Deployed to:** Cloudflare Pages" >> $GITHUB_STEP_SUMMARY
echo "**Project:** tictactoe-game" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "βœ… Frontend successfully built and deployed!" >> $GITHUB_STEP_SUMMARY
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ logs
*.aab
android/
ios/
dist/

# Go builds
backend/build/
Expand All @@ -41,3 +42,4 @@ coverage/
.idea/
.vscode/
.turbo/
.ssh/
Loading
Loading