readBuildArtifact
Retrieve and read the content of specific build artifacts from Jenkins jobs, supporting both text and binary file formats with optional build number specification.
Instructions
Read the content of a specific build artifact
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| artifactPath | Yes | Relative path to the artifact (e.g., "target/app.jar", "reports/test.xml") | |
| buildNumber | No | Build number (optional, defaults to last build) | |
| format | No | Format for returning binary files (default: text). Use base64 for binary files. | |
| jobFullName | Yes | Full path of the Jenkins job |
Implementation Reference
- src/tools/artifact-management.js:58-134 (handler)Implements the core logic for reading a build artifact: resolves build number, fetches the artifact content from Jenkins API, handles text/base64/JSON formats, and returns formatted success/error responses.export async function readBuildArtifact(client, args) { const { jobFullName, artifactPath, buildNumber = null, format = "text", } = args; const jobPath = encodeJobPath(jobFullName); const buildPath = buildNumber || "lastBuild"; try { // Get the actual build number if using 'lastBuild' let actualBuildNumber = buildNumber; if (!buildNumber) { const buildResponse = await client.get( `/job/${jobPath}/${buildPath}/api/json?tree=number` ); if (buildResponse.status === 200) { actualBuildNumber = buildResponse.data.number; } else { throw new Error("Could not determine build number"); } } const artifactUrl = `/job/${jobPath}/${actualBuildNumber}/artifact/${artifactPath}`; const responseType = format === "base64" ? "arraybuffer" : "text"; const response = await client.get(artifactUrl, { responseType }); if (response.status === 200) { let content; const mimeType = getMimeType(artifactPath); if (format === "base64") { content = Buffer.from(response.data).toString("base64"); } else { content = response.data; const extension = artifactPath.split(".").pop().toLowerCase(); if ( extension === "json" || (typeof content === "string" && content.trim().startsWith("{")) ) { try { content = JSON.parse(content); } catch {} } } return success("readBuildArtifact", { artifact: { path: artifactPath, buildNumber: actualBuildNumber, mimeType, format, size: response.headers["content-length"] || null, content, }, }); } return failure( "readBuildArtifact", `Artifact not found: ${artifactPath} in build ${actualBuildNumber}`, { statusCode: response.status } ); } catch (error) { if (error.response && error.response.status === 404) { return failure( "readBuildArtifact", `Artifact not found: ${artifactPath}`, { statusCode: 404 } ); } return formatError(error, "readBuildArtifact"); } }
- src/tools/index.js:238-268 (registration)Registers the 'readBuildArtifact' tool in the central toolRegistry, including name, description, input schema for validation, and reference to the handler function.readBuildArtifact: { name: "readBuildArtifact", description: "Read the content of a specific build artifact", inputSchema: { type: "object", properties: { jobFullName: { type: "string", description: "Full path of the Jenkins job", }, artifactPath: { type: "string", description: 'Relative path to the artifact (e.g., "target/app.jar", "reports/test.xml")', }, buildNumber: { type: "integer", description: "Build number (optional, defaults to last build)", }, format: { type: "string", enum: ["text", "base64"], description: "Format for returning binary files (default: text). Use base64 for binary files.", }, }, required: ["jobFullName", "artifactPath"], }, handler: readBuildArtifact, },
- src/tools/index.js:241-268 (schema)Defines the input schema for the readBuildArtifact tool, specifying parameters like jobFullName, artifactPath, optional buildNumber and format.inputSchema: { type: "object", properties: { jobFullName: { type: "string", description: "Full path of the Jenkins job", }, artifactPath: { type: "string", description: 'Relative path to the artifact (e.g., "target/app.jar", "reports/test.xml")', }, buildNumber: { type: "integer", description: "Build number (optional, defaults to last build)", }, format: { type: "string", enum: ["text", "base64"], description: "Format for returning binary files (default: text). Use base64 for binary files.", }, }, required: ["jobFullName", "artifactPath"], }, handler: readBuildArtifact, },