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

Issue

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

Environment

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("http://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("http://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.

Have more questions? Submit a request

4 Comments

  • 0
    Avatar
    Anant Patil

    Hi,

    I have used the above code and update my config file of specific job but its not replicating the changed config file in under that job?Please let me know is the above code working fine?Thanks

  • 0
    Avatar
    Denys Digtiar

    Hi Anant,

    Not sure what you mean by "... but its not replicating the changed config file in under that job". 

    The code should have been tested at the moment of writing. Given the backward compatibility of Java, it should still work as it was.

  • 0
    Avatar
    Anant Patil

    Hi Denys,

    Thank you for reply,

    Please consider this scenario,I have declared Global string parameter(GIT_PATH) for the job 'ABC',I am trying to update the value of GIT_PATH through the above code but its not reflecting that changes in server job 'ABC' config file.

     

    Edited by Anant Patil
  • 0
    Avatar
    Anant Patil

    Hi Denys,

    I have got it ,Thank you.

Please sign in to leave a comment.