Showing posts with label hello world example. Show all posts
Showing posts with label hello world example. Show all posts

Thursday, May 19, 2016

Invoking Jenkins Job using Jersey Client in Java with Username / Password authentication

General

The following post is document how to invoke a job using Jersery client in Java on Jenkins

Assumptions

  • You are familiar with Jenkins and have permissions to create a new job
  • I've used Jenkins version is 1.651.1 ( I'm guessing it will work on previous versions but I did not check)
  • You are familiar with Maven
  • You have IntelliJ ( I used Community Edition 2016.1)

Setup

Jenkins

Creating the job

  • Connect your Jenknis
  • Click on New Item
  • In Item name type 'hello_world'
  • Select 'Freestyle project'
  • Press ok

Configuring the job

  • Click on the job
  • Press on the configure link from the left
  • Check the 'This build is parameterized'
  • Click on 'Add Parameter'
  • Choose String Parameter
  • In the name type 'firstname' (Leave the Default value and description empty)
  • Scroll down to the Build section
  • Click on  'Add build step' and choose Execute shell
  • type 'echo ${firstname}'

Test the job

  • Click on the 'Build with Parameters'
  • Fill in a name click on the build number (should be #1) in case this job is new
  • Click on Console Output
  • You should see something like this
+ echo checking
checking

Java

Creating the project

  • Open IntelliJ
  • Click on 'Create New Project'
  • Choose maven and click next
  • GroupId: com.tzach.jenkins
  • ArtifactId: jerseyclient
  • Project name: jersey_client_jenkins
  • Open the pom.xml file
  • Add the following
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>2.22.2</version>
        </dependency>
    </dependencies>

  • Click on Enable auto import
  • Click on Maven from the right and choose compile
  • On the bottom you should see 'Process finished with exit code 0'

Now let's write some code

  • Create a new class named JenkinsJerseyClient
  • Create the following method: Response invoke(String username, String password, String firstname)
  • Paste the following
String target = "http://myjenkins:8080/job/hello_world/buildWithParameters";

HttpAuthenticationFeature httpAuthenticationFeature = HttpAuthenticationFeature.basicBuilder()
                .credentials(username, password)
                .build();

ClientConfig clientConfig = new ClientConfig();
clientConfig.register(httpAuthenticationFeature);

Client client = ClientBuilder.newClient(clientConfig);

WebTarget webTarget = client.target(target);
// Parameters needed for the job
Form form = new Form();
form.param("firstname", firstname);

// Sending invoke as POST
return webTarget.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));

  • Replace the URL of jenkins to your jenkins
  • Create a main method, and paste the following:
 System.out.println(new JenkinsJerseyClient().invoke("tzachs","tzachs_password","tzach").getStatus());

  • Replace 'tzachs' with your username
  • Replace 'tzachs_password' with your password
  • Replace 'tzach' with the parameters you need
  • Full code looks like this:

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 * Created by tzachs on 5/19/2016.
 */
public class JenkinsJerseyClient {

    public Response invoke(String username, String password, String firstname){
        String target = "http://myjenkins:8080/job/hello_world/buildWithParameters";

        HttpAuthenticationFeature httpAuthenticationFeature = HttpAuthenticationFeature.basicBuilder()
                .credentials(username, password)
                .build();

        ClientConfig clientConfig = new ClientConfig();
        clientConfig.register(httpAuthenticationFeature);

        Client client = ClientBuilder.newClient(clientConfig);

        WebTarget webTarget = client.target(target);
        Form form = new Form();
        form.param("firstname", firstname);

        return webTarget.request(MediaType.APPLICATION_JSON_TYPE)
                .post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
    }

    public static void main(String[] args) {
        System.out.println(new JenkinsJerseyClient().invoke("tzachs","tzachs_password","tzach").getStatus());
    }
}

Run

  • Run the main, you should see in the console log 201. If you see it, it's working. Go to jeknins see you have a new build number