Jenkins API Node.js Client

0.3.1 · abandoned · verified Tue Apr 21

This package, `jenkins-api`, provides a callback-based Node.js client for interacting with Jenkins CI servers. It allows developers to programmatically trigger builds, retrieve build and job information (like console output, build logs, and test results), and manage job configurations (e.g., enable/disable, create/delete jobs). The current stable version is 0.3.1, last published in March 2016. Due to its age and lack of updates, the package is considered abandoned, and it lacks modern features such as Promise-based APIs, async/await support, ESM compatibility, and TypeScript definitions, which are common in contemporary Node.js libraries. Its primary differentiation was a straightforward, direct API mapping for common Jenkins operations at the time of its active development.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing the Jenkins client with an API token and then fetching all jobs and triggering a specific job build.

const jenkinsapi = require('jenkins-api');

// Configure with API Token and Jenkins URL
// Replace with your actual Jenkins URL and API token
const jenkinsUrl = process.env.JENKINS_URL || 'http://localhost:8080';
const username = process.env.JENKINS_USERNAME || 'admin';
const apiToken = process.env.JENKINS_API_TOKEN || 'YOUR_API_TOKEN'; // Generate in Jenkins user settings

const jenkins = jenkinsapi.init(`http://${username}:${apiToken}@${jenkinsUrl.replace(/^https?:\/\//, '')}`);

const jobName = 'your-example-job'; // Replace with an actual job name in your Jenkins instance

console.log(`Attempting to get all jobs from ${jenkinsUrl}...`);
jenkins.all_jobs(function(err, data) {
  if (err) {
    console.error('Error fetching all jobs:', err);
    return;
  }
  console.log('Successfully fetched all jobs:');
  data.forEach(job => console.log(`- ${job.name}`));

  console.log(`\nAttempting to build job: ${jobName}...`);
  // For parameterized builds, use jenkins.build_with_params
  jenkins.build(jobName, function(err, data) {
    if (err) {
      console.error(`Error building job '${jobName}':`, err);
      // A common reason for 'forbidden' is CSRF protection. See warnings.
      return;
    }
    console.log(`Successfully triggered build for '${jobName}':`, data);
    console.log('Note: Data might be empty for successful build trigger without explicit response from Jenkins.');
  });
});

view raw JSON →