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

  • CloudBees Jenkins Enterprise
  • Remote Access API
  • Java

Resolution

Get current config:

public class Main {

  private static final int BUFFER_SIZE = 4096;
  
  public static void main(String[] args) {
    try {
      String saveDir = "/MY/SAVE/PATH"; // download directory
      String fileName = "config.xml";
      URL url = new URL("http://localhost:8080/job/test/" + fileName); // 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.setDoOutput(true);
      connection.setRequestProperty("Authorization", "Basic " + encoding);

      InputStream inputStream = connection.getInputStream();
      String saveFilePath = saveDir + File.separator + fileName;

      FileOutputStream outputStream = new FileOutputStream(saveFilePath);

      int bytesRead;
      byte[] buffer = new byte[BUFFER_SIZE];
      while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
      }

      outputStream.close();
      inputStream.close();

    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}

Post updated config:

import javax.xml.bind.DatatypeConverter;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {

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

  public static void main(String[] args) {
    try {
      String fileDir = "/MY/CONFIG/PATH"; // upload directory
      String fileName = "config.xml";
      URL url = new URL("http://localhost:8080/job/test/" + fileName); // 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.setReadTimeout(10000);
      connection.setConnectTimeout(15000);
      connection.setRequestMethod("POST");
      connection.setUseCaches(false);
      connection.setDoInput(true);
      connection.setDoOutput(true);
      connection.setRequestProperty("Authorization", "Basic " + encoding);

      String filePath = fileDir + File.separator + fileName;
      FileInputStream inputStream = new FileInputStream(new File(filePath));

      try(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);
      }

      inputStream.close();

    } 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.