|
| 1 | +name: Build and push image to registry |
| 2 | +on: |
| 3 | + pull_request: |
| 4 | + types: |
| 5 | + - synchronize |
| 6 | + push: |
| 7 | + tags: |
| 8 | + - '*' |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | +jobs: |
| 12 | + hadolint: |
| 13 | + uses: ./.github/workflows/docker-lint.yml |
| 14 | + |
| 15 | + tests: |
| 16 | + uses: ./.github/workflows/docker-tests.yml |
| 17 | + |
| 18 | + build: |
| 19 | + permissions: |
| 20 | + contents: read # for actions/checkout to fetch code |
| 21 | + security-events: write # for github/codeql-action/upload-sarif to upload SARIF results |
| 22 | + actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status |
| 23 | + pull-requests: write |
| 24 | + name: Build and push |
| 25 | + runs-on: ubuntu-22.04 |
| 26 | + needs: |
| 27 | + - hadolint |
| 28 | + - tests |
| 29 | + |
| 30 | + steps: |
| 31 | + - name: Checkout |
| 32 | + uses: actions/checkout@v4 |
| 33 | + |
| 34 | + - name: Set up Docker Buildx |
| 35 | + uses: docker/setup-buildx-action@v3 |
| 36 | + |
| 37 | + - name: Login to DockerHub |
| 38 | + uses: docker/login-action@v3 |
| 39 | + with: |
| 40 | + registry: ${{ vars.REGISTRY_URI }} |
| 41 | + username: ${{ secrets.REGISTRY_USERNAME }} |
| 42 | + password: ${{ secrets.REGISTRY_TOKEN }} |
| 43 | + |
| 44 | + - uses: rlespinasse/github-slug-action@v4 |
| 45 | + |
| 46 | + - name: Calculate tag |
| 47 | + id: tag |
| 48 | + run: | |
| 49 | + if [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then |
| 50 | + TAG="pr-${{ github.event.number }}" |
| 51 | + else |
| 52 | + TAG=${{ github.ref_name }} |
| 53 | + fi |
| 54 | + echo "IMAGE=${{ vars.REGISTRY_URI }}/${{ vars.REGISTRY_ORGANIZATION }}/bbb-webhooks:$TAG" >> $GITHUB_OUTPUT |
| 55 | +
|
| 56 | + - name: Docker meta |
| 57 | + id: meta |
| 58 | + uses: docker/metadata-action@v4 |
| 59 | + with: |
| 60 | + images: ${{ steps.tag.outputs.IMAGE }} |
| 61 | + |
| 62 | + - name: Build and push image |
| 63 | + uses: docker/build-push-action@v5 |
| 64 | + with: |
| 65 | + push: true |
| 66 | + tags: ${{ steps.tag.outputs.IMAGE }} |
| 67 | + context: . |
| 68 | + platforms: linux/amd64 |
| 69 | + cache-from: type=registry,ref=${{ steps.tag.outputs.IMAGE }} |
| 70 | + cache-to: type=registry,ref=${{ steps.tag.outputs.IMAGE }},image-manifest=true,oci-mediatypes=true,mode=max |
| 71 | + labels: | |
| 72 | + ${{ steps.meta.outputs.labels }} |
| 73 | +
|
| 74 | + - name: Add comment to pr |
| 75 | + if: ${{ github.event_name == 'pull_request' }} |
| 76 | + uses: actions/github-script@v7 |
| 77 | + with: |
| 78 | + script: | |
| 79 | + github.rest.issues.createComment({ |
| 80 | + issue_number: context.issue.number, |
| 81 | + owner: context.repo.owner, |
| 82 | + repo: context.repo.repo, |
| 83 | + body: "Updated Docker image pushed to `${{ steps.tag.outputs.IMAGE }}`" |
| 84 | + }) |
| 85 | +
|
| 86 | + - name: Run Trivy vulnerability scanner |
| 87 | + uses: aquasecurity/trivy-action@master |
| 88 | + with: |
| 89 | + image-ref: ${{ steps.tag.outputs.IMAGE }} |
| 90 | + format: 'sarif' |
| 91 | + output: 'trivy-results.sarif' |
| 92 | + severity: 'CRITICAL,HIGH' |
| 93 | + env: |
| 94 | + TRIVY_USERNAME: ${{ secrets.REGISTRY_USERNAME }} |
| 95 | + TRIVY_PASSWORD: ${{ secrets.REGISTRY_TOKEN }} |
| 96 | + |
| 97 | + - name: Upload Trivy scan results to GitHub Security tab |
| 98 | + uses: github/codeql-action/upload-sarif@v2 |
| 99 | + with: |
| 100 | + sarif_file: 'trivy-results.sarif' |
0 commit comments