Skip to content

Commit 5880847

Browse files
committed
Add build pipeline for hf detector
1 parent fe608da commit 5880847

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
name: Build and Push
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/trustyai-service-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: Pull prerequisite images
78+
run: |
79+
docker pull $(cat detectors/Dockerfile.hf | grep -o -P '(?<=FROM ).*(?= AS base)')
80+
docker pull $(cat detectors/Dockerfile.hf | grep -o -P '(?<=FROM ).*(?= AS builder)')
81+
- name: Put expiry date on CI-tagged image
82+
if: env.BUILD_CONTEXT == 'ci'
83+
run: echo 'LABEL quay.expires-after=7d#' >> detectors/Dockerfile.hf
84+
- name: Build image
85+
run: cd detectors; docker build -t ${{ env.IMAGE_NAME }}:$TAG Dockerfile.hf
86+
- name: Log in to Quay
87+
run: docker login -u ${{ secrets.QUAY_ROBOT_USERNAME }} -p ${{ secrets.QUAY_ROBOT_SECRET }} quay.io
88+
- name: Push to Quay CI repo
89+
run: docker push ${{ env.IMAGE_NAME }}:$TAG
90+
91+
# Leave comment
92+
- uses: peter-evans/find-comment@v3
93+
name: Find Comment
94+
id: fc
95+
with:
96+
issue-number: ${{ github.event.pull_request.number }}
97+
comment-author: 'github-actions[bot]'
98+
body-includes: PR image build and manifest generation completed successfully
99+
- uses: peter-evans/create-or-update-comment@v4
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 and manifest generation completed successfully!
107+
108+
📦 [PR image](quay.io/trustyai/guardrails-detector-huggingface-runtime-ci:${{ github.event.pull_request.head.sha }}): `quay.io/trustyai/guardrails-detector-huggingface-runtime-ci:${{ github.event.pull_request.head.sha }}`
109+
110+
```
111+
- name: Trivy scan
112+
uses: aquasecurity/[email protected]
113+
with:
114+
scan-type: 'image'
115+
image-ref: "${{ env.IMAGE_NAME }}:${{ env.TAG }}"
116+
format: 'sarif'
117+
output: 'trivy-results.sarif'
118+
severity: 'MEDIUM,HIGH,CRITICAL'
119+
exit-code: '0'
120+
ignore-unfixed: false
121+
vuln-type: 'os,library'
122+
123+
- name: Update Security tab
124+
uses: github/codeql-action/upload-sarif@v3
125+
with:
126+
sarif_file: 'trivy-results.sarif'

0 commit comments

Comments
 (0)