name: Deploy
on:
push:
branches: [ main ]
tags:
- 'v*'
workflow_dispatch:
inputs:
environment:
description: 'Deployment environment'
required: true
default: 'staging'
type: choice
options:
- staging
- production
permissions:
contents: read
packages: write
jobs:
build-and-push-docker:
name: Build and Push Docker Image
runs-on: ubuntu-latest
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64
- name: Generate deployment summary
run: |
echo "### Docker Image Pushed! 🐳" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Tags:**" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Pull command:**" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY
echo "docker pull ghcr.io/${{ github.repository }}:latest" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
deploy-staging:
name: Deploy to Staging
runs-on: ubuntu-latest
needs: build-and-push-docker
if: |
github.event_name == 'push' &&
github.ref == 'refs/heads/main' ||
(github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'staging')
environment:
name: staging
url: https://staging.oxide.example.com
steps:
- name: Deploy to staging server
run: |
echo "🚀 Deploying to staging environment..."
echo "This is a placeholder. Add your deployment commands here."
echo "Example: kubectl apply -f k8s/staging/"
echo "Example: docker-compose -f docker-compose.staging.yml up -d"
- name: Deployment summary
run: |
echo "### ✅ Deployed to Staging!" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Environment:** staging" >> $GITHUB_STEP_SUMMARY
echo "**Branch:** ${{ github.ref_name }}" >> $GITHUB_STEP_SUMMARY
echo "**Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY
deploy-production:
name: Deploy to Production
runs-on: ubuntu-latest
needs: build-and-push-docker
if: |
(github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')) ||
(github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'production')
environment:
name: production
url: https://oxide.example.com
steps:
- name: Deploy to production server
run: |
echo "🚀 Deploying to production environment..."
echo "This is a placeholder. Add your deployment commands here."
echo "Example: kubectl apply -f k8s/production/"
echo "Example: ansible-playbook deploy.yml -i production"
- name: Deployment summary
run: |
echo "### 🎉 Deployed to Production!" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Environment:** production" >> $GITHUB_STEP_SUMMARY
echo "**Tag:** ${{ github.ref_name }}" >> $GITHUB_STEP_SUMMARY
echo "**Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY
- name: Create deployment notification
run: |
echo "📢 Send notification to Slack/Discord/etc."
echo "Example: curl -X POST https://hooks.slack.com/..."