Merge pull request #1 from pwviptbl/melhorias-gerais #22
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: PHP Security Scan CI/CD | |
| on: | |
| push: | |
| branches: | |
| - main | |
| pull_request: | |
| branches: | |
| - main | |
| jobs: | |
| security_analysis: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| with: | |
| fetch-depth: 0 # Manter para o git diff ter histórico completo | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: '3.13' # Sua versao do Python | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install Jinja2 reportlab | |
| - name: Get changed PHP files (Using Git Diff) | |
| id: changed-php-files | |
| run: | | |
| CHANGED_FILES="" | |
| if [ "${{ github.event_name }}" == "pull_request" ]; then | |
| BASE_REF="${{ github.base_ref }}" | |
| CHANGED_FILES=$(git diff --name-only "origin/$BASE_REF" HEAD -- '*.php' | xargs) | |
| else | |
| if git rev-parse --verify HEAD^1 &>/dev/null; then | |
| CHANGED_FILES=$(git diff --name-only HEAD^1 HEAD -- '*.php' | xargs) | |
| else | |
| # No primeiro commit, considera todos os arquivos PHP como novos | |
| CHANGED_FILES=$(find . -name "*.php" -type f | xargs) | |
| fi | |
| fi | |
| # Define os outputs para os próximos passos | |
| if [ -z "$CHANGED_FILES" ]; then | |
| echo "Nenhum arquivo PHP modificado encontrado." | |
| echo "changed_files_list=" >> "$GITHUB_OUTPUT" | |
| echo "any_changed=false" >> "$GITHUB_OUTPUT" | |
| else | |
| echo "Arquivos PHP modificados detectados: $CHANGED_FILES" | |
| echo "changed_files_list=$CHANGED_FILES" >> "$GITHUB_OUTPUT" | |
| echo "any_changed=true" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Run PHP Security Analysis on changed files | |
| # A condição 'if' deve estar no mesmo nível que 'name' e 'run' | |
| if: steps.changed-php-files.outputs.any_changed == 'true' | |
| run: | | |
| echo "## Iniciando Analise de Seguranca ##" | |
| MODIFIED_PHP_FILES="${{ steps.changed-php-files.outputs.changed_files_list }}" | |
| echo "Arquivos PHP sendo analisados: $MODIFIED_PHP_FILES" | |
| # Chama seu script principal, passando os arquivos modificados. | |
| # Nao usamos --no-report aqui para sempre gerar relatorios no CI/CD | |
| # para que o artefato esteja sempre disponivel. | |
| python script.py $MODIFIED_PHP_FILES | |
| echo "Script de analise finalizado com o codigo de saida: $?" | |
| exit $? | |
| - name: No PHP files changed or found | |
| # A condição 'if' deve estar no mesmo nível que 'name' e 'run' | |
| if: steps.changed-php-files.outputs.any_changed == 'false' | |
| run: | | |
| echo "Nenhum arquivo PHP modificado encontrado para analisar. Pulando a analise de seguranca." | |
| - name: Upload Security Report Artifacts | |
| # A condição 'if' deve estar no mesmo nível que 'name' e 'uses' | |
| if: success() && steps.changed-php-files.outputs.any_changed == 'true' | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: security-scan-reports | |
| path: report/ | |
| # A condição 'if' para verificar se a pasta existe nao pode ir aqui dentro do 'with'. | |
| # O upload-artifact v4 já é mais inteligente e não vai falhar se a pasta estiver vazia | |
| # ou se o path não existir (ele vai gerar um artefato vazio ou aviso). | |
| # A condicao 'success() && steps.changed-php-files.outputs.any_changed == 'true'' ja garante que a analise rodou. |