How to update job config files using the REST API and Java?

Article ID:226835367
2 minute readKnowledge base

Issue

I would like to be able to update job config files remotely using the Jenkins REST API and Java.

Resolution

Get current config:

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;

public class Main {

  private static final int BUFFER_SIZE = 4096;

  public static void main(String[] args) {
    try {
      URL url = new URL("https://localhost:8080/job/test/config.xml"); // 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.setDoOutput(true);
      connection.setRequestProperty("Authorization", "Basic " + encoding);

      Path testProjectConfigPath = Files.createTempFile("test", "config.xml"); // change to the path that makes sense in your case
      try (InputStream inputStream = connection.getInputStream();
           OutputStream outputStream = Files.newOutputStream(testProjectConfigPath)) {
        int bytesRead;
        byte[] buffer = new byte[BUFFER_SIZE];
        while ((bytesRead = inputStream.read(buffer)) != -1) {
          outputStream.write(buffer, 0, bytesRead);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Post updated config:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;

public class Main {

  private static final byte[] BUFFER = new byte[8192];

  public static void main(String[] args) {
    try {
      URL url = new URL("https://localhost:8080/job/test/config.xml"); // 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"));
      Path configPath = Paths.get("/path/to/config.xml") // A path to the modified project configuration file.

      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setReadTimeout(10000);
      connection.setConnectTimeout(15000);
      connection.setRequestMethod("POST");
      connection.setUseCaches(false);
      connection.setDoInput(true);
      connection.setDoOutput(true);
      connection.setRequestProperty("Authorization", "Basic " + encoding);

      try (InputStream inputStream = Files.newInputStream(configPath);
           DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
        int c;
        while ((c = inputStream.read(BUFFER, 0, BUFFER.length)) > 0) {
          wr.write(BUFFER, 0, c);
        }
      }

      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

See Remote Access API for more.