JobmakerSite/.gitea/workflows/deploy.yml
Ricardo Carneiro c9efcd12c3
Some checks failed
Build and Deploy JobMaker / build (push) Failing after 10s
Build and Deploy JobMaker / deploy (push) Has been skipped
Build and Deploy JobMaker / notify (push) Failing after 1s
fix: tests
2025-06-17 17:04:48 -03:00

182 lines
5.4 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 nginx inicializar completamente
echo "⏳ Waiting for nginx to start..."
sleep 20
# Verificar se container está rodando
echo "📋 Container status:"
docker ps | grep test-container
# Mostrar logs para debug
echo "📋 Container logs:"
docker logs test-container
# Verificar se nginx está rodando
echo "🔍 Checking nginx process:"
docker exec test-container ps aux | grep nginx
# Teste interno primeiro (mais confiável)
echo "🔍 Testing internal connectivity:"
docker exec test-container wget -q --spider http://localhost/ && echo "✅ Internal test OK" || exit 1
# Teste externo com retry
echo "🔍 Testing external connectivity:"
for i in {1..5}; do
if curl -f http://localhost:8080; then
echo "✅ External test OK on attempt $i"
break
else
echo "⚠️ Attempt $i failed, retrying..."
sleep 5
fi
if [ $i -eq 5 ]; then
echo "❌ All attempts failed"
docker logs test-container
exit 1
fi
done
# 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