1+ name : test
2+
3+ on : pull_request
4+
5+ jobs :
6+ unit-tests :
7+ permissions :
8+ checks : write
9+ pull-requests : write
10+ runs-on : ubuntu-latest
11+ outputs :
12+ unittest-results : ${{ steps.decode-results.outputs.content }}
13+ services :
14+ mariadb :
15+ image : mariadb:latest
16+ env :
17+ MARIADB_ROOT_PASSWORD : root
18+ ports :
19+ - 3306:3306
20+ options : >-
21+ --health-cmd="healthcheck.sh
22+ --connect
23+ --innodb_initialized"
24+ --health-interval=10s
25+ --health-timeout=5s
26+ --health-retries=3
27+
28+ steps :
29+ - uses : actions/checkout@v4
30+ - name : Set up JDK
31+ uses : actions/setup-java@v4
32+ with :
33+ java-version : ' 17'
34+ distribution : ' temurin'
35+ cache : ' maven'
36+
37+ - name : Install MySQL client and Load Database Schema
38+ run : |
39+ sudo apt-get update
40+ sudo apt-get install -y default-mysql-client
41+ mysql -h 127.0.0.1 -u root --password=root -e "CREATE USER IF NOT EXISTS 'mopat'@'%' IDENTIFIED BY 'mopat';"
42+ mysql -h 127.0.0.1 -u root --password=root -e "GRANT ALL PRIVILEGES ON *.* TO 'mopat'@'%'; FLUSH PRIVILEGES;"
43+ mysql -h 127.0.0.1 -u root --password=root < db/installationInitTest.sql
44+
45+ - name : Test with Maven
46+ run : mvn -B test --file pom.xml
47+
48+ - name : Publish Test Report
49+ id : testReport
50+ uses : mikepenz/action-junit-report@v5
51+ if : success() || failure() # always run even if the previous step fails
52+ with :
53+ report_paths : ' **/target/surefire-reports/*.xml'
54+
55+ - name : Decode results
56+ id : decode-results
57+ if : always() && github.event_name == 'pull_request'
58+ run : |
59+ echo "<details><summary>🧪 Unit Test Results</summary>" >> unit-results.md
60+ echo "${{ steps.testReport.outputs.summary }}" >> unit-results.md
61+ echo "</details>" >> unit-results.md
62+ # Save content as output
63+ echo "content<<EOF" >> $GITHUB_OUTPUT
64+ cat unit-results.md >> $GITHUB_OUTPUT
65+ echo "EOF" >> $GITHUB_OUTPUT
66+
67+
68+ selenium-tests :
69+ permissions :
70+ checks : write
71+ pull-requests : write
72+
73+ runs-on : ubuntu-latest
74+ outputs :
75+ selenium-results : ${{ steps.decode-results.outputs.content }}
76+
77+ steps :
78+ # Checkout the repository
79+ - name : Check out the repository
80+ uses : actions/checkout@v4
81+
82+ # Set up Docker
83+ - name : Set up Docker Buildx
84+ uses : docker/setup-buildx-action@v3
85+
86+ # Pull the Chrome image
87+ - name : Pull Selenoid Chrome Image
88+ run : docker pull selenoid/chrome:latest
89+
90+ # Setup Docker-Compose
91+ - name : Docker Compose Action
92+ uses : hoverkraft-tech/compose-action@v2.0.2
93+ with :
94+ compose-file : " selenium/docker-compose.yml"
95+
96+ # Wait for the server to be ready before running tests
97+ - name : Wait for the application to be ready
98+ run : |
99+ until curl --output /dev/null --silent --head --fail http://localhost:8080; do
100+ echo 'Waiting for the application to be ready...'
101+ sleep 5
102+ done
103+ until curl --output /dev/null --silent --head --fail http://localhost:4444/status; do
104+ echo 'Waiting for Selenoid to be ready...'
105+ sleep 5
106+ done
107+
108+ # Set up Python and run Selenium tests
109+ - name : Set up Python
110+ uses : actions/setup-python@v4
111+ with :
112+ python-version : ' 3.13' # Specify the version of Python you are using
113+
114+ - name : Install dependencies
115+ run : pip install -r selenium/requirements.txt # Ensure your Selenium and other dependencies are listed here
116+
117+ - name : Run Selenium tests
118+ run : |
119+ python3 selenium/selenium_tests.py > test-results.log
120+
121+ - name : Save results to env
122+ if : always() && github.event_name == 'pull_request'
123+ id : test_report
124+ run : echo "SELENIUM_TEST_SUMMARY=$(base64 -w 0 test-results.log)" >> $GITHUB_ENV
125+
126+ # Install GitHub CLI
127+ - name : Install GitHub CLI
128+ run : sudo apt-get update && sudo apt-get install -y gh
129+
130+ - name : Decode results
131+ id : decode-results
132+ if : always() && github.event_name == 'pull_request'
133+ run : |
134+ decoded_results=$(echo "${{ env.SELENIUM_TEST_SUMMARY }}" | base64 --decode)
135+ echo "<details><summary>🌐 Selenium Test Results</summary>" > selenium_block.md
136+ echo "$decoded_results" >> selenium_block.md
137+ echo "</details>" >> selenium_block.md
138+ # Save content as output
139+ echo "content<<EOF" >> $GITHUB_OUTPUT
140+ cat selenium_block.md >> $GITHUB_OUTPUT
141+ echo "EOF" >> $GITHUB_OUTPUT
142+
143+ trivy-scan :
144+ runs-on : ubuntu-latest
145+ outputs :
146+ trivy-results : ${{ steps.trivy-table.outputs.content }}
147+ permissions :
148+ contents : read
149+ pull-requests : write
150+
151+ steps :
152+ - name : Checkout code
153+ uses : actions/checkout@v5
154+
155+ - name : Set up Docker Buildx
156+ uses : docker/setup-buildx-action@v3
157+
158+ - name : Build Docker images with docker compose
159+ working-directory : ./.github/docker_testenv
160+ run : |
161+ docker compose build
162+
163+ - name : Run Trivy vulnerability scanner on image
164+ id : trivy
165+ uses : aquasecurity/trivy-action@0.34.2
166+ env :
167+ TRIVY_DISABLE_VEX_NOTICE : " true"
168+ with :
169+ image-ref : docker_testenv-webapp-container:latest
170+ format : json
171+ exit-code : 0
172+ output : trivy-report.json
173+
174+ - name : Convert Trivy JSON to Markdown table (split OS vs app)
175+ id : trivy-table
176+ run : |
177+ # Check if any vulnerabilities exist
178+ if ! jq -e '.Results[].Vulnerabilities | select(length > 0)' trivy-report.json > /dev/null; then
179+ echo "✅ No vulnerabilities found!" >> trivy-comment.md
180+ exit 0
181+ fi
182+
183+ #####################################
184+ # 🐳 1️⃣ Base Image Vulnerabilities (os-pkgs)
185+ #####################################
186+
187+ os_total=$(jq '[.Results[] | select(.Class=="os-pkgs") | .Vulnerabilities[]?] | length' trivy-report.json)
188+ os_with_fixes=$(jq '[.Results[] | select(.Class=="os-pkgs") | .Vulnerabilities[]? | select(.FixedVersion != null and .FixedVersion != "")] | length' trivy-report.json)
189+
190+ echo "<details><summary>🐳 Base Image Vulnerabilities: $os_total vulnerabilities found, $os_with_fixes with fixes</summary>" >> trivy-comment.md
191+ echo "" >> trivy-comment.md
192+
193+ jq -r '
194+ [ .Results[]
195+ | select(.Class == "os-pkgs")
196+ | .Vulnerabilities[]?
197+ ]
198+ | select(length > 0)
199+ | group_by(.PkgName)
200+ | sort_by(.[0].PkgName)
201+ | .[]
202+ | "#### 📦 Package: \([.[0].PkgName])\n"
203+ + "| Severity | Vulnerability ID | Installed Version | Fixed Version |\n"
204+ + "|-----------|------------------|------------------|----------------|\n"
205+ + (map("| \(.Severity) | \(.VulnerabilityID) | \(.InstalledVersion) | \(.FixedVersion // "-") |") | join("\n"))
206+ + "\n"
207+ ' trivy-report.json >> trivy-comment.md
208+
209+ echo "</details>" >> trivy-comment.md
210+ echo "" >> trivy-comment.md
211+
212+ #####################################
213+ # ☕️ 2️⃣ Tomcat / Java / Library Vulnerabilities
214+ #####################################
215+
216+ app_total=$(jq '[.Results[] | select(.Class!="os-pkgs") | .Vulnerabilities[]?] | length' trivy-report.json)
217+ app_with_fixes=$(jq '[.Results[] | select(.Class!="os-pkgs") | .Vulnerabilities[]? | select(.FixedVersion != null and .FixedVersion != "")] | length' trivy-report.json)
218+
219+ echo "<details><summary>☕️ Application / Library Vulnerabilities: $app_total vulnerabilities found, $app_with_fixes with fixes</summary>" >> trivy-comment.md
220+ echo "" >> trivy-comment.md
221+
222+ jq -r '
223+ [ .Results[]
224+ | select(.Class != "os-pkgs")
225+ | .Vulnerabilities[]?
226+ ]
227+ | select(length > 0)
228+ | group_by(.PkgName)
229+ | sort_by(.[0].PkgName)
230+ | .[]
231+ | "#### 📦 Package: \([.[0].PkgName])\n"
232+ + "| Severity | Vulnerability ID | Installed Version | Fixed Version |\n"
233+ + "|-----------|------------------|------------------|----------------|\n"
234+ + (map("| \(.Severity) | \(.VulnerabilityID) | \(.InstalledVersion) | \(.FixedVersion // "-") |") | join("\n"))
235+ + "\n"
236+ ' trivy-report.json >> trivy-comment.md
237+
238+ echo "</details>" >> trivy-comment.md
239+ # Save content as output
240+ echo "content<<EOF" >> $GITHUB_OUTPUT
241+ cat trivy-comment.md >> $GITHUB_OUTPUT
242+ echo "EOF" >> $GITHUB_OUTPUT
243+ comment :
244+ runs-on : ubuntu-latest
245+ needs : [unit-tests, selenium-tests, trivy-scan]
246+ if : always()
247+ permissions :
248+ contents : read
249+ pull-requests : write
250+ steps :
251+ - name : Post PR comment
252+ uses : peter-evans/create-or-update-comment@v4
253+ with :
254+ issue-number : ${{ github.event.pull_request.number }}
255+ body : |
256+ ### Test Results
257+
258+ ${{ needs.unit-tests.outputs.unittest-results }}
259+
260+ ${{ needs.selenium-tests.outputs.selenium-results }}
261+
262+ ### Vulnerability Scan Results
263+
264+ ${{ needs.trivy-scan.outputs.trivy-results }}
265+
266+ env :
267+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
0 commit comments