name: "DevSecOps Enterprise Pipeline" # ========================================== # PONTO 4: TRIGGERS (Push vs PR) # ========================================== on: push: branches: [ main ] pull_request: branches: [ main ] jobs: security-gate-and-deploy: runs-on: ubuntu-latest steps: - name: Checkout Code uses: actions/checkout@v3 with: fetch-depth: 0 # ========================================== # STAGE 1: STATIC SECURITY TESTING (SAST, SCA) # ========================================== - name: Gitleaks Scan run: | curl -sL https://github.com/gitleaks/gitleaks/releases/download/v8.18.2/gitleaks_8.18.2_linux_x64.tar.gz | tar -xz -C /tmp /tmp/gitleaks detect --source . --verbose --redact --log-opts="-1" --exit-code 1 # PONTO 3: Artefactos do Trivy adicionados - name: Scan Docker Image Vulnerabilities (Trivy) run: | curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin # 1. Guarda o output para um ficheiro de texto para o Artefato trivy image --severity HIGH,CRITICAL --format table --output trivy-report.txt nginx:alpine # 2. Corre novamente no terminal para forçar a quebra da pipeline se encontrar falhas (Exit-Code 1) trivy image --severity HIGH,CRITICAL --exit-code 1 nginx:alpine - name: Archive Trivy Report if: always() uses: actions/upload-artifact@v3 with: name: trivy-sca-report path: trivy-report.txt - name: SonarQube Analysis run: | curl -sL https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-5.0.1.3006-linux.zip -o sonar-scanner.zip unzip -q sonar-scanner.zip ./sonar-scanner-5.0.1.3006-linux/bin/sonar-scanner \ -Dsonar.projectKey=website-test \ -Dsonar.sources=. \ -Dsonar.host.url=https://sonar.each-cs.com \ -Dsonar.token=${{ secrets.SONAR_TOKEN }} \ -Dsonar.qualitygate.wait=true # ========================================== # STAGE 2 & 3: DYNAMIC TESTING (ZAP) # ========================================== - name: Provision Ephemeral Sandbox run: | docker rm -f website-test-sandbox || true docker run -d --name website-test-sandbox nginx:alpine docker cp index.html website-test-sandbox:/usr/share/nginx/html/index.html sleep 5 - name: OWASP ZAP Baseline Scan run: | mkdir -p qatests docker rm -f zap-scanner || true docker volume rm zap-reports || true docker volume create zap-reports docker run --user root --name zap-scanner \ --link website-test-sandbox:website-test-sandbox \ -v zap-reports:/zap/wrk/:rw \ -t ghcr.io/zaproxy/zaproxy:stable zap-baseline.py \ -t http://website-test-sandbox \ -r report.html \ -I docker cp zap-scanner:/zap/wrk/report.html qatests/report.html docker rm -f zap-scanner || true docker volume rm zap-reports || true - name: Teardown Ephemeral Sandbox if: always() run: | docker rm -f website-test-sandbox || true # ========================================== # STAGE 4: PRODUCTION DEPLOYMENT (SSH) # ========================================== # PONTO 6: Deploy Seguro Condicional # Só executa se for um Push (não PR) e se for na branch main - name: Hardened Production Deployment via SSH if: github.ref == 'refs/heads/main' && github.event_name == 'push' uses: appleboy/ssh-action@v1.0.0 with: host: ${{ secrets.SSH_HOST }} username: ${{ secrets.SSH_USER }} key: ${{ secrets.SSH_PRIVATE_KEY }} script: | echo ">>> A iniciar Deploy Seguro no Servidor de Produção..." # 1. Navegar para a pasta do projeto (Ajusta o caminho para o teu servidor real) cd /opt/website-test # 2. Puxar as últimas alterações do repositório git pull origin main # 3. Tratamento do ficheiro .ENV dinâmico (Frente 2) # Isto injeta os Secrets do Gitea num ficheiro .env temporário no servidor echo "PORT=80" > .env echo "API_SECRET=${{ secrets.API_SECRET_PROD }}" >> .env # 4. Construir e iniciar o contentor de Produção docker build -t website-test-backend:latest . docker rm -f website-test-backend || true docker run -d -p 80:80 --env-file .env --name website-test-backend website-test-backend:latest echo ">>> Deploy em Produção concluído com sucesso!" # ========================================== # STAGE 5: ARTIFACT MANAGEMENT & REPORTING # ========================================== - name: Archive ZAP Report (Raw HTML) if: always() uses: actions/upload-artifact@v3 with: name: owasp-zap-report path: qatests/report.html - name: Pipeline Status Notification if: always() run: | echo "Pipeline finished with status: ${{ job.status }}"