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