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