background

Every time to the web page manual version a little annoying, write a script to improve the efficiency of development.

CFG

Get the API TOKEN in Jenkins Settings. To pass authentication, combine the host with the account password.

const token = {
  user: ' '.password: ' ',}const host = 'jenkins.xxx.cn';
const basicUrl = `http://${token.user}:${token.password}@${host}`;
Copy the code

BUILD

Call the jenkins.job.build() method provided by Jenkins.

// crumbIssuer The default value is false, true Enables CSRF protection
const jenkins = require('jenkins')({ baseUrl, crumbIssuer: true });

const job_name = ' ';  // The name of the job you want to publish

jenkins.job.build(
  {
    name: job_name,
    parameters: {
      name: 'value'.// Enter some parameter information}},function(err, data) {
    if (err) throw err;

    console.log('queue item number', data);
});
Copy the code

View release status

After executing build we want to see the status of the current build, whether it was successful, failed or in progress.

Jenkins.job. Get () gets information about the current job. Data.lastbuild. number gets the number of the lastBuild, which is the build we just triggered. Jenkins.build.get () gets information about the current build. Data. result Retrieves the result of the current build

jenkins.job.get(job_name, (err, data) => {
  if (err) throw err;

  const lastBuildNumber = data.lastBuild.number;
  console.log('last build number', lastBuildNumber);

  jenkins.build.get(job_name, lastBuildNumber, (err, data) => {
    if (err) throw err;
    console.log('last build result', data.result); })});Copy the code

data

jenkins