pipeline { agent any parameters { choice( name: 'TEST_TARGET', choices: [ // ── Groups (run all tests in folder) ── 'tests/business_users', 'tests/individual_users', // ── business_users ── 'tests/business_users/b1_user.spec.ts', 'tests/business_users/b2_user.spec.ts', 'tests/business_users/b3_user.spec.ts', 'tests/business_users/b4_user.spec.ts', 'tests/business_users/b5_user.spec.ts', 'tests/business_users/b6_user.spec.ts', 'tests/business_users/b7_user.spec.ts', 'tests/business_users/b8_user.spec.ts', 'tests/business_users/b9_user.spec.ts', 'tests/business_users/b10_user.spec.ts', // ── individual_users ── 'tests/individual_users/a1_user.spec.ts', 'tests/individual_users/a2_user.spec.ts', 'tests/individual_users/a3_user.spec.ts', 'tests/individual_users/a4_user.spec.ts', 'tests/individual_users/a5_user.spec.ts', 'tests/individual_users/a6_user.spec.ts', 'tests/individual_users/a7_user.spec.ts', 'tests/individual_users/a8_user.spec.ts', 'tests/individual_users/a9_user.spec.ts', 'tests/individual_users/a10_user.spec.ts' ], description: 'Select test file or group to run' ) choice( name: 'BROWSER', choices: [ 'chromium', 'firefox', 'webkit', 'chromium,firefox', 'chromium,webkit', 'firefox,webkit', 'chromium,firefox,webkit' ], description: 'Select browser(s) to run tests on' ) } environment { REMOTE_DIR = '/home/jenkins_user/robocorp-automation' IMAGE_NAME = 'robocorp-automation' REPORTS_DIR = '/home/jenkins_user/robocorp-automation/reports' REPORT_HOST = 'robocorp-automation-reports.promptyf.cloud' RESULTS_API = 'https://robocorp-automation-report-be.promptyf.cloud/api/v1/tests/save' CREATED_USER = 'automation@datafab.ai' } stages { stage('Run Tests on Server B') { steps { script { buildName "#${BUILD_NUMBER} - ${params.TEST_TARGET} - ${params.BROWSER}" writeFile file: 'test_script.sh', text: """#!/bin/bash set -e RP="${REPORTS_DIR}/build-${BUILD_NUMBER}" mkdir -p \${RP} EF="${REMOTE_DIR}/.env" V1="${REMOTE_DIR}/test-results:/app/test-results" V2="\${RP}:/app/allure-report" V3="${REMOTE_DIR}/allure-results:/app/allure-results" VA="${REMOTE_DIR}/.auth:/app/.auth" IM="${IMAGE_NAME}:latest" TS="${params.TEST_TARGET}" BR="${params.BROWSER}" echo "Target : \${TS}" echo "Browser: \${BR}" PROJECTS=\$(echo "\${BR}" | tr ',' '\\n' | sed 's/^/--project=/' | tr '\\n' ' ') PC="npx playwright test \${TS} \${PROJECTS} --reporter=allure-playwright --reporter=json" AC="npx allure generate allure-results -o allure-report --clean" SC="\${PC} || true && \${AC}" DCMD=(docker run --rm --env-file \${EF}) DCMD+=(-e PLAYWRIGHT_JSON_OUTPUT_NAME=/app/test-results/test-results.json) DCMD+=(-v \${V1} -v \${V2} -v \${V3} -v \${VA} \${IM}) DCMD+=(sh -c "\${SC}") "\${DCMD[@]}" echo "Tests completed." # ── Send results to API ── RESULTS_FILE="${REMOTE_DIR}/test-results/test-results.json" if [ -f "\${RESULTS_FILE}" ]; then echo "Sending test results to API..." cat > /tmp/save_results.py << 'PYEOF' import json, sys with open(sys.argv[1]) as f: results = json.load(f) payload = {'test': sys.argv[2], 'build': sys.argv[3], 'results': results, 'is_active': True, 'created_user': sys.argv[4]} with open('/tmp/test_payload.json', 'w') as f: json.dump(payload, f) PYEOF python3 /tmp/save_results.py "\${RESULTS_FILE}" "\${TS}" "build_${BUILD_NUMBER}" "${CREATED_USER}" rm -f /tmp/save_results.py curl -s -o /dev/null -w "API response: %{http_code}\\n" \\ -X POST "${RESULTS_API}" \\ -H 'accept: application/json' \\ -H 'Content-Type: application/json' \\ --data-binary @/tmp/test_payload.json rm -f /tmp/test_payload.json else echo "WARNING: test-results.json not found, skipping API call." fi """ sshPublisher( publishers: [ sshPublisherDesc( configName: 'deploy-server-datafab-prod', transfers: [ sshTransfer( sourceFiles: 'test_script.sh', remoteDirectory: '', execCommand: """#!/bin/bash bash \$HOME/test_script.sh rm -f \$HOME/test_script.sh """ ) ], verbose: true ) ] ) } } } } post { always { script { def reportUrl = "http://${REPORT_HOST}/build-${BUILD_NUMBER}/index.html" echo "=========================================" echo "TEST REPORT: ${reportUrl}" echo "=========================================" } } success { echo 'Tests passed.' } failure { echo 'Tests failed. Check the report link above.' } } }