|
| 1 | +# https://github.com/epam/ai-dial-ci/blob/2.4.1/actions/build_docker/action.yml |
| 2 | +# This version has modified values for maintainer and org.opencontainers.image.licenses |
| 3 | + |
| 4 | +name: 'Build docker' |
| 5 | +description: 'Build a docker image, tag it, (optionally) scan it, and (optionally) push it to Dockerhub and GHCR' |
| 6 | +inputs: |
| 7 | + bypass-checks: |
| 8 | + description: 'Do not fail pipeline if checks failed' |
| 9 | + default: 'false' |
| 10 | + push-enabled: |
| 11 | + description: 'Push the image to registry(ies)' |
| 12 | + required: false |
| 13 | + default: 'false' |
| 14 | + trivy-enabled: |
| 15 | + description: 'Enable Trivy scanning' |
| 16 | + required: false |
| 17 | + default: 'true' |
| 18 | + trivy-severity: |
| 19 | + description: 'Severities of vulnerabilities to fail the build' |
| 20 | + required: false |
| 21 | + default: 'CRITICAL,HIGH' |
| 22 | + trivy-severity-for-sarif: |
| 23 | + description: 'Severities of vulnerabilities in SARIF report' |
| 24 | + required: false |
| 25 | + default: 'CRITICAL,HIGH' |
| 26 | + trivy-limit-severities-for-sarif: |
| 27 | + description: 'Limit severities for SARIF format' |
| 28 | + required: false |
| 29 | + default: 'true' |
| 30 | + trivy-vuln-type: |
| 31 | + description: 'Type of vulnerabilities to scan' |
| 32 | + required: false |
| 33 | + default: 'os,library' |
| 34 | + trivy-ignore-unfixed: |
| 35 | + description: 'Ignore unpatched/unfixed vulnerabilities' |
| 36 | + required: false |
| 37 | + default: 'true' |
| 38 | + image-name: |
| 39 | + description: 'Name of the image to build' |
| 40 | + required: true |
| 41 | + image-tag: |
| 42 | + description: 'Tag of the image to build' |
| 43 | + required: true |
| 44 | + image-extra-aliases: |
| 45 | + description: 'Extra aliases for the image, e.g. to publish to multiple registries' |
| 46 | + required: false |
| 47 | + default: '' |
| 48 | + ghcr-username: |
| 49 | + description: 'GitHub Container Registry username. This is required for pushing to GitHub Container Registry' |
| 50 | + required: false |
| 51 | + default: ${{ github.actor }} |
| 52 | + ghcr-password: |
| 53 | + description: 'GitHub Container Registry password/token. This is required for pushing to GitHub Container Registry' |
| 54 | + required: false |
| 55 | + default: '' |
| 56 | + dockerhub-username: |
| 57 | + description: 'Docker Hub username. This is required for pushing to Docker Hub' |
| 58 | + required: false |
| 59 | + default: '' |
| 60 | + dockerhub-password: |
| 61 | + description: 'Docker Hub password/token. This is required for pushing to Docker Hub' |
| 62 | + required: false |
| 63 | + default: '' |
| 64 | + gpr-username: |
| 65 | + description: 'GitHub Package Registry username (for pulling packages from image being built)' |
| 66 | + required: false |
| 67 | + default: ${{ github.actor }} |
| 68 | + gpr-password: |
| 69 | + required: false |
| 70 | + description: 'GitHub Package Registry password/token (for pulling packages from image being built)' |
| 71 | + default: '' |
| 72 | + platforms: |
| 73 | + required: false |
| 74 | + description: 'Docker build platforms (default linux/amd64)' |
| 75 | + default: 'linux/amd64' |
| 76 | + |
| 77 | +runs: |
| 78 | + using: 'composite' |
| 79 | + steps: |
| 80 | + - name: Setup Docker Qemu |
| 81 | + uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392 # v3.6.0 |
| 82 | + - name: Setup Docker buildx |
| 83 | + uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2 # v3.10.0 |
| 84 | + - name: Login to GitHub Container Registry |
| 85 | + if: ${{ fromJSON(inputs.push-enabled) }} # workaround for composite jobs not being able to pass boolean inputs |
| 86 | + uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 # v3.4.0 |
| 87 | + with: |
| 88 | + registry: ghcr.io |
| 89 | + username: ${{ inputs.ghcr-username }} |
| 90 | + password: ${{ inputs.ghcr-password }} |
| 91 | + - name: Login to Docker Hub |
| 92 | + if: ${{ fromJSON(inputs.push-enabled) }} # workaround for composite jobs not being able to pass boolean inputs |
| 93 | + uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 # v3.4.0 |
| 94 | + with: |
| 95 | + username: ${{ inputs.dockerhub-username }} |
| 96 | + password: ${{ inputs.dockerhub-password }} |
| 97 | + - name: Build and export to Docker |
| 98 | + if: ${{ fromJSON(inputs.trivy-enabled) }} # workaround for composite jobs not being able to pass boolean inputs |
| 99 | + uses: docker/build-push-action@14487ce63c7a62a4a324b0bfb37086795e31c6c1 # v6.16.0 |
| 100 | + with: |
| 101 | + context: . |
| 102 | + load: true |
| 103 | + platforms: linux/amd64 |
| 104 | + cache-from: type=gha |
| 105 | + cache-to: type=gha,mode=max |
| 106 | + tags: | |
| 107 | + ${{ inputs.image-name }}:${{ inputs.image-tag }} |
| 108 | + ${{ inputs.image-extra-aliases }} |
| 109 | + labels: | |
| 110 | + maintainer=StatGPT |
| 111 | + org.opencontainers.image.title=${{ inputs.image-name }} |
| 112 | + org.opencontainers.image.version=${{ inputs.image-tag }} |
| 113 | + org.opencontainers.image.description=${{ inputs.image-name }} |
| 114 | + org.opencontainers.image.created=${{ github.event.repository.pushed_at }} |
| 115 | + org.opencontainers.image.licenses=MIT |
| 116 | + org.opencontainers.image.revision=${{ github.sha }} |
| 117 | + org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} |
| 118 | + org.opencontainers.image.url=${{ github.server_url }}/${{ github.repository }} |
| 119 | + secrets: | |
| 120 | + GPR_USERNAME=${{ inputs.gpr-username }} |
| 121 | + GPR_PASSWORD=${{ inputs.gpr-password }} |
| 122 | + env: |
| 123 | + DOCKER_BUILD_RECORD_UPLOAD: false |
| 124 | + - name: Run Trivy vulnerability scanner (SARIF, no fail) |
| 125 | + id: trivy-sarif |
| 126 | + # Do not perform SARIF scan on private repos - GitHub Advanced Security is not enabled |
| 127 | + if: ${{ fromJSON(inputs.trivy-enabled) && !github.event.repository.private }} # workaround for composite jobs not being able to pass boolean inputs |
| 128 | + uses: aquasecurity/trivy-action@7aca5acc9500b463826cc47a47a65ad7d404b045 # TODO: v0.31.0+ |
| 129 | + with: |
| 130 | + image-ref: '${{ inputs.image-name }}:${{ inputs.image-tag }}' |
| 131 | + format: 'sarif' |
| 132 | + exit-code: '0' |
| 133 | + output: 'trivy-results.sarif' |
| 134 | + ignore-unfixed: ${{ inputs.trivy-ignore-unfixed }} |
| 135 | + vuln-type: ${{ inputs.trivy-vuln-type }} |
| 136 | + severity: ${{ inputs.trivy-severity-for-sarif }} |
| 137 | + limit-severities-for-sarif: ${{ inputs.trivy-limit-severities-for-sarif }} |
| 138 | + env: |
| 139 | + TRIVY_DISABLE_VEX_NOTICE: true |
| 140 | + continue-on-error: true # HACK: Alongside with `exit-code: 0`, ensure not failing the workflow |
| 141 | + - name: Upload Trivy scan results to GitHub Security tab |
| 142 | + # Do not upload SARIF reports on private repos - GitHub Advanced Security is not enabled |
| 143 | + if: ${{ !cancelled() && fromJSON(inputs.trivy-enabled) && !github.event.repository.private }} # workaround for composite jobs not being able to pass boolean inputs |
| 144 | + uses: github/codeql-action/upload-sarif@60168efe1c415ce0f5521ea06d5c2062adbeed1b #v3.28.17 |
| 145 | + with: |
| 146 | + sarif_file: 'trivy-results.sarif' |
| 147 | + category: trivy |
| 148 | + env: |
| 149 | + CONTINUE_ON_ERROR: ${{ inputs.bypass-checks }} # Hack to use the input below as a boolean |
| 150 | + continue-on-error: ${{ fromJSON(env.CONTINUE_ON_ERROR) }} |
| 151 | + - name: Run Trivy vulnerability scanner (stdout, table view, may fail) |
| 152 | + if: ${{ fromJSON(inputs.trivy-enabled) }} # workaround for composite jobs not being able to pass boolean inputs |
| 153 | + uses: aquasecurity/trivy-action@7aca5acc9500b463826cc47a47a65ad7d404b045 # TODO: v0.31.0+ |
| 154 | + with: |
| 155 | + image-ref: '${{ inputs.image-name }}:${{ inputs.image-tag }}' |
| 156 | + format: 'table' |
| 157 | + exit-code: '1' |
| 158 | + ignore-unfixed: ${{ inputs.trivy-ignore-unfixed }} |
| 159 | + vuln-type: ${{ inputs.trivy-vuln-type }} |
| 160 | + severity: ${{ inputs.trivy-severity }} |
| 161 | + skip-setup-trivy: ${{ steps.trivy-sarif.conclusion != 'skipped' }} |
| 162 | + env: |
| 163 | + CONTINUE_ON_ERROR: ${{ inputs.bypass-checks }} # Hack to use the input below as a boolean |
| 164 | + TRIVY_SKIP_DB_UPDATE: ${{ steps.trivy-sarif.conclusion != 'skipped' }} |
| 165 | + TRIVY_SKIP_JAVA_DB_UPDATE: ${{ steps.trivy-sarif.conclusion != 'skipped' }} |
| 166 | + TRIVY_DISABLE_VEX_NOTICE: true |
| 167 | + continue-on-error: ${{ fromJSON(env.CONTINUE_ON_ERROR) }} |
| 168 | + - name: Build and push |
| 169 | + if: ${{ fromJSON(inputs.push-enabled) }} # workaround for composite jobs not being able to pass boolean inputs |
| 170 | + uses: docker/build-push-action@14487ce63c7a62a4a324b0bfb37086795e31c6c1 # v6.16.0 |
| 171 | + with: |
| 172 | + context: . |
| 173 | + push: true |
| 174 | + platforms: ${{ inputs.platforms }} |
| 175 | + cache-from: type=gha |
| 176 | + tags: | |
| 177 | + ${{ inputs.image-name }}:${{ inputs.image-tag }} |
| 178 | + ${{ inputs.image-extra-aliases }} |
| 179 | + labels: | |
| 180 | + maintainer=StatGPT |
| 181 | + org.opencontainers.image.title=${{ inputs.image-name }} |
| 182 | + org.opencontainers.image.version=${{ inputs.image-tag }} |
| 183 | + org.opencontainers.image.description=${{ inputs.image-name }} |
| 184 | + org.opencontainers.image.created=${{ github.event.repository.pushed_at }} |
| 185 | + org.opencontainers.image.licenses=MIT |
| 186 | + org.opencontainers.image.revision=${{ github.sha }} |
| 187 | + org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} |
| 188 | + org.opencontainers.image.url=${{ github.server_url }}/${{ github.repository }} |
| 189 | + secrets: | |
| 190 | + GPR_USERNAME=${{ inputs.gpr-username }} |
| 191 | + GPR_PASSWORD=${{ inputs.gpr-password }} |
| 192 | + env: |
| 193 | + DOCKER_BUILD_RECORD_UPLOAD: false |
0 commit comments