How to build a job using the REST API and Java

Issue

I would like to be able to build a job remotely using the Jenkins REST API and Java. This provides a workaround for JENKINS-12543, that doesn’t require SSH Key Authentication.
 

Environment

  • CloudBees Jenkins Enterprise
  • Remote Access API
  • Java

Resolution

Example build:

import javax.xml.bind.DatatypeConverter;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {

  public static void main(String[] args) {
    try {
      URL url = new URL ("http://localhost:8080/job/test/build"); // Jenkins URL localhost:8080, job named 'test'
      String user = "developer"; // username
      String pass = "developer"; // password or API token
      String authStr = user +":"+  pass;
      String encoding = DatatypeConverter.printBase64Binary(authStr.getBytes("utf-8"));

      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setRequestMethod("POST");
      connection.setDoOutput(true);
      connection.setRequestProperty("Authorization", "Basic " + encoding);
      InputStream content = connection.getInputStream();
      BufferedReader in   =
          new BufferedReader (new InputStreamReader (content));
      String line;
      while ((line = in.readLine()) != null) {
        System.out.println(line);
      }
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}

Obviously, replace:

  • developer:developer with your username:password
  • localhost:8080 with your Jenkins URL
  • test with your job name

Example build with String parameter:

import javax.xml.bind.DatatypeConverter;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {

  public static void main(String[] args) {
    try {
      URL url = new URL ("http://localhost:8080/job/test/buildWithParameters"); // Jenkins URL localhost:8080, job named 'test'
      String user = "developer"; // username
      String pass = "developer"; // password or API token
      String authStr = user +":"+  pass;
      String encoding = DatatypeConverter.printBase64Binary(authStr.getBytes("utf-8"));

      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setRequestMethod("POST");
      connection.setDoOutput(true);
      connection.setRequestProperty("Authorization", "Basic " + encoding);

      String urlParams="paramA=123";
      byte[] postData = urlParams.getBytes("utf-8");
      try(DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
        wr.write(postData);
      }

      InputStream content = connection.getInputStream();
      BufferedReader in   =
          new BufferedReader (new InputStreamReader (content));
      String line;
      while ((line = in.readLine()) != null) {
        System.out.println(line);
      }
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}

Obviously, replace:

  • developer:developer with your username:password
  • localhost:8080 with your Jenkins URL
  • test with your job name
  • paramA with your parameter name
  • 123 with your parameter value

See Remote Access API for more.

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.