Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 71cfbf0fe9 | |||
| 48a8061909 |
+88
-34
@@ -1,7 +1,10 @@
|
|||||||
name: "DevSecOps Enterprise Pipeline"
|
name: "DevSecOps Enterprise Pipeline"
|
||||||
|
|
||||||
|
# ALTERADO: Agora a pipeline reage a QUALQUER alteração em QUALQUER branch,
|
||||||
|
# permitindo a automação total sem intervenção manual.
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [ main ]
|
branches: ["*"] # Escuta todas as branches
|
||||||
pull_request:
|
pull_request:
|
||||||
branches: [ main ]
|
branches: [ main ]
|
||||||
|
|
||||||
@@ -14,79 +17,130 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: 1. Build Application Image
|
# ==========================================
|
||||||
|
# ETAPA 1: VALIDACAO FUNCIONAL (TESTES UNITÁRIOS)
|
||||||
|
# Correm SEMPRE, em qualquer branch.
|
||||||
|
# ==========================================
|
||||||
|
- name: 1.1. Setup Node.js Environment
|
||||||
|
uses: actions/setup-node@v3
|
||||||
|
with:
|
||||||
|
node-version: '18'
|
||||||
|
|
||||||
|
- name: 1.2. Run Unit Tests (Jest)
|
||||||
|
run: |
|
||||||
|
echo ">>> A instalar dependências e a correr testes funcionais..."
|
||||||
|
# Ajusta para a pasta onde tens o teu package.json (ex: cd backend && npm install)
|
||||||
|
npm install
|
||||||
|
npm test
|
||||||
|
|
||||||
|
- name: 1.3. Build Application Image
|
||||||
run: docker build -f backend/Dockerfile -t apex-solutions:latest .
|
run: docker build -f backend/Dockerfile -t apex-solutions:latest .
|
||||||
|
|
||||||
# ==========================================
|
# ==========================================
|
||||||
# ETAPAS ESTÁTICAS DE SEGURANÇA SEPARADAS
|
# ETAPA 2: ANÁLISE ESTÁTICA RÁPIDA (SAST, SCA)
|
||||||
|
# Correm SEMPRE, a cada commit de qualquer branch secundária.
|
||||||
# ==========================================
|
# ==========================================
|
||||||
|
|
||||||
- name: 2. Secret Scanning (Gitleaks)
|
- name: 2.1. Secret Scanning (Gitleaks)
|
||||||
run: |
|
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
|
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 --exit-code 1 || echo "[ALERTA] Credenciais detetadas! Ignorado temporariamente."
|
/tmp/gitleaks detect --source . --verbose --redact --log-opts="-1" --exit-code 1
|
||||||
|
|
||||||
- name: 3. Software Composition Analysis (Trivy)
|
- name: 2.2. Software Composition Analysis (Trivy)
|
||||||
run: |
|
run: |
|
||||||
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin
|
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin
|
||||||
trivy image --severity HIGH,CRITICAL apex-solutions:latest || echo "[ALERTA] Vulnerabilidades de infraestrutura encontradas!"
|
trivy image --severity HIGH,CRITICAL --format table --output trivy-report.txt apex-solutions:latest
|
||||||
|
trivy image --severity HIGH,CRITICAL --exit-code 1 apex-solutions:latest
|
||||||
|
|
||||||
- name: 4. Static Application Security Testing (SonarQube)
|
- name: 2.3. Archive Trivy Report
|
||||||
|
if: always()
|
||||||
|
uses: actions/upload-artifact@v3
|
||||||
|
with:
|
||||||
|
name: trivy-sca-report
|
||||||
|
path: trivy-report.txt
|
||||||
|
|
||||||
|
- name: 2.4. Static Application Security Testing (SonarQube)
|
||||||
run: |
|
run: |
|
||||||
curl -sL https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-5.0.1.3006-linux.zip -o sonar-scanner.zip
|
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
|
unzip -q sonar-scanner.zip
|
||||||
./sonar-scanner-5.0.1.3006-linux/bin/sonar-scanner \
|
./sonar-scanner-5.0.1.3006-linux/bin/sonar-scanner \
|
||||||
-Dsonar.projectKey=apex-solutions \
|
-Dsonar.projectKey=website-test \
|
||||||
-Dsonar.sources=. \
|
-Dsonar.sources=. \
|
||||||
-Dsonar.host.url=https://sonar.each-cs.com \
|
-Dsonar.host.url=https://sonar.each-cs.com \
|
||||||
-Dsonar.token=${{ secrets.SONAR_TOKEN }} \
|
-Dsonar.token=${{ secrets.SONAR_TOKEN }} \
|
||||||
-Dsonar.qualitygate.wait=true || echo "[ALERTA] SonarQube falhou no Quality Gate."
|
-Dsonar.qualitygate.wait=true
|
||||||
|
|
||||||
# ==========================================
|
# ==========================================
|
||||||
# ETAPAS DINÂMICAS E DEPLOY
|
# ETAPA 3: ANÁLISE DINÂMICA (DAST - OWASP ZAP)
|
||||||
|
# CRÍTICO: O condicional 'if' garante que este bloco SÓ corre na branch 'main'
|
||||||
# ==========================================
|
# ==========================================
|
||||||
|
|
||||||
- name: 5. Dynamic Security Scan (OWASP ZAP)
|
- name: 3.1. Provision Ephemeral Sandbox
|
||||||
|
if: github.ref == 'refs/heads/main'
|
||||||
run: |
|
run: |
|
||||||
echo ">>> Lançando Sandbox Efêmero..."
|
docker rm -f website-test-sandbox || true
|
||||||
docker rm -f apex-sandbox || true
|
docker run -d --name website-test-sandbox apex-solutions:latest
|
||||||
docker run -d --name apex-sandbox apex-solutions:latest
|
|
||||||
sleep 5
|
sleep 5
|
||||||
|
|
||||||
echo ">>> Executando OWASP ZAP Baseline Scan..."
|
- name: 3.2. OWASP ZAP Baseline Scan
|
||||||
|
if: github.ref == 'refs/heads/main'
|
||||||
|
run: |
|
||||||
mkdir -p qatests
|
mkdir -p qatests
|
||||||
docker rm -f zap-scanner || true
|
docker rm -f zap-scanner || true
|
||||||
docker volume rm zap-reports || true
|
docker volume rm zap-reports || true
|
||||||
docker volume create zap-reports
|
docker volume create zap-reports
|
||||||
|
|
||||||
docker run --user root --name zap-scanner \
|
docker run --user root --name zap-scanner \
|
||||||
--link apex-sandbox:apex-sandbox \
|
--link website-test-sandbox:website-test-sandbox \
|
||||||
-v zap-reports:/zap/wrk/:rw \
|
-v zap-reports:/zap/wrk/:rw \
|
||||||
-t ghcr.io/zaproxy/zaproxy:stable zap-baseline.py \
|
-t ghcr.io/zaproxy/zaproxy:stable zap-baseline.py \
|
||||||
-t http://apex-sandbox:3000 \
|
-t http://website-test-sandbox:3000 \
|
||||||
-r report.html \
|
-r report.html \
|
||||||
-I
|
-I
|
||||||
|
|
||||||
docker cp zap-scanner:/zap/wrk/report.html qatests/report.html || true
|
docker cp zap-scanner:/zap/wrk/report.html qatests/report.html
|
||||||
|
|
||||||
echo ">>> Destruindo Sandbox..."
|
|
||||||
docker rm -f zap-scanner || true
|
docker rm -f zap-scanner || true
|
||||||
docker volume rm zap-reports || true
|
docker volume rm zap-reports || true
|
||||||
docker rm -f apex-sandbox || true
|
|
||||||
|
|
||||||
- name: 6. Production Deployment & Local Reports
|
- name: 3.3. Teardown Ephemeral Sandbox
|
||||||
|
if: always() && github.ref == 'refs/heads/main'
|
||||||
run: |
|
run: |
|
||||||
echo ">>> Atualizando Produção..."
|
docker rm -f website-test-sandbox || true
|
||||||
docker rm -f portal-corporativo || true
|
|
||||||
docker run -d -p 3000:3000 --name portal-corporativo apex-solutions:latest
|
|
||||||
sleep 3
|
|
||||||
curl --silent --show-error --fail http://localhost:3000 || exit 1
|
|
||||||
|
|
||||||
echo ">>> Publicando Relatório ZAP no servidor Web..."
|
|
||||||
docker cp qatests/report.html portal-corporativo:/usr/src/app/frontend/zap-report.html || true
|
|
||||||
|
|
||||||
- name: 7. Archive ZAP Report (Gitea Artifacts)
|
# ==========================================
|
||||||
if: always()
|
# ETAPA 4: DEPLOY SEGURO VIA SSH
|
||||||
|
# CRÍTICO: Só executa se passar em TUDO e for um push final na branch 'main'
|
||||||
|
# ==========================================
|
||||||
|
|
||||||
|
- name: 4.1. 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 o Deploy Remoto em Produção via SSH..."
|
||||||
|
cd /opt/website-test
|
||||||
|
git pull origin main
|
||||||
|
|
||||||
|
# Tratamento do .env em produção usando os Secrets do Gitea
|
||||||
|
echo "PORT=3000" > .env
|
||||||
|
echo "API_SECRET=${{ secrets.API_SECRET_PROD }}" >> .env
|
||||||
|
|
||||||
|
# Reconstrói a imagem e reinicia o serviço refletindo o espelho da main
|
||||||
|
docker build -f backend/Dockerfile -t apex-solutions:production .
|
||||||
|
docker stop portal-corporativo || true
|
||||||
|
docker rm portal-corporativo || true
|
||||||
|
docker run -d -p 3000:3000 --env-file .env --name portal-corporativo apex-solutions:production
|
||||||
|
echo ">>> Deploy em Produção finalizado com sucesso!"
|
||||||
|
|
||||||
|
# ==========================================
|
||||||
|
# ETAPA 5: GESTÃO DE ARTEFACTOS
|
||||||
|
# ==========================================
|
||||||
|
|
||||||
|
- name: Archive ZAP Report (Raw HTML)
|
||||||
|
if: always() && github.ref == 'refs/heads/main'
|
||||||
uses: actions/upload-artifact@v3
|
uses: actions/upload-artifact@v3
|
||||||
with:
|
with:
|
||||||
name: owasp-zap-report
|
name: owasp-zap-report
|
||||||
|
|||||||
Reference in New Issue
Block a user