154 lines
4.5 KiB
YAML
154 lines
4.5 KiB
YAML
# .gitea/workflows/deploy.yml
|
|
name: Build and Deploy JobMaker
|
|
|
|
on:
|
|
push:
|
|
branches: [ main ]
|
|
pull_request:
|
|
branches: [ main ]
|
|
|
|
env:
|
|
REGISTRY: registry.redecarneir.us
|
|
IMAGE_NAME: jobmaker-website
|
|
DEPLOY_HOST: 92.246.130.58
|
|
DEPLOY_USER: root
|
|
CONTAINER_NAME: jobmaker-web
|
|
|
|
jobs:
|
|
build:
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: 📥 Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: 🐳 Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
- name: 🏷️ Generate tags
|
|
id: meta
|
|
run: |
|
|
echo "VERSION=v$(date +%Y%m%d)-${GITHUB_SHA::8}" >> $GITHUB_OUTPUT
|
|
echo "LATEST_TAG=${REGISTRY}/${IMAGE_NAME}:latest" >> $GITHUB_OUTPUT
|
|
echo "VERSION_TAG=${REGISTRY}/${IMAGE_NAME}:v$(date +%Y%m%d)-${GITHUB_SHA::8}" >> $GITHUB_OUTPUT
|
|
|
|
- name: 🔨 Build Docker image
|
|
run: |
|
|
echo "🔨 Building Docker image..."
|
|
docker build \
|
|
-t ${{ steps.meta.outputs.LATEST_TAG }} \
|
|
-t ${{ steps.meta.outputs.VERSION_TAG }} \
|
|
.
|
|
|
|
- name: 🧪 Test container
|
|
run: |
|
|
echo "🧪 Testing container..."
|
|
# Iniciar container de teste
|
|
docker run -d --name test-container -p 8080:80 ${{ steps.meta.outputs.LATEST_TAG }}
|
|
|
|
# Aguardar container iniciar
|
|
sleep 10
|
|
|
|
# Testar se está respondendo
|
|
curl -f http://localhost:8080 || exit 1
|
|
curl -f http://localhost:8080/robots.txt || exit 1
|
|
curl -f http://localhost:8080/sitemap.xml || exit 1
|
|
curl -f http://localhost:8080/en/ || exit 1
|
|
|
|
# Parar container de teste
|
|
docker stop test-container
|
|
docker rm test-container
|
|
|
|
echo "✅ Container tests passed!"
|
|
|
|
- name: 📤 Push to registry
|
|
run: |
|
|
echo "📤 Pushing to registry..."
|
|
docker push ${{ steps.meta.outputs.LATEST_TAG }}
|
|
docker push ${{ steps.meta.outputs.VERSION_TAG }}
|
|
echo "✅ Images pushed successfully!"
|
|
|
|
deploy:
|
|
needs: build
|
|
runs-on: ubuntu-latest
|
|
if: github.ref == 'refs/heads/main'
|
|
|
|
steps:
|
|
- name: 🔑 Setup SSH
|
|
run: |
|
|
mkdir -p ~/.ssh
|
|
echo "${{ secrets.DEPLOY_SSH_KEY }}" > ~/.ssh/id_rsa
|
|
chmod 600 ~/.ssh/id_rsa
|
|
ssh-keyscan -H ${{ env.DEPLOY_HOST }} >> ~/.ssh/known_hosts
|
|
|
|
- name: 🚀 Deploy to server
|
|
run: |
|
|
echo "🚀 Starting deployment..."
|
|
|
|
ssh ${{ env.DEPLOY_USER }}@${{ env.DEPLOY_HOST }} << 'EOF'
|
|
set -e
|
|
|
|
echo "📥 Pulling latest image..."
|
|
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
|
|
|
|
echo "🛑 Stopping current container..."
|
|
docker stop ${{ env.CONTAINER_NAME }} || true
|
|
docker rm ${{ env.CONTAINER_NAME }} || true
|
|
|
|
echo "🚀 Starting new container..."
|
|
docker run -d \
|
|
--name ${{ env.CONTAINER_NAME }} \
|
|
--restart unless-stopped \
|
|
-p 8080:80 \
|
|
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
|
|
|
|
echo "🧪 Testing deployment..."
|
|
sleep 5
|
|
|
|
# Testar se está respondendo
|
|
curl -f http://localhost/ || exit 1
|
|
curl -f http://localhost/en/ || exit 1
|
|
|
|
echo "🧹 Cleaning up old images..."
|
|
docker image prune -f
|
|
|
|
echo "✅ Deployment completed successfully!"
|
|
EOF
|
|
|
|
- name: 📊 Deployment status
|
|
run: |
|
|
echo "📊 Checking deployment status..."
|
|
|
|
ssh ${{ env.DEPLOY_USER }}@${{ env.DEPLOY_HOST }} << 'EOF'
|
|
echo "=== Container Status ==="
|
|
docker ps | grep ${{ env.CONTAINER_NAME }}
|
|
|
|
echo "=== Container Logs (last 20 lines) ==="
|
|
docker logs --tail 20 ${{ env.CONTAINER_NAME }}
|
|
|
|
echo "=== Disk Usage ==="
|
|
df -h
|
|
|
|
echo "=== Docker Images ==="
|
|
docker images | grep ${{ env.IMAGE_NAME }}
|
|
EOF
|
|
|
|
notify:
|
|
needs: [build, deploy]
|
|
runs-on: ubuntu-latest
|
|
if: always()
|
|
|
|
steps:
|
|
- name: 📢 Notify deployment result
|
|
run: |
|
|
if [[ "${{ needs.deploy.result }}" == "success" ]]; then
|
|
echo "🎉 Deployment successful!"
|
|
echo "✅ JobMaker is live at: http://${{ env.DEPLOY_HOST }}"
|
|
echo "✅ English version: http://${{ env.DEPLOY_HOST }}/en/"
|
|
else
|
|
echo "❌ Deployment failed!"
|
|
echo "Check the logs above for details."
|
|
exit 1
|
|
fi
|
|
|