Trivy test #107
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: PR Test & Analysis | |
| on: | |
| pull_request: | |
| types: [ opened, reopened, synchronize ] | |
| branches: | |
| - main | |
| - dev | |
| concurrency: | |
| group: ci-pr-${{ github.base_ref }} | |
| cancel-in-progress: true | |
| jobs: | |
| fetch-and-diff: | |
| runs-on: ubuntu-latest | |
| env: | |
| MODULES: demo demo1 | |
| outputs: | |
| modified_modules: ${{ steps.determine_modules.outputs.modules }} | |
| steps: | |
| - name: Git Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Get Modified Files | |
| run: | | |
| MODIFIED_FILES=$(git diff --name-only origin/${{ github.head_ref }} origin/${{ github.base_ref }} | tr '\n' ' ') | |
| echo $MODIFIED_FILES | |
| echo "MODIFIED_FILES=$MODIFIED_FILES" >> $GITHUB_ENV | |
| - name: Fetch Base Branch | |
| run: | | |
| echo "From : ${{ github.head_ref }}" | |
| echo "To : ${{ github.base_ref }}" | |
| git fetch origin +refs/heads/${{ github.head_ref }}:refs/remotes/origin/${{ github.base_ref }} | |
| - name: Determine Modified Modules | |
| id: determine_modules | |
| run: | | |
| IFS=' ' read -r -a MODIFIED_FILES_ARRAY <<< "${{ env.MODIFIED_FILES }}" | |
| MODIFIED_MODULES=() | |
| echo "----------------------------------" | |
| for FILE in "${MODIFIED_FILES_ARRAY[@]}"; do | |
| for MODULE in $MODULES; do | |
| if [[ "$FILE" == "$MODULE"* ]]; then | |
| if ! [[ " ${MODIFIED_MODULES[@]} " =~ " ${MODULE} " ]]; then | |
| echo "changes=true" >> $GITHUB_ENV | |
| echo "Add Module($MODULE)" | |
| MODIFIED_MODULES+=("$MODULE") | |
| fi | |
| fi | |
| done | |
| done | |
| MODULE_MATRIX="" | |
| for MODULE in "${MODIFIED_MODULES[@]}"; do | |
| echo ${MODULE} | |
| if [ -n "$MODULE_MATRIX" ]; then | |
| MODULE_MATRIX+=",\"${MODULE}\"" | |
| else | |
| MODULE_MATRIX+="\"${MODULE}\"" | |
| fi | |
| done | |
| MODULE_MATRIX="[$MODULE_MATRIX]" | |
| echo "MODIFIED_MODULE_MATRIX=$MODULE_MATRIX" | |
| echo "modules=$(echo $MODULE_MATRIX)" >> $GITHUB_OUTPUT | |
| - name: Login to DockerHub | |
| uses: docker/login-action@v3 | |
| if: env.changes =='true' | |
| with: | |
| username: ${{ vars.DOCKERHUB_ID }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Build | |
| if: env.changes =='true' | |
| run: | | |
| RAW_MODULES=${{ steps.determine_modules.outputs.modules }} | |
| MODULES=$(echo "$RAW_MODULES" | sed 's/\[//g; s/\]//g') | |
| IFS=',' read -ra MODIFIED_MODULES <<< "$MODULES" | |
| for MODULE in "${MODIFIED_MODULES[@]}"; do | |
| docker build -f ${MODULE}/Dockerfile -t jerryworld/${MODULE}-${{ github.base_ref }}:${{ github.sha }} . | |
| docker push jerryworld/${MODULE}-${{ github.base_ref }}:${{ github.sha }} | |
| done | |
| scan: | |
| needs: fetch-and-diff | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| module: ${{ fromJSON(needs.fetch-and-diff.outputs.modified_modules) }} | |
| steps: | |
| - name: Install Trivy | |
| run: | | |
| curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sudo sh -s -- -b /usr/local/bin | |
| - name: Run Trivy vulnerability scanner | |
| run: | | |
| echo "CHECK Target : ${{ matrix.module }}" | |
| MODULE=${{ matrix.module }} | |
| trivy image \ | |
| --format table \ | |
| --ignore-unfixed \ | |
| --vuln-type os,library \ | |
| --severity CRITICAL,HIGH,MEDIUM \ | |
| --output ${MODULE}_trivy-results.sarif \ | |
| jerryworld/${{ matrix.module }}-${{ github.base_ref }}:${{ github.sha }} | |
| - name: check sarif | |
| id: save_sarif | |
| run: | | |
| ls -al | |
| MODULE=${{ matrix.module }} | |
| DATA=$(cat ${MODULE}_trivy-results.sarif) | |
| echo -e "trivy-results<<EOF" >> $GITHUB_OUTPUT | |
| echo -e "$DATA" >> $GITHUB_OUTPUT | |
| echo "EOF" >> $GITHUB_OUTPUT | |
| - name: Add comment | |
| uses: actions/github-script@v6 | |
| env: | |
| TRIVY_RESULTS: ${{ steps.save_sarif.outputs.trivy-results }} | |
| with: | |
| github-token: ${{ secrets.GIT_TOKEN }} | |
| script: | | |
| const { owner, repo } = context.repo; | |
| const pr_number = context.payload.pull_request.number; | |
| github.rest.issues.createComment({ | |
| owner: owner, | |
| repo: repo, | |
| issue_number: pr_number, | |
| body: process.env.TRIVY_RESULTS | |
| }); | |
| push-manifest: | |
| name: Push Manifest | |
| needs: fetch-and-diff | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| module: ${{ fromJSON(needs.fetch-and-diff.outputs.modified_modules) }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: jerry-world/k8s-manifest-test | |
| ref: refs/heads/main | |
| token: ${{ secrets.GIT_TOKEN }} | |
| - name: Update Patch | |
| run: | | |
| git config --global user.email "sy920112@naver.com" | |
| git config --global user.name "jerry-world" | |
| echo "UPDATE Target : ${{ matrix.module }}" | |
| MODULE=${{ matrix.module }} | |
| cd apps/${MODULE}/overlay/${{ github.base_ref }} | |
| after_sha="${{ github.sha }}" | |
| echo "this revision : ${after_sha}" | |
| sed -i "s|\(image:[[:space:]]*[^:]*:\)[^[:space:]]*$|\1${after_sha}|g" ${MODULE}-deployment-patch.yaml | |
| git add -A | |
| git commit -m "update manifest demo" | |
| git push | |
| #env: | |
| # SLACK_VULNERABILITY_WEBHOOK_URL: ${{secrets.SLACK_VULNERABILITY_WEBHOOK_URL}} | |
| # SLACK_PR_NOTIFICATION_WEBHOOK_URL: ${{secret.SLACK_PR_NOTIFICATION_WEBHOOK_URL}} |