This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and SonarQube Analysis | |
on: | |
pull_request: | |
branches: | |
# - dev # when working with a development seat of sonarServer | |
- main | |
push: | |
branches: | |
# - dev # when working with a development seat of sonarServer | |
- main | |
jobs: | |
build-and-test: | |
runs-on: macos-latest | |
env: | |
PORT: 4201 | |
APP_PORT: 4200 | |
steps: | |
- name: Checkout Code | |
uses: actions/checkout@v3 | |
- name: Set Up Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: 22.14.0 | |
- name: Install Dependencies | |
run: | | |
echo "\nInstalling dependencies\n\n" | |
npm ci | |
- name: Run Translation Validation | |
run: | | |
echo "\nRunning translation validation\n\n" | |
cd tests/translation && npx ts-node translation-validation.ts | |
- name: Build the Client | |
run: | | |
echo "\nBuilding the client\n\n" | |
cd client && npm run build | |
- name: Run Server Tests | |
run: | | |
echo "\nRunning server tests\n\n" | |
cd server && npm test | |
- name: Run Client Tests | |
run: | | |
echo "\nRunning client tests\n\n" | |
cd client && npm test | |
# #e2e Tests | |
# TODO uncomment once testcafe works with chrome latest | |
# - name: Start Angular server | |
# run: npm run prod & | |
# - name: Wait for Angular server to start | |
# run: | | |
# until curl --output /dev/null --silent --head --fail http://localhost:4200; do | |
# echo 'Waiting for Angular server...' | |
# sleep 5 | |
# done | |
# - name: Run E2E Tests | |
# run: | | |
# echo "\nRunning e2e tests\n\n" | |
# cd tests/e2e && node clean_screenshots && node test_runner | |
# - name: Run Screenshot Diff Tests | |
# run: | | |
# echo "\nRunning screenshot diff tests\n\n" | |
# cd tests/e2e && npx testcafe-blink-diff screenshots --compare accepted:tested --open --threshold 0.003 | |
- name: Install SonarScanner | |
run: | | |
curl -sSLo sonar-scanner-cli.zip https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-6.2.1.4610-macosx-aarch64.zip | |
unzip -q sonar-scanner-cli.zip -d $HOME/sonar-scanner | |
echo "$HOME/sonar-scanner/sonar-scanner-6.2.1.4610-macosx-aarch64/bin" >> $GITHUB_PATH | |
- name: Run SonarScanner | |
run: | | |
sonar-scanner \ | |
-Dsonar.projectKey=TheGameKnave_angular-momentum \ | |
-Dsonar.organization=thegameknave \ | |
-Dsonar.host.url=https://sonarcloud.io | |
env: | |
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
- name: Wait for Quality Gate | |
run: | | |
#!/bin/bash | |
TASK_FILE=".scannerwork/report-task.txt" | |
echo "TASK_FILE=$TASK_FILE" | |
if [[ ! -f "$TASK_FILE" ]]; then | |
echo "Error: Sonar task file not found: $TASK_FILE" | |
exit 1 | |
fi | |
TASK_ID=$(grep "ceTaskId" "$TASK_FILE" | cut -d= -f2 | tr -d '[:space:]') | |
if [[ -z "$TASK_ID" ]]; then | |
echo "Error: TASK_ID is empty. Check Sonar task file." | |
exit 1 | |
fi | |
echo "Fetching task status... for TASK_ID=$TASK_ID" | |
# Wait for SonarCloud to complete the analysis | |
MAX_RETRIES=15 # Increased retries | |
RETRY_DELAY=10 # Wait time in seconds | |
TRIES=0 | |
while ((TRIES < MAX_RETRIES)); do | |
# Fetch task status from SonarCloud | |
curl -s -u "***:" "https://sonarcloud.io/api/ce/task?id=$TASK_ID" -o task.json | |
# Check if the response file exists and contains data | |
if [[ ! -s task.json ]]; then | |
echo "Warning: Empty response from SonarCloud. Retrying..." | |
sleep $RETRY_DELAY | |
((TRIES++)) | |
continue | |
fi | |
# Output the entire task response for debugging | |
echo "Task API Response: $(cat task.json)" | |
STATUS=$(grep -o '"status":"[^"]*"' task.json | cut -d: -f2 | tr -d '"') | |
if [[ -z "$STATUS" ]]; then | |
echo "Warning: STATUS is empty. Retrying..." | |
sleep $RETRY_DELAY | |
((TRIES++)) | |
continue | |
fi | |
echo "Current Task Status: $STATUS" | |
if [[ "$STATUS" == "SUCCESS" ]]; then | |
break | |
elif [[ "$STATUS" == "FAILED" ]]; then | |
echo "SonarCloud analysis failed!" | |
cat task.json # Show details on failure | |
exit 1 | |
fi | |
((TRIES++)) | |
if [[ "$TRIES" -ge "$MAX_RETRIES" ]]; then | |
echo "SonarCloud analysis taking too long. Exiting." | |
exit 1 | |
fi | |
echo "Waiting for SonarCloud to complete analysis... (Attempt $TRIES/$MAX_RETRIES)" | |
sleep $RETRY_DELAY | |
done | |
echo "Fetching Quality Gate status..." | |
ANALYSIS_ID=$(grep -o '"analysisId":"[^"]*"' task.json | cut -d: -f2 | tr -d '"') | |
if [[ -z "$ANALYSIS_ID" ]]; then | |
echo "Error: Failed to extract ANALYSIS_ID from task.json" | |
exit 1 | |
fi | |
curl -s -u "***:" "https://sonarcloud.io/api/qualitygates/project_status?analysisId=$ANALYSIS_ID" -o gate.json | |
echo "Quality Gate API Response:" | |
cat gate.json | |
GATE_STATUS=$(jq -r '.projectStatus.status' gate.json) | |
if [[ "$GATE_STATUS" != "OK" ]]; then | |
echo "Quality Gate failed!" | |
exit 1 | |
fi | |
echo "Quality Gate passed successfully!" | |
shell: bash |