Issue
I would like to be able to update job config files remotely using the Jenkins REST API and Java.
Environment
-
CloudBees CI (CloudBees Core) on modern cloud platforms - Managed controller
-
CloudBees CI (CloudBees Core) on modern cloud platforms - Operations Center
-
CloudBees CI (CloudBees Core) on traditional platforms - Client controller
-
CloudBees CI (CloudBees Core) on traditional platforms - Operations Center
-
CloudBees Jenkins Enterprise - Managed controller
-
CloudBees Jenkins Enterprise - Operations center
-
Remote Access API
-
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.