|
| 1 | +name: Build and Push - Regex Detector |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: |
| 5 | + - main |
| 6 | + paths-ignore: |
| 7 | + - "README.md" |
| 8 | + - "LICENCE" |
| 9 | + - "curl.sh" |
| 10 | + tags: |
| 11 | + - v* |
| 12 | + pull_request_target: |
| 13 | + paths-ignore: |
| 14 | + - "README.md" |
| 15 | + - "LICENCE" |
| 16 | + - "curl.sh" |
| 17 | + types: [labeled, opened, synchronize, reopened] |
| 18 | +jobs: |
| 19 | + # Ensure that tests pass before publishing a new image. |
| 20 | + build-and-push-ci: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + permissions: |
| 23 | + contents: read |
| 24 | + pull-requests: write |
| 25 | + security-events: write |
| 26 | + steps: # Assign context variable for various action contexts (tag, main, CI) |
| 27 | + - name: Assigning CI context |
| 28 | + if: github.head_ref != '' && github.head_ref != 'main' && !startsWith(github.ref, 'refs/tags/v') |
| 29 | + run: echo "BUILD_CONTEXT=ci" >> $GITHUB_ENV |
| 30 | + - name: Assigning tag context |
| 31 | + if: github.head_ref == '' && startsWith(github.ref, 'refs/tags/v') |
| 32 | + run: echo "BUILD_CONTEXT=tag" >> $GITHUB_ENV |
| 33 | + - name: Assigning main context |
| 34 | + if: github.head_ref == '' && github.ref == 'refs/heads/main' |
| 35 | + run: echo "BUILD_CONTEXT=main" >> $GITHUB_ENV |
| 36 | + # |
| 37 | + # Run checkouts |
| 38 | + - uses: mheap/github-action-required-labels@v4 |
| 39 | + if: env.BUILD_CONTEXT == 'ci' |
| 40 | + with: |
| 41 | + mode: minimum |
| 42 | + count: 1 |
| 43 | + labels: "ok-to-test, lgtm, approved" |
| 44 | + - uses: actions/checkout@v3 |
| 45 | + if: env.BUILD_CONTEXT == 'ci' |
| 46 | + with: |
| 47 | + ref: ${{ github.event.pull_request.head.sha }} |
| 48 | + - uses: actions/checkout@v3 |
| 49 | + if: env.BUILD_CONTEXT == 'main' || env.BUILD_CONTEXT == 'tag' |
| 50 | + # |
| 51 | + # Print variables for debugging |
| 52 | + - name: Log reference variables |
| 53 | + run: | |
| 54 | + echo "CONTEXT: ${{ env.BUILD_CONTEXT }}" |
| 55 | + echo "GITHUB.REF: ${{ github.ref }}" |
| 56 | + echo "GITHUB.HEAD_REF: ${{ github.head_ref }}" |
| 57 | + echo "SHA: ${{ github.event.pull_request.head.sha }}" |
| 58 | + echo "MAIN IMAGE AT: ${{ vars.QUAY_RELEASE_REPO }}:latest" |
| 59 | + echo "CI IMAGE AT: quay.io/trustyai/regex-detector-ci:${{ github.event.pull_request.head.sha }}" |
| 60 | +
|
| 61 | + # Set environments depending on context |
| 62 | + - name: Set CI environment |
| 63 | + if: env.BUILD_CONTEXT == 'ci' |
| 64 | + run: | |
| 65 | + echo "TAG=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV |
| 66 | + echo "IMAGE_NAME=quay.io/trustyai/regex-detector-ci" >> $GITHUB_ENV |
| 67 | + - name: Set main-branch environment |
| 68 | + if: env.BUILD_CONTEXT == 'main' |
| 69 | + run: | |
| 70 | + echo "TAG=latest" >> $GITHUB_ENV |
| 71 | + echo "IMAGE_NAME=${{ vars.QUAY_RELEASE_REPO }}" >> $GITHUB_ENV |
| 72 | + - name: Set tag environment |
| 73 | + if: env.BUILD_CONTEXT == 'tag' |
| 74 | + run: | |
| 75 | + echo "TAG=${{ github.ref_name }}" >> $GITHUB_ENV |
| 76 | + echo "IMAGE_NAME=${{ vars.QUAY_RELEASE_REPO }}" >> $GITHUB_ENV |
| 77 | + # |
| 78 | + # Run docker commands |
| 79 | + - name: Put expiry date on CI-tagged image |
| 80 | + if: env.BUILD_CONTEXT == 'ci' |
| 81 | + run: echo 'LABEL quay.expires-after=7d#' >> Dockerfile |
| 82 | + - name: Build image |
| 83 | + run: docker build -t ${{ env.IMAGE_NAME }}:$TAG -f Dockerfile . |
| 84 | + - name: Log in to Quay |
| 85 | + run: docker login -u ${{ secrets.QUAY_ROBOT_USERNAME }} -p ${{ secrets.QUAY_ROBOT_SECRET }} quay.io |
| 86 | + - name: Push to Quay CI repo |
| 87 | + run: docker push ${{ env.IMAGE_NAME }}:$TAG |
| 88 | + |
| 89 | + # Leave comment |
| 90 | + - uses: peter-evans/find-comment@v3 |
| 91 | + name: Find Comment |
| 92 | + if: env.BUILD_CONTEXT == 'ci' |
| 93 | + id: fc |
| 94 | + with: |
| 95 | + issue-number: ${{ github.event.pull_request.number }} |
| 96 | + comment-author: 'github-actions[bot]' |
| 97 | + body-includes: PR image build completed successfully |
| 98 | + - uses: peter-evans/create-or-update-comment@v4 |
| 99 | + if: env.BUILD_CONTEXT == 'ci' |
| 100 | + name: Generate/update success message comment |
| 101 | + with: |
| 102 | + comment-id: ${{ steps.fc.outputs.comment-id }} |
| 103 | + issue-number: ${{ github.event.pull_request.number }} |
| 104 | + edit-mode: replace |
| 105 | + body: | |
| 106 | + PR image build completed successfully! |
| 107 | + |
| 108 | + 📦 [PR image](https://quay.io/trustyai/regex-detector-ci?tab=tags): `quay.io/trustyai/regex-detector-ci:${{ github.event.pull_request.head.sha }}` |
| 109 | + - name: Trivy scan |
| 110 | + uses: aquasecurity/[email protected] |
| 111 | + with: |
| 112 | + scan-type: 'image' |
| 113 | + image-ref: "${{ env.IMAGE_NAME }}:${{ env.TAG }}" |
| 114 | + format: 'sarif' |
| 115 | + output: 'trivy-results.sarif' |
| 116 | + severity: 'MEDIUM,HIGH,CRITICAL' |
| 117 | + exit-code: '0' |
| 118 | + ignore-unfixed: false |
| 119 | + vuln-type: 'os,library' |
| 120 | + |
| 121 | + - name: Update Security tab |
| 122 | + uses: github/codeql-action/upload-sarif@v3 |
| 123 | + with: |
| 124 | + sarif_file: 'trivy-results.sarif' |
0 commit comments