|
| 1 | +name: build test scan docker images |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - 'main' |
| 7 | + - 'master' |
| 8 | + paths: |
| 9 | + - app/** |
| 10 | + |
| 11 | +env: |
| 12 | + DOCKERFILE_PATH: app |
| 13 | + DOCKERFILE_TAG: ${{ github.event.pull_request.head.sha }} |
| 14 | + REGISTRY_PATH: gcr.io/getindata-images-public/app |
| 15 | + REGISTRY_TYPE: "gcr.io" # If not set then will default to Docker Hub |
| 16 | + REGISTRY_USERNAME: _json_key |
| 17 | + |
| 18 | +jobs: |
| 19 | + buildtestscan: |
| 20 | + runs-on: ubuntu-22.04 |
| 21 | + steps: |
| 22 | + - uses: actions/checkout@v3 |
| 23 | + with: |
| 24 | + fetch-depth: 100 |
| 25 | + |
| 26 | + - name: Set up Docker Buildx |
| 27 | + uses: docker/setup-buildx-action@v2.2.1 |
| 28 | + |
| 29 | + - name: Cache Docker layers |
| 30 | + uses: actions/cache@v3.2.0 |
| 31 | + with: |
| 32 | + path: /tmp/.buildx-cache |
| 33 | + key: ${{ runner.os }}-buildx-${{ env.DOCKERFILE_TAG }} |
| 34 | + restore-keys: | |
| 35 | + ${{ runner.os }}-buildx- |
| 36 | +
|
| 37 | + - name: Login to registry "${{ env.REGISTRY_TYPE }}" |
| 38 | + uses: docker/login-action@v2.1.0 |
| 39 | + with: |
| 40 | + registry: ${{ env.REGISTRY_TYPE }} |
| 41 | + username: ${{ env.REGISTRY_USERNAME }} |
| 42 | + password: ${{ secrets.REGISTRY_PASSWORD }} |
| 43 | + |
| 44 | + - name: Build and push Docker image |
| 45 | + uses: docker/build-push-action@v3.2.0 |
| 46 | + with: |
| 47 | + context: "${{ env.DOCKERFILE_PATH }}" |
| 48 | + push: true |
| 49 | + tags: "${{ env.REGISTRY_PATH }}:${{ env.DOCKERFILE_TAG }}" |
| 50 | + cache-from: type=local,src=/tmp/.buildx-cache |
| 51 | + cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max |
| 52 | + |
| 53 | + - name: Run Checkov action |
| 54 | + id: checkov |
| 55 | + uses: bridgecrewio/checkov-action@v12.1347.0 |
| 56 | + with: |
| 57 | + quiet: true # optional: display only failed checks |
| 58 | + soft_fail: true # optional: do not return an error code if there are failed checks |
| 59 | + framework: dockerfile |
| 60 | + output_format: github_failed_only |
| 61 | + log_level: WARNING # optional: set log level. Default WARNING |
| 62 | + dockerfile_path: "${{ env.DOCKERFILE_PATH }}/Dockerfile" # path to the Dockerfile |
| 63 | + |
| 64 | + - name: Show Checkov results |
| 65 | + uses: actions-ecosystem/action-create-comment@v1 |
| 66 | + with: |
| 67 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 68 | + body: | |
| 69 | + ## Checkov |
| 70 | + ${{ env.CHECKOV_RESULTS }} |
| 71 | +
|
| 72 | + - name: Run Trivy vulnerability scanner |
| 73 | + uses: aquasecurity/trivy-action@0.8.0 |
| 74 | + env: |
| 75 | + TRIVY_USERNAME: ${{ env.REGISTRY_USERNAME }} |
| 76 | + TRIVY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }} |
| 77 | + with: |
| 78 | + image-ref: "${{ env.REGISTRY_PATH }}:${{ env.DOCKERFILE_TAG }}" |
| 79 | + format: 'json' |
| 80 | + exit-code: '0' |
| 81 | + output: results_trivy.json |
| 82 | + ignore-unfixed: false |
| 83 | + vuln-type: 'os,library' |
| 84 | + severity: 'CRITICAL' |
| 85 | + |
| 86 | + - name: Parse Trivy results |
| 87 | + run: | |
| 88 | + echo "| PkgName | InstalledVersion | Severity | Title | CVE URL | |
| 89 | + | ------ | ------ | ------ | ------ | ------ |" > results_trivy.md |
| 90 | + cat results_trivy.json | jq -r '.Results[].Vulnerabilities[] | [.PkgName, .InstalledVersion, .Severity, .Title, .PrimaryURL]| @tsv' | |
| 91 | + awk ' |
| 92 | + BEGIN{ FS = "\t" } # Set field separator to tab |
| 93 | + { |
| 94 | + # Step 2: Replace all tab characters with pipe characters |
| 95 | + gsub("\t", " | ", $0) |
| 96 | +
|
| 97 | + # Step 3: Print fields with Markdown table formatting |
| 98 | + printf "| %s |\n", $0 |
| 99 | + }' >> results_trivy.md |
| 100 | +
|
| 101 | + - name: Export Trivy results |
| 102 | + run: | |
| 103 | + echo 'TRIVY_RESULTS<<EOF' >> $GITHUB_ENV |
| 104 | + cat results_trivy.md >> $GITHUB_ENV |
| 105 | + echo 'EOF' >> $GITHUB_ENV |
| 106 | +
|
| 107 | + - name: Show Trivy results |
| 108 | + uses: actions-ecosystem/action-create-comment@v1 |
| 109 | + with: |
| 110 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 111 | + body: | |
| 112 | + ## Trivy |
| 113 | + ${{ env.TRIVY_RESULTS }} |
| 114 | +
|
| 115 | + - name: Move cache |
| 116 | + if: always() # always run even if the previous step fails |
| 117 | + run: | |
| 118 | + rm -rf /tmp/.buildx-cache |
| 119 | + mv /tmp/.buildx-cache-new /tmp/.buildx-cache |
0 commit comments