Skip to main content
Glama
xhuaustc

Jenkins MCP Tool

create_or_update_job_from_jenkinsfile

Create or update Jenkins CI/CD jobs using Jenkinsfile pipeline definitions to automate deployment and testing 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
NameRequiredDescriptionDefault
server_nameYes
job_nameYes
jenkinsfile_contentYes
descriptionNo

Implementation Reference

  • The handler function for the 'create_or_update_job_from_jenkinsfile' tool. It is decorated with @mcp.tool() for registration and implements the logic to create or update a Jenkins pipeline job using the provided Jenkinsfile content, organizing jobs under a user-specific 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)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/xhuaustc/jenkins-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server