Manage Jenkins CI servers via the Groovy Script Console API. Use when the user wants to check builds, update pipelines, trigger builds, or view logs.
Trigger
Activate when the user mentions Jenkins, CI builds, deployment pipeline, or build status.
Base Directory
All script paths are relative to this skill's base directory.
Setup
Before first use, check if ~/.config/jenkins.json exists:
python3 scripts/jenkins_api.py check-config
If the config is missing, ask the user for:
- Jenkins URL (e.g.,
http://jenkins.example.com:8080) - Username (the Jenkins user with Script Console access)
- API Token (generated from Jenkins → User → Configure → API Token)
Then save the config:
python3 scripts/jenkins_api.py init --url '<URL>' --user '<USER>' --token '<TOKEN>'
The script validates the connection before saving. If it fails, report the error and ask the user to check their credentials.
Architecture
Credentials are stored in ~/.config/jenkins.json (outside any repo):
{
"url": "http://your-jenkins:8080",
"user": "your-username",
"token": "your-api-token"
}
Important: The config.xml REST API (POST) may not work if your user lacks Job/Configure permission. This skill uses the Groovy Script Console API (POST /scriptText) which only requires Script Console access.
Usage
Run the CLI helper for common operations:
python3 scripts/jenkins_api.py <command> [args...]
Commands
| Command | Args | Description |
|---|---|---|
list |
— | List all Jenkins jobs |
status |
<job_name> |
Get last build status |
trigger |
<job_name> |
Trigger a new build |
log |
<job_name> |
Get console log (last 200 lines) |
script |
<job_name> |
Get the inline pipeline script |
groovy |
<script> |
Run arbitrary Groovy on Script Console |
Examples
# Check build status
python3 scripts/jenkins_api.py status my-job
# Trigger a build
python3 scripts/jenkins_api.py trigger my-job
# View build log
python3 scripts/jenkins_api.py log my-job
# Get pipeline script
python3 scripts/jenkins_api.py script my-job
# Run custom Groovy
python3 scripts/jenkins_api.py groovy 'println Jenkins.instance.allItems*.fullName'
Modifying Pipeline Scripts
To update a pipeline inline script programmatically, use the Groovy Script Console via run_groovy(). Example pattern:
from scripts.jenkins_api import run_groovy
result = run_groovy("""
import jenkins.model.Jenkins
import org.jenkinsci.plugins.workflow.job.WorkflowJob
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition
def job = Jenkins.instance.getItemByFullName("my-job") as WorkflowJob
def oldScript = ((CpsFlowDefinition) job.definition).script
def newScript = oldScript.replace("old text", "new text")
job.definition = new CpsFlowDefinition(newScript, false)
job.save()
println "Done"
""")
IMPORTANT: Script Security Approval
When setting CpsFlowDefinition(script, false) (sandbox disabled), Jenkins requires the script to be approved by an admin before it can run. The next build will fail with UnapprovedUsageException until approved.
To auto-approve pending scripts after modifying a pipeline:
run_groovy("""
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval
def sa = ScriptApproval.get()
sa.pendingScripts.each { ps ->
sa.approveScript(ps.hash)
println "Approved: ${ps.hash}"
}
""")
Always run this approval step immediately after modifying any pipeline script.
Modifying FreeStyleProject Build Steps
FreeStyleProject jobs (not Pipeline) require a different approach. To add/modify build steps:
run_groovy("""
import jenkins.model.Jenkins
import hudson.tasks.Shell
def job = Jenkins.instance.getItemByFullName("my-freestyle-job")
def builders = job.buildersList
def existing = builders.toList()
def newStep = new Shell("#!/usr/bin/env zsh\\nsource ~/.zshrc\\nmy-command")
builders.clear()
builders.add(existing[0]) // Keep first step (e.g., EnvInject)
builders.add(newStep) // Insert new step
builders.add(existing[1]) // Keep original build step
job.save()
""")
Network Notes
- If your Jenkins domain is blocked by ICP compliance (common for Chinese cloud providers), the script auto-resolves hostnames to IP via regex in
_load_config() - Configure the hostname-to-IP mapping in
jenkins_api.pyif needed