Skip to main content
Glama

triggerBuild

Start Jenkins job builds by specifying job names and parameters, including file paths, to automate continuous integration processes.

Instructions

Trigger a Jenkins job build with file and regular parameters

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
jobFullNameYesJenkins job name (e.g., "PlaywrightBDD")
parametersYesBuild parameters including file paths

Implementation Reference

  • The primary handler function that executes the tool logic: triggers a Jenkins job build, handles parameters (string/file), constructs FormData for API POST, extracts queue ID from response.
    export async function triggerBuild(client, args) {
    	const { jobFullName, parameters = {} } = args;
    	if (!jobFullName) return failure("triggerBuild", "jobFullName is required");
    	const jobPath = encodeURIComponent(jobFullName).replace(/%2F/g, "/");
    	const allowAbsolute = process.env.ALLOW_ABSOLUTE_FILE_PARAMS === "1";
    
    	try {
    		const form = new FormData();
    		const jsonParams = { parameter: [] };
    		let fileIndex = 0;
    
    		for (const [key, value] of Object.entries(parameters)) {
    			if (typeof value === "string") {
    				const potentialPath = value;
    				const isAbsolute = path.isAbsolute(potentialPath);
    				const withinCwd =
    					!isAbsolute || potentialPath.startsWith(process.cwd());
    				const exists =
    					withinCwd &&
    					fs.existsSync(potentialPath) &&
    					fs.statSync(potentialPath).isFile();
    
    				if (exists && (!isAbsolute || allowAbsolute)) {
    					const fileFieldName = `file${fileIndex}`;
    					form.append(
    						fileFieldName,
    						fs.createReadStream(potentialPath),
    						path.basename(potentialPath)
    					);
    					jsonParams.parameter.push({
    						name: key,
    						file: fileFieldName,
    					});
    					fileIndex++;
    					continue;
    				} else if (isAbsolute && !allowAbsolute && exists) {
    					return failure(
    						"triggerBuild",
    						`Absolute file paths are not allowed: ${potentialPath}. Set ALLOW_ABSOLUTE_FILE_PARAMS=1 to override.`
    					);
    				}
    			}
    
    			// Regular parameter fallback
    			jsonParams.parameter.push({ name: key, value: String(value) });
    		}
    
    		form.append("json", JSON.stringify(jsonParams));
    
    		const response = await client.post(
    			`${client.baseUrl}/job/${jobPath}/build`,
    			form,
    			{
    				headers: form.getHeaders(),
    				maxContentLength: Infinity,
    				maxBodyLength: Infinity,
    			}
    		);
    
    		if (response.status >= 200 && response.status < 300) {
    			const location =
    				response.headers["location"] || response.headers["Location"];
    			const queueId = location?.match(/queue\/item\/(\d+)/)?.[1] || null;
    			return success("triggerBuild", {
    				message: `Build triggered successfully for ${jobFullName}`,
    				queueUrl: location || null,
    				queueId,
    				statusCode: response.status,
    			});
    		}
    		return failure(
    			"triggerBuild",
    			`Build trigger returned status ${response.status}`,
    			{ statusCode: response.status, data: response.data }
    		);
    	} catch (error) {
    		return formatError(error, "triggerBuild");
    	}
    }
  • Input schema for the triggerBuild tool, specifying jobFullName (required string) and parameters (object with additional properties for flexibility).
    inputSchema: {
    	type: "object",
    	properties: {
    		jobFullName: {
    			type: "string",
    			description: 'Jenkins job name (e.g., "PlaywrightBDD")',
    		},
    		parameters: {
    			type: "object",
    			description: "Build parameters including file paths",
    			additionalProperties: true,
    		},
    	},
    	required: ["jobFullName", "parameters"],
    },
  • Tool registration in the central toolRegistry object, defining name, description, inputSchema, and references the handler function.
    triggerBuild: {
    	name: "triggerBuild",
    	description:
    		"Trigger a Jenkins job build with file and regular parameters",
    	inputSchema: {
    		type: "object",
    		properties: {
    			jobFullName: {
    				type: "string",
    				description: 'Jenkins job name (e.g., "PlaywrightBDD")',
    			},
    			parameters: {
    				type: "object",
    				description: "Build parameters including file paths",
    				additionalProperties: true,
    			},
    		},
    		required: ["jobFullName", "parameters"],
    	},
    	handler: triggerBuild,
    },

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/umishra1504/Jenkins-mcp-server'

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