|
| 1 | +--- |
| 2 | + |
| 3 | +name: Docker Image CI |
| 4 | + |
| 5 | +on: # yamllint disable-line rule:truthy |
| 6 | + push: |
| 7 | + branches: ["main"] |
| 8 | + pull_request: |
| 9 | + # The branches below must be a subset of the branches above |
| 10 | + branches: ["main"] |
| 11 | + workflow_dispatch: |
| 12 | + |
| 13 | +env: |
| 14 | + IMAGE_NAME: algorithm-exercises-rust |
| 15 | + ARTIFACT_NAME: algorithm-exercises-rust_${{ github.sha }} |
| 16 | + |
| 17 | +jobs: |
| 18 | + |
| 19 | + build: |
| 20 | + name: "Build Docker images" |
| 21 | + runs-on: ubuntu-24.04 |
| 22 | + steps: |
| 23 | + - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5 |
| 24 | + |
| 25 | + - name: Set up Docker Buildx |
| 26 | + uses: docker/setup-buildx-action@v3 |
| 27 | + |
| 28 | + - name: "LINT: Build and push" |
| 29 | + uses: docker/build-push-action@v6 |
| 30 | + with: |
| 31 | + context: . |
| 32 | + target: lint |
| 33 | + outputs: | |
| 34 | + type=docker,dest=/tmp/${{ env.ARTIFACT_NAME }}_lint.tar |
| 35 | + tags: | |
| 36 | + ${{ env.IMAGE_NAME }}:lint |
| 37 | + - name: "LINT: Upload artifact" |
| 38 | + uses: actions/upload-artifact@v4 |
| 39 | + with: |
| 40 | + name: ${{ env.ARTIFACT_NAME }}_lint |
| 41 | + path: /tmp/${{ env.ARTIFACT_NAME }}_lint.tar |
| 42 | + |
| 43 | + - name: "TEST: Build and push" |
| 44 | + uses: docker/build-push-action@v6 |
| 45 | + with: |
| 46 | + context: . |
| 47 | + target: testing |
| 48 | + outputs: | |
| 49 | + type=docker,dest=/tmp/${{ env.ARTIFACT_NAME }}_test.tar |
| 50 | + tags: | |
| 51 | + ${{ env.IMAGE_NAME }}:test |
| 52 | + - name: "TEST: Upload artifact" |
| 53 | + uses: actions/upload-artifact@v4 |
| 54 | + with: |
| 55 | + name: ${{ env.ARTIFACT_NAME }}_test |
| 56 | + path: /tmp/${{ env.ARTIFACT_NAME }}_test.tar |
| 57 | + |
| 58 | + - name: "PRODUCTION: Build and push" |
| 59 | + uses: docker/build-push-action@v6 |
| 60 | + with: |
| 61 | + context: . |
| 62 | + target: production |
| 63 | + outputs: | |
| 64 | + type=docker,dest=/tmp/${{ env.ARTIFACT_NAME }}_prod.tar |
| 65 | + tags: | |
| 66 | + ${{ env.IMAGE_NAME }}:latest |
| 67 | + ${{ env.IMAGE_NAME }}:${{ github.sha }} |
| 68 | + - name: "PRODUCTION: Upload artifact" |
| 69 | + uses: actions/upload-artifact@v4 |
| 70 | + with: |
| 71 | + name: ${{ env.ARTIFACT_NAME }}_prod |
| 72 | + path: /tmp/${{ env.ARTIFACT_NAME }}_prod.tar |
| 73 | + |
| 74 | + lint: |
| 75 | + name: "Run in docker: LINT" |
| 76 | + runs-on: ubuntu-24.04 |
| 77 | + needs: build |
| 78 | + steps: |
| 79 | + - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5 |
| 80 | + |
| 81 | + - name: Download artifact |
| 82 | + uses: actions/download-artifact@v5 |
| 83 | + with: |
| 84 | + name: ${{ env.ARTIFACT_NAME }}_lint |
| 85 | + path: /tmp/ |
| 86 | + |
| 87 | + - name: Load image |
| 88 | + run: | |
| 89 | + docker load --input /tmp/${{ env.ARTIFACT_NAME }}_lint.tar |
| 90 | + docker image ls -a |
| 91 | +
|
| 92 | + - name: Run lint |
| 93 | + run: > |
| 94 | + docker |
| 95 | + compose --profile lint |
| 96 | + run --rm ${{ env.IMAGE_NAME }}-lint |
| 97 | + make lint |
| 98 | +
|
| 99 | + test: |
| 100 | + name: "Run in docker: TEST" |
| 101 | + runs-on: ubuntu-24.04 |
| 102 | + needs: build |
| 103 | + steps: |
| 104 | + - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5 |
| 105 | + |
| 106 | + - name: Download artifact |
| 107 | + uses: actions/download-artifact@v5 |
| 108 | + with: |
| 109 | + name: ${{ env.ARTIFACT_NAME }}_test |
| 110 | + path: /tmp/ |
| 111 | + |
| 112 | + - name: Load image |
| 113 | + run: | |
| 114 | + docker load --input /tmp/${{ env.ARTIFACT_NAME }}_test.tar |
| 115 | + docker image ls -a |
| 116 | +
|
| 117 | + - name: Run test |
| 118 | + run: > |
| 119 | + docker |
| 120 | + compose --profile test |
| 121 | + run --rm ${{ env.IMAGE_NAME }}-test |
| 122 | + make test |
| 123 | +
|
| 124 | + # yamllint disable rule:line-length |
| 125 | + # security: |
| 126 | + # name: "Snyk Container" |
| 127 | + # runs-on: ubuntu-24.04 |
| 128 | + # needs: build |
| 129 | + # permissions: |
| 130 | + # actions: read |
| 131 | + # contents: read |
| 132 | + # security-events: write |
| 133 | + # steps: |
| 134 | + # - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 |
| 135 | + # - name: Download artifact |
| 136 | + # uses: actions/download-artifact@v4 |
| 137 | + # with: |
| 138 | + # name: ${{ env.ARTIFACT_NAME }}_prod |
| 139 | + # path: /tmp/ |
| 140 | + |
| 141 | + # - name: Load image |
| 142 | + # run: | |
| 143 | + # docker load --input /tmp/${{ env.ARTIFACT_NAME }}_prod.tar |
| 144 | + # docker image ls -a |
| 145 | + |
| 146 | + # - name: Run Snyk to check Docker image for vulnerabilities |
| 147 | + # # Snyk can be used to break the build when it detects vulnerabilities. |
| 148 | + # # In this case we want to upload the issues to GitHub Code Scanning |
| 149 | + # continue-on-error: true |
| 150 | + # uses: snyk/actions/docker@master |
| 151 | + # env: |
| 152 | + # # yamllint disable rule:line-length |
| 153 | + # # In order to use the Snyk Action you will need to have a Snyk API token. |
| 154 | + # # See https://docs.snyk.io/integrations/ci-cd-integrations/github-actions-integration#getting-your-snyk-token |
| 155 | + # # or you can sign up for free at https://snyk.io/login |
| 156 | + # # yamllint enable rule:line-length |
| 157 | + # SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} |
| 158 | + # with: |
| 159 | + # image: ${{ env.IMAGE_NAME }}:${{ github.sha }} |
| 160 | + # args: --file=Dockerfile |
| 161 | + # # yamllint disable rule:line-length |
| 162 | + # # https://github.com/github/codeql-action/issues/2187#issuecomment-2043220400 |
| 163 | + # - name: Replace security-severity undefined for license-related findings |
| 164 | + # run: | |
| 165 | + # sed -i 's/"security-severity": "undefined"/"security-severity": "0"/g' snyk.sarif |
| 166 | + # sed -i 's/"security-severity": "null"/"security-severity": "0"/g' snyk.sarif |
| 167 | + # # yamllint enable rule:line-length |
| 168 | + # - name: Upload result to GitHub Code Scanning |
| 169 | + # uses: github/codeql-action/upload-sarif@v3 |
| 170 | + # with: |
| 171 | + # sarif_file: 'snyk.sarif' |
| 172 | + # yamllint enable rule:line-length |
| 173 | + |
| 174 | + scan: |
| 175 | + name: "Trivy" |
| 176 | + runs-on: ubuntu-24.04 |
| 177 | + needs: build |
| 178 | + permissions: |
| 179 | + actions: read |
| 180 | + contents: read |
| 181 | + security-events: write |
| 182 | + steps: |
| 183 | + - name: Download artifact |
| 184 | + uses: actions/download-artifact@v5 |
| 185 | + with: |
| 186 | + name: ${{ env.ARTIFACT_NAME }}_prod |
| 187 | + path: /tmp/ |
| 188 | + |
| 189 | + - name: Load image |
| 190 | + run: | |
| 191 | + docker load --input /tmp/${{ env.ARTIFACT_NAME }}_prod.tar |
| 192 | + docker image ls -a |
| 193 | +
|
| 194 | + - name: Run Trivy vulnerability scanner (cli report) |
| 195 | + uses: aquasecurity/trivy-action@0.33.1 |
| 196 | + with: |
| 197 | + image-ref: ${{ env.IMAGE_NAME }}:${{ github.sha }} |
| 198 | + format: 'table' |
| 199 | + env: |
| 200 | + TRIVY_DB_REPOSITORY: ${{ vars.TRIVY_DB_REPOSITORY }} |
| 201 | + |
| 202 | + - name: Run Trivy vulnerability scanner (sarif report) |
| 203 | + uses: aquasecurity/trivy-action@0.33.1 |
| 204 | + with: |
| 205 | + image-ref: ${{ env.IMAGE_NAME }}:${{ github.sha }} |
| 206 | + format: 'sarif' |
| 207 | + output: 'trivy-results.sarif' |
| 208 | + env: |
| 209 | + TRIVY_DB_REPOSITORY: ${{ vars.TRIVY_DB_REPOSITORY }} |
| 210 | + |
| 211 | + - name: Upload Trivy scan results to GitHub Security tab |
| 212 | + uses: github/codeql-action/upload-sarif@v3 |
| 213 | + with: |
| 214 | + sarif_file: 'trivy-results.sarif' |
0 commit comments