fix(ci): correct VM_IP to use secret + SSH deploy_key pattern #103
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: EVA-Mind CI/CD | |
| on: | |
| push: | |
| branches: [main] | |
| env: | |
| GO_VERSION: "1.24" | |
| VM_IP: ${{ secrets.VM_IP }} | |
| VM_USER: "web2a" | |
| jobs: | |
| # ============================================================================ | |
| # BUILD & TEST (local no GitHub Actions) | |
| # ============================================================================ | |
| build: | |
| name: Build & Test | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Go | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: ${{ env.GO_VERSION }} | |
| - name: Cache Go modules | |
| uses: actions/cache@v4 | |
| with: | |
| path: ~/go/pkg/mod | |
| key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} | |
| restore-keys: | | |
| ${{ runner.os }}-go- | |
| - name: Build | |
| run: | | |
| go mod tidy | |
| CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o eva-mind . | |
| echo "Build OK: $(ls -lh eva-mind | awk '{print $5}')" | |
| # ============================================================================ | |
| # DEPLOY TO VM (via SSH direto — mesmo padrão do Malaria-Angolar) | |
| # ============================================================================ | |
| deploy: | |
| name: Deploy to VM | |
| runs-on: ubuntu-latest | |
| needs: build | |
| if: github.ref == 'refs/heads/main' | |
| steps: | |
| - name: Setup SSH key | |
| run: | | |
| mkdir -p ~/.ssh | |
| printf '%s\n' "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/deploy_key | |
| chmod 600 ~/.ssh/deploy_key | |
| ssh-keyscan -H ${{ env.VM_IP }} >> ~/.ssh/known_hosts 2>/dev/null | |
| echo "Host * | |
| IdentityFile ~/.ssh/deploy_key | |
| StrictHostKeyChecking accept-new" > ~/.ssh/config | |
| chmod 600 ~/.ssh/config | |
| - name: Deploy via SSH (git pull + build + restart) | |
| run: | | |
| ssh ${{ env.VM_USER }}@${{ env.VM_IP }} 'bash /home/web2a/EVA-Mind/scripts/redeploy.sh' | |
| - name: Health check | |
| run: | | |
| sleep 5 | |
| for i in 1 2 3; do | |
| HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \ | |
| --connect-timeout 10 \ | |
| http://${{ env.VM_IP }}:8091/api/health || echo "000") | |
| echo "Attempt $i: HTTP $HTTP_CODE" | |
| if [ "$HTTP_CODE" = "200" ]; then | |
| echo "EVA-Mind OK" | |
| exit 0 | |
| fi | |
| sleep 5 | |
| done | |
| echo "ERROR: Health check failed (last: HTTP $HTTP_CODE)" | |
| ssh ${{ env.VM_USER }}@${{ env.VM_IP }} 'sudo journalctl -u eva-mind --no-pager -n 30' || true | |
| exit 1 | |
| - name: Show logs on failure | |
| if: failure() | |
| run: | | |
| ssh ${{ env.VM_USER }}@${{ env.VM_IP }} 'sudo journalctl -u eva-mind --no-pager -n 50' || true | |
| - name: Notify | |
| if: always() | |
| run: echo "Deploy ${{ job.status }} - commit ${{ github.sha }}" |