Files
k8s-game-2048/.github/workflows/deploy-dev.yml
Greg 09ec016b6a feat: Implement proper branch-based auto-promotion strategy
🚀 **New Branching Strategy:**
- develop → triggers dev deployment → auto-promotes to staging branch
- staging → triggers staging deployment → manual approval → promotes to main branch
- main → triggers production deployment

📝 **Workflow Changes:**
- deploy-dev.yml: Now triggers on develop branch
- deploy-staging.yml: Now triggers on staging branch push
- deploy-prod.yml: Now triggers on main branch push
- auto-promote.yml: Tests dev → merges develop to staging branch
- promote-to-production.yml: Tests staging → requires approval → merges staging to main
- build-image.yml: Now builds on all branches (main, develop, staging)

🎯 **Auto-Promotion Flow:**
1. Push to develop → Deploy to dev → Test → Auto-merge to staging
2. Staging deployment → Test → Manual approval → Auto-merge to main
3. Main deployment → Production live!

This provides proper separation between environments with appropriate gates.
2025-06-30 23:18:14 -07:00

218 lines
6.8 KiB
YAML

name: Deploy to Development
on:
workflow_run:
workflows: ["Build and Push Container Image"]
types:
- completed
branches: [ develop ]
push:
branches: [ develop ]
workflow_dispatch:
inputs:
image_tag:
description: 'Image tag to deploy (default: latest)'
required: false
default: 'latest'
env:
REGISTRY: ghcr.io
IMAGE_NAME: ghndrx/k8s-game-2048
jobs:
deploy-dev:
name: Deploy to Development
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }}
environment: development
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up kubectl
uses: azure/setup-kubectl@v3
with:
version: 'latest'
- name: Configure kubectl
run: |
mkdir -p ~/.kube
echo "${{ secrets.KUBECONFIG }}" | base64 -d > ~/.kube/config
chmod 600 ~/.kube/config
- name: Set image tag
run: |
IMAGE_TAG="${{ github.event.inputs.image_tag || 'latest' }}"
echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV
echo "Deploying image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:$IMAGE_TAG"
- name: Deploy to development
run: |
echo "🚀 Deploying to development environment..."
# Apply namespace
kubectl apply -f manifests/dev/namespace.yml
# Ensure GHCR secret exists
if kubectl get secret ghcr-secret -n default &>/dev/null; then
echo "🔐 Copying GHCR secret to dev namespace..."
kubectl get secret ghcr-secret -o yaml | \
sed 's/namespace: default/namespace: game-2048-dev/' | \
sed '/resourceVersion:/d' | \
sed '/uid:/d' | \
sed '/creationTimestamp:/d' | \
kubectl apply -f -
fi
# Update image in service and deploy
kubectl patch ksvc game-2048-dev -n game-2048-dev --type merge -p '{"spec":{"template":{"spec":{"containers":[{"image":"${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}","imagePullPolicy":"Always"}]}}}}'
echo "⏳ Waiting for deployment to be ready..."
kubectl wait --for=condition=Ready ksvc/game-2048-dev -n game-2048-dev --timeout=300s || echo "⚠️ Service may still be starting"
- name: Verify deployment
run: |
echo "📊 Deployment status:"
kubectl get ksvc -n game-2048-dev
echo ""
echo "✅ Development deployment completed!"
echo "🌐 Available at: https://2048-dev.wa.darknex.us"
- name: Run smoke test
run: |
echo "🧪 Running smoke test..."
sleep 30
for i in {1..5}; do
echo "Attempt $i/5..."
# Test canonical domain first
if curl -s --max-time 30 https://game-2048-dev.game-2048-dev.dev.wa.darknex.us/ | grep -q "2048"; then
echo "✅ Canonical domain smoke test passed!"
break
# Fallback to custom domain
elif curl -s --max-time 30 https://2048-dev.wa.darknex.us/ | grep -q "2048"; then
echo "✅ Custom domain smoke test passed!"
break
elif [ $i -eq 5 ]; then
echo "⚠️ Smoke test failed after 5 attempts"
exit 1
else
echo "Retrying in 30 seconds..."
sleep 30
fi
done
uses: azure/setup-kubectl@v3
with:
version: 'v1.28.0'
- name: Configure kubectl
run: |
echo "${{ secrets.KUBECONFIG }}" | base64 -d > kubeconfig
export KUBECONFIG=kubeconfig
- name: Update image in manifests
run: |
sed -i "s|ghcr.io/ghndrx/k8s-game-2048:latest|${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}|g" manifests/dev/service.yml
- name: Deploy to development
run: |
export KUBECONFIG=kubeconfig
kubectl apply -f manifests/dev/
- name: Wait for deployment
run: |
export KUBECONFIG=kubeconfig
kubectl wait --for=condition=Ready ksvc/game-2048-dev -n game-2048-dev --timeout=300s
- name: Get service URL
id: get-url
run: |
export KUBECONFIG=kubeconfig
SERVICE_URL=$(kubectl get ksvc game-2048-dev -n game-2048-dev -o jsonpath='{.status.url}')
echo "service_url=$SERVICE_URL" >> $GITHUB_OUTPUT
echo "🚀 Development service deployed at: $SERVICE_URL"
- name: Set up Node.js for testing
uses: actions/setup-node@v4
with:
node-version: '18'
cache: 'npm'
cache-dependency-path: tests/package.json
- name: Install Playwright dependencies
run: |
cd tests
npm install
npx playwright install --with-deps
- name: Run Playwright tests
run: |
cd tests
BASE_URL=${{ steps.get-url.outputs.service_url }} npx playwright test
env:
CI: true
- name: Upload test results
uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-results-dev-${{ github.sha }}-${{ github.run_number }}
path: |
tests/playwright-report/
tests/test-results/
retention-days: 30
- name: Upload screenshots
uses: actions/upload-artifact@v4
if: always()
with:
name: screenshots-dev-${{ github.sha }}-${{ github.run_number }}
path: tests/test-results/**/*.png
retention-days: 30
promote-to-staging:
needs: build-and-deploy
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/develop'
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Create Pull Request to Staging
uses: peter-evans/create-pull-request@v5
with:
token: ${{ secrets.GITHUB_TOKEN }}
push-to-fork: false
branch: auto-promote/dev-to-staging-${{ github.sha }}
base: staging
title: "🚀 Auto-promote: Deploy ${{ github.sha }} to staging"
body: |
## 🚀 Auto-promotion from Development
**Source**: `develop` branch
**Commit**: ${{ github.sha }}
**Triggered by**: @${{ github.actor }}
### ✅ Development Tests Passed
- Basic functionality tests
- Gameplay mechanics tests
- Visual regression tests
- Environment validation tests
### 🎯 Changes in this promotion:
${{ github.event.head_commit.message }}
This PR was automatically created after successful deployment and testing in the development environment.
**Development URL**: https://2048-dev.wa.darknex.us
**Will deploy to**: https://2048-staging.wa.darknex.us
labels: |
auto-promotion
staging
deploy