Sunday, September 10, 2017

Jenkins: Trigger build remotely via script using crumb

Python:

import requests
api_proto = 'http'
api_url = 'localhost'
api_port = '8080'
api_job = 'test01'
api_user = 'admin'
api_pass = 'password'

api_crumb = requests.get(api_proto + '://' + api_user + ':' + api_pass + '@' + api_url + ':' + api_port + '/crumbIssuer/api/json')
if api_crumb.status_code == 200 :
    api_crumb = api_crumb.json()['crumb']

resp = requests.post(api_proto + '://' + api_url + ':' + api_port + '/job/' + api_job + '/build', auth=(api_user, api_pass), headers={"Jenkins-Crumb":api_crumb})
if resp.status_code == 201:
    print(api_job + ' was triggered successfuly..')



Powershell:

$API_User = "admin"
$API_Pass = "password"
$API_URL = "localhost"
$API_Port = "8080"
$API_Job = "test01"

$h = @{}
$h.Add('Authorization', 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$(${API_User}):$(${API_Pass})")))

$Params = @{uri = "http://${API_URL}:${API_Port}/crumbIssuer/api/json";
            Method = 'Get';
            Headers = $h;}
$API_Crumb = Invoke-RestMethod @Params

$h.Add('Jenkins-Crumb', $API_Crumb.crumb)
$Params['uri'] = "http://${API_URL}:${API_Port}/job/${API_Job}/build"
$Params['Method'] = 'Post'
$Params['Headers'] = $h

Invoke-RestMethod @Params



Perl:

use strict;
use warnings;
use autodie;
use REST::Client;
use MIME::Base64;
use JSON;

my $api_proto = 'http';            #HTTP/HTTPS
my $api_url = 'localhost';        #Server IP
my $api_port = '8080';            #Port number
my $api_job = 'test01';            #Jenkins job name
my $api_user = 'admin';            #Jenkins account username
my $api_pass = 'password';        #Jenkins account password

my $client = REST::Client->new();
$client->addHeader('Authorization', 'Basic '.encode_base64($api_user . ':' . $api_pass));
$client->GET($api_proto.'://'.$api_url.':'.$api_port.'/crumbIssuer/api/json');

if($client->responseCode() == 200)
{
    my $data = decode_json($client->responseContent());
    $client->addHeader('Jenkins-Crumb', $data->{'crumb'});
    $client->POST($api_proto.'://'.$api_url.':'.$api_port.'/job/'.$api_job.'/build');
    print("$api_job was triggered successfully..\n");
}



PHP:

<?php

$api_url = 'localhost';
$api_port = '8080';
$api_user = 'admin';
$api_pass = 'password';
$api_job = 'test01';

$opts = array(
  'http' => array(
    'method' => 'GET'
  )
);
$context = stream_context_create($opts);
$domain = 'http://'.$api_user.':'.$api_pass.'@'.$api_url.':'.$api_port.'/crumbIssuer/api/json';
$file = file_get_contents($domain, false, $context);

if(!$file) {
    echo 'Something went wrong..Try again!';
} else {
    $file = json_decode($file);
}

$opts = array(
  'http' => array(
    'method' => 'POST',
    'header' => 'Jenkins-Crumb:'.$file->{'crumb'}
  )
);
$context = stream_context_create($opts);
$domain = 'http://'.$api_user.':'.$api_pass.'@'.$api_url.':'.$api_port.'/job/'.$api_job.'/build';
$handle = fopen($domain, 'r', false, $context);

if($handle) {
    echo $api_job.' was triggered successfully...';
}

fclose($handle);

?>



Curl/Wget: https://support.cloudbees.com/hc/en-us/articles/219257077-CSRF-Protection-Explained

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.