create_or_update_job_from_jenkinsfile
Automate Jenkins job creation or updates using a Jenkinsfile. Specify server, job name, and pipeline script content to streamline CI/CD workflows.
Instructions
Create or update a Jenkins job based on a Jenkinsfile.
Args:
server_name: Jenkins server name
job_name: Name for the job (create if not exists, update if exists)
jenkinsfile_content: Content of the Jenkinsfile (pipeline script)
description: Optional job description
ctx: MCP context (for logging)
Returns:
Dict containing job creation/update result with status and job_url
Raises:
JenkinsError: Job creation/update failed
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| description | No | ||
| jenkinsfile_content | Yes | ||
| job_name | Yes | ||
| server_name | Yes |
Implementation Reference
- src/jenkins/tools/mcp_tools.py:283-352 (handler)The handler function for the create_or_update_job_from_jenkinsfile MCP tool. It is decorated with @mcp.tool(), which registers it with the MCP server. The function creates or updates a Jenkins pipeline job using the provided Jenkinsfile content, organizing jobs under a MCPS/username folder.@mcp.tool() def create_or_update_job_from_jenkinsfile( server_name: str, job_name: str, jenkinsfile_content: str, description: str = "", ctx: Context = None, ) -> dict: """Create or update a Jenkins job based on a Jenkinsfile. Args: server_name: Jenkins server name job_name: Name for the job (create if not exists, update if exists) jenkinsfile_content: Content of the Jenkinsfile (pipeline script) description: Optional job description ctx: MCP context (for logging) Returns: Dict containing job creation/update result with status and job_url Raises: JenkinsError: Job creation/update failed """ client = JenkinsAPIClient(server_name) # Organize all jobs under MCPS/username directory # Get username from Jenkins server config, extract part before @ if it's an email server_config = client._server_config username = server_config.get("user", "unknown") if "@" in username: username = username.split("@")[0] final_folder_path = f"MCPS/{username}" job_full_name = f"{final_folder_path}/{job_name}" # Create job configuration XML for pipeline job job_config = f"""<?xml version='1.1' encoding='UTF-8'?> <flow-definition plugin="workflow-job"> <actions/> <description>{description}</description> <keepDependencies>false</keepDependencies> <properties/> <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps"> <script>{jenkinsfile_content}</script> <sandbox>true</sandbox> </definition> <triggers/> <disabled>false</disabled> </flow-definition>""" # Check if job already exists try: print(f"===job_full_name: {job_full_name}", flush=True) existing_job = client.get_job_info(job_full_name) print(f"=====existing_job: {existing_job}", flush=True) # Job exists, update it if ctx: ctx.log("info", f"Updating existing job '{job_name}' on {server_name}") ctx.log("debug", f"Target folder: {final_folder_path}") return client.update_job(job_name, job_config, final_folder_path) except Exception as e: # Job doesn't exist, create it print(e, flush=True) if ctx: ctx.log("info", f"Creating new job '{job_name}' on {server_name}") ctx.log("debug", f"Target folder: {final_folder_path}") return client.create_job(job_name, job_config, final_folder_path)