|
| 1 | +name: Build and Scan Docker Image |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - 'Dockerfile' |
| 9 | + - '.dockerignore' |
| 10 | + - 'package.json' |
| 11 | + - 'package-lock.json' |
| 12 | + - 'tsconfig.json' |
| 13 | + - '.github/workflows/build.yaml' |
| 14 | + |
| 15 | +jobs: |
| 16 | + build-docker-image: |
| 17 | + runs-on: ubuntu-24.04 |
| 18 | + outputs: |
| 19 | + image_tag: ${{ steps.vars.outputs.commit_hash }} |
| 20 | + tar_file: ${{ steps.vars.outputs.tar_file }} |
| 21 | + image_name: ${{ steps.vars.outputs.image_name }} |
| 22 | + steps: |
| 23 | + - name: Checkout repository |
| 24 | + uses: actions/checkout@v4 |
| 25 | + |
| 26 | + - name: Get Commit Hash |
| 27 | + id: vars |
| 28 | + run: | |
| 29 | + commit_hash=$(git rev-parse --short HEAD) |
| 30 | + tar_file="ndc-openapi-${commit_hash}.tar" |
| 31 | + image_name="ndc-openapi:${commit_hash}" |
| 32 | +
|
| 33 | + echo "commit_hash: $commit_hash" |
| 34 | + echo "tar_file: $tar_file" |
| 35 | + echo "image_name: $image_name" |
| 36 | +
|
| 37 | + echo "commit_hash=$commit_hash" >> $GITHUB_ENV |
| 38 | + echo "tar_file=$tar_file" >> $GITHUB_ENV |
| 39 | + echo "image_name=$image_name" >> $GITHUB_ENV |
| 40 | + echo "::set-output name=commit_hash::$commit_hash" |
| 41 | + echo "::set-output name=tar_file::$tar_file" |
| 42 | + echo "::set-output name=image_name::$image_name" |
| 43 | +
|
| 44 | + - name: Set up Docker Buildx |
| 45 | + uses: docker/setup-buildx-action@v3 |
| 46 | + |
| 47 | + - name: Build Docker image |
| 48 | + run: | |
| 49 | + docker build -t $image_name . |
| 50 | +
|
| 51 | + - name: Save Docker image as artifact |
| 52 | + run: | |
| 53 | + docker save -o $tar_file $image_name |
| 54 | +
|
| 55 | + - name: Upload Docker image artifact |
| 56 | + uses: actions/upload-artifact@v4 |
| 57 | + with: |
| 58 | + name: ${{ env.tar_file }} |
| 59 | + path: ${{ env.tar_file }} |
| 60 | + retention-days: 1 |
| 61 | + |
| 62 | + scan-docker-image-with-gokakashi: |
| 63 | + needs: build-docker-image |
| 64 | + runs-on: ubuntu-latest |
| 65 | + steps: |
| 66 | + - name: Download Docker image artifact |
| 67 | + uses: actions/download-artifact@v4 |
| 68 | + with: |
| 69 | + name: ${{ needs.build-docker-image.outputs.tar_file }} |
| 70 | + |
| 71 | + - name: Load Docker image |
| 72 | + run: | |
| 73 | + docker load -i ${{ needs.build-docker-image.outputs.tar_file }} |
| 74 | +
|
| 75 | + - name: Scan docker image with gokakashi |
| 76 | + uses: shinobistack/[email protected] |
| 77 | + with: |
| 78 | + image: '${{ needs.build-docker-image.outputs.image_name }}' |
| 79 | + labels: agentKey=${{ github.run_id }} |
| 80 | + policy: ci-platform |
| 81 | + server: https://gokakashi-server.hasura-app.io |
| 82 | + token: ${{ secrets.GOKAKASHI_API_TOKEN }} |
| 83 | + cf_client_id: ${{ secrets.CF_ACCESS_CLIENT_ID }} |
| 84 | + cf_client_secret: ${{ secrets.CF_ACCESS_CLIENT_SECRET }} |
| 85 | + interval: 10 |
| 86 | + retries: 8 |
| 87 | + |
| 88 | + scan-docker-image-with-trivy: |
| 89 | + needs: build-docker-image |
| 90 | + runs-on: ubuntu-latest |
| 91 | + steps: |
| 92 | + - name: Download Docker image artifact |
| 93 | + uses: actions/download-artifact@v4 |
| 94 | + with: |
| 95 | + name: ${{ needs.build-docker-image.outputs.tar_file }} |
| 96 | + |
| 97 | + - name: Load Docker image |
| 98 | + run: | |
| 99 | + docker load -i ${{ needs.build-docker-image.outputs.tar_file }} |
| 100 | + - name: Run Trivy vulnerability scan |
| 101 | + uses: aquasecurity/trivy-action@master |
| 102 | + with: |
| 103 | + image-ref: '${{ needs.build-docker-image.outputs.image_name }}' |
| 104 | + format: 'table' |
| 105 | + exit-code: 1 |
| 106 | + severity: 'CRITICAL,HIGH' |
0 commit comments