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

0 Comments

Please sign in to leave a comment.