pipeline {
agent {
docker {
image 'alpine:3.20'
args '-u root'
}
}
options {
timeout(time: 30, unit: 'MINUTES')
timestamps()
}
environment {
QODEX_HOST = 'https://agents.qodex.ai'
}
stages {
stage('Qodex smoke') {
steps {
withCredentials([string(credentialsId: 'qodex-api-key', variable: 'QODEX_API_KEY')]) {
sh '''
set -euo pipefail
apk add --no-cache curl jq >/dev/null
ENV_NAME=staging
if [ "${BRANCH_NAME:-}" = "main" ]; then ENV_NAME=prod; fi
RESPONSE=$(curl -fsS -X POST \
"$QODEX_HOST/api/projects/$QODEX_PROJECT/webhooks/trigger" \
-H "Authorization: Bearer $QODEX_API_KEY" \
-H "Content-Type: application/json" \
-d "{\\"environment\\":\\"$ENV_NAME\\",\\"tags\\":[\\"smoke\\"]}")
RUN_ID=$(echo "$RESPONSE" | jq -r .testRunId)
if [ -z "$RUN_ID" ] || [ "$RUN_ID" = "null" ]; then
echo "Failed to start run. Response: $RESPONSE"
exit 1
fi
DEADLINE=$(( $(date +%s) + 1500 ))
while [ "$(date +%s)" -lt "$DEADLINE" ]; do
STATUS=$(curl -fsS \
"$QODEX_HOST/api/projects/$QODEX_PROJECT/test-runs/$RUN_ID" \
-H "Authorization: Bearer $QODEX_API_KEY" | jq -r .status)
echo "status=$STATUS"
case "$STATUS" in
completed) exit 0 ;;
failed) exit 1 ;;
*) sleep 10 ;;
esac
done
echo "Timed out waiting for Qodex run $RUN_ID"
exit 1
'''
}
}
}
}
}