site/docs/integrations/jenkins.md
This guide demonstrates how to integrate Promptfoo's LLM evaluation into your Jenkins pipeline. This setup enables automatic testing of your prompts and models whenever changes are made to your repository.
Create a Jenkinsfile in your repository root. Here's a basic configuration that installs Promptfoo and runs evaluations:
pipeline {
agent any
environment {
OPENAI_API_KEY = credentials('openai-api-key')
PROMPTFOO_CACHE_PATH = '~/.promptfoo/cache'
}
stages {
stage('Setup') {
steps {
sh 'npm install -g promptfoo'
}
}
stage('Evaluate Prompts') {
steps {
script {
try {
sh 'promptfoo eval -c promptfooconfig.yaml --prompts prompts/**/*.json --share -o output.json'
} catch (Exception e) {
currentBuild.result = 'FAILURE'
error("Prompt evaluation failed: ${e.message}")
}
}
}
}
stage('Process Results') {
steps {
script {
def output = readJSON file: 'output.json'
echo "Evaluation Results:"
echo "Successes: ${output.results.stats.successes}"
echo "Failures: ${output.results.stats.failures}"
if (output.shareableUrl) {
echo "View detailed results at: ${output.shareableUrl}"
}
if (output.results.stats.failures > 0) {
currentBuild.result = 'UNSTABLE'
}
}
}
}
}
post {
always {
archiveArtifacts artifacts: 'output.json', fingerprint: true
}
}
}
You'll need to add the API keys for any LLM providers you're using. For example, if you're using OpenAI, you'll need to add the OpenAI API key.
To implement caching for better performance and reduced API costs:
mkdir -p ~/.promptfoo/cache
chown -R jenkins:jenkins ~/.promptfoo/cache
Here's an example of a more advanced pipeline with additional features:
The advanced configuration includes several important improvements:
timeout option ensures builds don't run indefinitely (1 hour limit)pollSCMprompts/ directory changepipeline {
agent any
environment {
OPENAI_API_KEY = credentials('openai-api-key')
PROMPTFOO_CACHE_PATH = '~/.promptfoo/cache'
}
options {
timeout(time: 1, unit: 'HOURS')
timestamps()
}
triggers {
pollSCM('H/15 * * * *')
}
stages {
stage('Setup') {
steps {
sh 'npm install -g promptfoo'
}
}
stage('Evaluate Prompts') {
when {
changeset 'prompts/**'
}
steps {
script {
try {
sh '''
promptfoo eval \
-c promptfooconfig.yaml \
--prompts prompts/**/*.json \
--share \
-o output.json
'''
} catch (Exception e) {
currentBuild.result = 'FAILURE'
error("Prompt evaluation failed: ${e.message}")
}
}
}
}
stage('Process Results') {
steps {
script {
def output = readJSON file: 'output.json'
// Create HTML report
writeFile file: 'evaluation-report.html', text: """
<html>
<body>
<h1>Prompt Evaluation Results</h1>
<p>Successes: ${output.results.stats.successes}</p>
<p>Failures: ${output.results.stats.failures}</p>
<p>View detailed results: <a href="${output.shareableUrl}">${output.shareableUrl}</a></p>
</body>
</html>
"""
// Publish HTML report
publishHTML([
allowMissing: false,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: '.',
reportFiles: 'evaluation-report.html',
reportName: 'Prompt Evaluation Report'
])
if (output.results.stats.failures > 0) {
currentBuild.result = 'UNSTABLE'
}
}
}
}
}
post {
always {
archiveArtifacts artifacts: 'output.json,evaluation-report.html', fingerprint: true
cleanWs()
}
failure {
emailext (
subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
body: "Prompt evaluation failed. Check console output at ${env.BUILD_URL}",
recipientProviders: [[$class: 'DevelopersRecipientProvider']]
)
}
}
}
Common issues and solutions:
Permission issues:
Pipeline timeout:
Cache problems:
rm -rf ~/.promptfoo/cache/*Node.js issues:
nodejs tool installer in JenkinsFor more information on Promptfoo configuration and usage, refer to the configuration reference.