Change load Repos #55
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
| on: | |
| push: | |
| branches: | |
| - add-bucket-upload-workflow | |
| pull_request: | |
| branches: | |
| - add-bucket-upload-workflow | |
| jobs: | |
| bucket-upload-S3: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 | |
| with: | |
| ref: ${{ github.sha }} | |
| - uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0 | |
| with: | |
| go-version: "^1.22" | |
| - name: Clone 2ms Repository and Checkout Commit SHA | |
| run: | | |
| # Clonar o repositório 2ms | |
| git clone https://github.com/checkmarx/2ms.git /tmp/2ms | |
| cd /tmp/2ms | |
| git fetch --all | |
| git checkout ${{ github.sha }} | |
| # Compilar o 2ms | |
| go build -o dist/2ms main.go | |
| # Verificar se o binário foi criado corretamente | |
| ls -la dist/2ms | |
| # Garantir permissões de execução | |
| chmod +x dist/2ms | |
| - name: Load Repos from JSON and Clone Each Repo | |
| run: | | |
| # Baixar o arquivo repos.json diretamente | |
| curl -o /tmp/repos.json https://raw.githubusercontent.com/cx-miguel-neiva/2ms-github-action/main/repos.json | |
| # Extrair os repositórios e iterar sobre eles | |
| REPOS_LIST=$(jq -r '.projects[]' /tmp/repos.json) | |
| # Exibir o conteúdo de REPOS_LIST para depuração | |
| echo "Repos List: $REPOS_LIST" | |
| # Iterar sobre cada repositório na lista | |
| for repo_url in $REPOS_LIST; do | |
| repo_name=$(basename "$repo_url" .git) | |
| echo "Cloning repository: $repo_url" | |
| # Criar uma pasta para o repositório | |
| mkdir -p "$GITHUB_WORKSPACE/$repo_name" | |
| # Clonar o repositório na pasta criada | |
| git clone "$repo_url" "$GITHUB_WORKSPACE/$repo_name" | |
| # Verificar se o repositório foi clonado corretamente | |
| ls -la "$GITHUB_WORKSPACE/$repo_name" | |
| done | |
| - name: Run 2ms Scan for each repo | |
| run: | | |
| mkdir -p $GITHUB_WORKSPACE/results | |
| # Obter a lista de repositórios do GITHUB_ENV | |
| IFS=',' read -r -a REPOS_ARRAY <<< "$REPOS_LIST" | |
| # Verificar se o binário 2ms existe no diretório correto | |
| if [ ! -f /tmp/2ms/dist/2ms ]; then | |
| echo "2ms binary not found!" | |
| exit 1 | |
| fi | |
| # Iterar sobre cada repositório | |
| for repo_url in "${REPOS_ARRAY[@]}"; do | |
| repo_name=$(basename "$repo_url" .git) | |
| echo "Cloning repository: $repo_url" | |
| # Criar pasta para o repositório e clonar o repositório | |
| git clone "$repo_url" "$GITHUB_WORKSPACE/$repo_name" | |
| # Verificar se o repositório foi clonado corretamente | |
| ls -la "$GITHUB_WORKSPACE/$repo_name" | |
| # Verificar se o binário 2ms existe no diretório onde foi compilado | |
| ls -la /tmp/2ms/dist/ | |
| echo "Running 2ms scan on $repo_name" | |
| # Executar o scan 2ms no repositório clonado, passando o path correto | |
| /tmp/2ms/dist/2ms filesystem --path "$GITHUB_WORKSPACE/$repo_name" --ignore-on-exit results --report-path "$GITHUB_WORKSPACE/results/$repo_name.sarif" | |
| # Verificar se o comando 2ms foi executado corretamente | |
| if [ $? -ne 0 ]; then | |
| echo "2ms scan failed for $repo_name, continuing with the next repo." | |
| continue | |
| fi | |
| done | |
| - name: Get Results Directory | |
| id: get_results_dir | |
| run: | | |
| echo "results_dir=results" >> $GITHUB_ENV | |
| - name: Get 2ms Version | |
| id: get_twoms_version | |
| run: | | |
| echo "twoms_version=$(curl -s https://api.github.com/repos/checkmarx/2ms/releases/latest | jq -r '.tag_name')" >> $GITHUB_ENV | |
| - name: Set S3 Destination Path | |
| id: set_s3_path | |
| run: | | |
| BRANCH_NAME="${{ github.head_ref || github.ref_name }}" | |
| PR_NUMBER="${{ github.event.number }}" | |
| ENGINE="2ms" | |
| COMMIT_HASH="${{ github.sha }}" | |
| PR_OWNER="${{ github.actor }}" | |
| TARGET_BRANCH="master" | |
| DEST_DIR="${ENGINE}/${TARGET_BRANCH}/${BRANCH_NAME}/${{ env.twoms_version }}/pr-${PR_NUMBER}" | |
| echo "destination_dir=$DEST_DIR" >> $GITHUB_ENV | |
| echo "results_dir=${{ env.results_dir }}" >> $GITHUB_ENV | |
| - name: Organize SARIF files | |
| run: | | |
| mkdir -p "${{ env.results_dir }}/pr-${{ github.event.number }}" | |
| echo "Listing SARIF files before processing..." | |
| ls -la $GITHUB_WORKSPACE/results/ | |
| for sarif_file in $GITHUB_WORKSPACE/results/*.sarif; do | |
| if [[ -f "$sarif_file" ]]; then | |
| project_name=$(basename "$sarif_file" .sarif) | |
| mkdir -p "${{ env.results_dir }}/pr-${{ github.event.number }}/$project_name" | |
| mv "$sarif_file" "${{ env.results_dir }}/pr-${{ github.event.number }}/$project_name/results.sarif" | |
| echo "Moved $sarif_file to pr-${{ github.event.number }}/$project_name/results.sarif" | |
| fi | |
| done | |
| - name: Create Metadata File | |
| run: | | |
| COMMIT_TIMESTAMP=$(git log -1 --format=%ct) | |
| METADATA_PATH="${{ env.results_dir }}/pr-${{ github.event.number }}/metadata.json" | |
| echo '{ | |
| "seq": "'"${COMMIT_TIMESTAMP}"'", | |
| "tag": "'"${{ github.event.number }}"'", | |
| "comment":"'"${{ github.event.pull_request.title }}"'", | |
| "commit": "'"${{ github.sha }}"'", | |
| "owner": "'"${{ github.actor }}"'", | |
| "branch": "'"${{ github.head_ref || github.ref_name }}"'", | |
| "engine": "2ms", | |
| "version": "'"${{ env.twoms_version }}"'" | |
| }' > "$METADATA_PATH" | |
| - name: Upload results to S3 | |
| run: | | |
| aws s3 cp --recursive "${{ env.results_dir }}/pr-${{ github.event.number }}" "s3://${{ secrets.CES_AWS_BUCKET }}/${{ env.destination_dir }}" \ | |
| --storage-class STANDARD | |
| env: | |
| AWS_ACCESS_KEY_ID: ${{ secrets.CES_BUCKET_AWS_ACCESS_KEY }} | |
| AWS_SECRET_ACCESS_KEY: ${{ secrets.CES_BUCKET_AWS_SECRET_ACCESS_KEY }} |