|
| 1 | +name: Create and publish a Docker image |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + pull_request: |
| 8 | + branches: |
| 9 | + - main |
| 10 | + |
| 11 | +# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. |
| 12 | +env: |
| 13 | + REGISTRY: ghcr.io |
| 14 | + IMAGE_NAME: ${{ github.repository }} |
| 15 | + |
| 16 | +# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. |
| 17 | +jobs: |
| 18 | + build-and-push-image: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. |
| 21 | + permissions: |
| 22 | + contents: read |
| 23 | + packages: write |
| 24 | + # |
| 25 | + steps: |
| 26 | + - name: Checkout repository |
| 27 | + uses: actions/checkout@v4 |
| 28 | + # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. |
| 29 | + - name: Log in to the Container registry |
| 30 | + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 |
| 31 | + with: |
| 32 | + registry: ${{ env.REGISTRY }} |
| 33 | + username: ${{ github.actor }} |
| 34 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 35 | + # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels. |
| 36 | + - name: Extract metadata (tags, labels) for Docker |
| 37 | + id: meta |
| 38 | + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 |
| 39 | + with: |
| 40 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 41 | + tags: | |
| 42 | + type=ref,event=branch |
| 43 | + type=ref,event=pr |
| 44 | + type=sha |
| 45 | + env: |
| 46 | + DOCKER_METADATA_PR_HEAD_SHA: true |
| 47 | + # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. |
| 48 | + # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository. |
| 49 | + # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. |
| 50 | + - name: Build and push Docker image |
| 51 | + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 |
| 52 | + with: |
| 53 | + context: . |
| 54 | + push: true |
| 55 | + tags: ${{ steps.meta.outputs.tags }} |
| 56 | + labels: ${{ steps.meta.outputs.labels }} |
| 57 | + file: Dockerfile |
| 58 | + container-vuln-scan: |
| 59 | + needs: build-and-push-image |
| 60 | + runs-on: ubuntu-latest |
| 61 | + if: |
| 62 | + steps: |
| 63 | + - name: Checkout code |
| 64 | + uses: actions/checkout@v2 |
| 65 | + - name: Extract metadata (tags, labels) for Docker |
| 66 | + id: meta |
| 67 | + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 |
| 68 | + with: |
| 69 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 70 | + tags: | |
| 71 | + type=ref,event=branch |
| 72 | + type=ref,event=pr |
| 73 | + type=sha |
| 74 | + env: |
| 75 | + DOCKER_METADATA_PR_HEAD_SHA: true |
| 76 | + - name: Run Trivy vulnerability scanner |
| 77 | + uses: aquasecurity/[email protected] |
| 78 | + id: runscanner |
| 79 | + continue-on-error: true |
| 80 | + env: |
| 81 | + TRIVY_DB_REPOSITORY: public.ecr.aws/aquasecurity/trivy-db:2 |
| 82 | + with: |
| 83 | + image-ref: 'ghcr.io/pulibrary/digital-cicognara-library:${{ steps.meta.outputs.version }}' |
| 84 | + format: 'table' |
| 85 | + exit-code: '1' |
| 86 | + ignore-unfixed: true |
| 87 | + vuln-type: 'os,library' |
| 88 | + severity: 'CRITICAL,HIGH' |
| 89 | + output: 'vulnerabilities.table' |
| 90 | + - name: Set variables |
| 91 | + id: scanner |
| 92 | + if: ${{ always() }} |
| 93 | + run: | |
| 94 | + EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64) |
| 95 | + echo "results<<$EOF" >> $GITHUB_OUTPUT |
| 96 | + echo "$(cat vulnerabilities.table)" >> $GITHUB_OUTPUT |
| 97 | + echo "$EOF" >> $GITHUB_OUTPUT |
| 98 | + - name: Output variable |
| 99 | + if: ${{ always() }} |
| 100 | + env: |
| 101 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 102 | + WORKFLOW_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 103 | + SCANNER_OUTPUTS: ${{ steps.scanner.outputs.results }} |
| 104 | + run: echo "${{ env.SCANNER_OUTPUTS }}" |
| 105 | + - name: Find Comment for scan |
| 106 | + if: github.event_name == 'pull_request' |
| 107 | + uses: peter-evans/find-comment@v3 |
| 108 | + id: fc |
| 109 | + with: |
| 110 | + issue-number: ${{ github.event.pull_request.number }} |
| 111 | + comment-author: 'github-actions[bot]' |
| 112 | + body-includes: 'Container Scanning Status: ' |
| 113 | + - name: Create or update comment |
| 114 | + if: github.event_name == 'pull_request' |
| 115 | + uses: peter-evans/create-or-update-comment@v4 |
| 116 | + env: |
| 117 | + SCANNER_OUTPUTS: ${{ steps.scanner.outputs.results }} |
| 118 | + with: |
| 119 | + comment-id: ${{ steps.fc.outputs.comment-id }} |
| 120 | + issue-number: ${{ github.event.pull_request.number }} |
| 121 | + body: | |
| 122 | + ## Container Scanning Status: ${{ steps.runscanner.outcome != 'success' && '❌ Failure' || '✅ Success' }} |
| 123 | + ``` |
| 124 | + ${{ env.SCANNER_OUTPUTS }} |
| 125 | + ``` |
| 126 | + edit-mode: replace |
| 127 | + - name: Create issue |
| 128 | + if: steps.runscanner.outcome != 'success' && github.event_name != 'pull_request' |
| 129 | + uses: JasonEtco/create-an-issue@v2 |
| 130 | + env: |
| 131 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 132 | + WORKFLOW_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 133 | + SCANNER_OUTPUTS: ${{ steps.scanner.outputs.results }} |
| 134 | + with: |
| 135 | + filename: .github/failed-vuln-check.md |
| 136 | + update_existing: true |
| 137 | + - name: Find existing security issue |
| 138 | + id: issues |
| 139 | + if: steps.runscanner.outcome == 'success' && github.event_name != 'pull_request' |
| 140 | + uses: lee-dohm/select-matching-issues@v1 |
| 141 | + with: |
| 142 | + query: 'Container Vulnerability Scanner Failed is:open ' |
| 143 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 144 | + - name: Close found issues |
| 145 | + continue-on-error: true |
| 146 | + env: |
| 147 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 148 | + if: steps.runscanner.outcome == 'success' && github.event_name != 'pull_request' |
| 149 | + run: cat ${{ steps.issues.outputs.path }} | xargs gh issue close -c 'Container Scan Passing on Merge to Main' |
| 150 | + |
0 commit comments