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

2 Comments

  • 0
    Avatar
    Bhaskar Polipilli

    Hi,

    I have tried the above solution and am getting the below exception

    java.io.IOException: Server returned HTTP response code: 403 for URL: http://localhost:8080/job/samplejob/build
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at com.itaf.majesco.jenkinsclient.JenkinsConnection.main(JenkinsConnection.java:25)

    I have installed jenkins on my machine, created a sample job "samplejob" and would like to build this job programatically.

    Below is the source which I am trying to execute:

    package com.sample.jenkinsclient;

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

    import javax.xml.bind.DatatypeConverter;

    public class JenkinsConnection {

    public static void main(String[] args) {
    try {
    URL url = new URL ("http://localhost:8080/job/samplejob/build"); // Jenkins URL localhost:8080, job named 'test'
    String user = "admin"; // username
    String pass = "d546aece82de38a55c2f84bcdeb7a334"; // 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();
    }

    }

    }

     

     

    Any help would be appreciated !!!

  • 0
    Avatar
    Denys Digtiar

    Hi Bhaskar Polipilli,

    The HTTP response code is "Forbidden". Either Authentication or Authorization failed, i.e. either credentials are not right or the user is not permitted to build the "samolejob". You need to figure out which is the case and make appropriate adjustments.

Please sign in to leave a comment.