How to build a job using the REST API and Java?

Article ID:226852648
2 minute readKnowledge base

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.

Resolution

Example build:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

public class Main {
  public static void main(String[] args) {
    try {
      URL url = new URL("https://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 = Base64.getEncoder().encodeToString(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 java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

public class Main {
  public static void main(String[] args) {
    try {
      URL url = new URL("https://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 = Base64.getEncoder().encodeToString(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.