Simple tutorial on using curl to operate hudson/Jenkins on a command line

Source: Internet
Author: User
Tags curl

Recently, the project team wanted to develop an Auto UT tool based on hudson (now renamed jenkins, but I still like hudson, you can use command line commands to operate UT jobs on hudson. I would like to learn from the hudson perl package developed by the UK project team, but its internal implementation is too complicated and we can't use many functions, so I can only think about it myself. Later.

Curl is an open source file transmission tool that uses the URL syntax in the command line mode. It is widely used in Unix and multiple Linux distributions, and has DOS, Win32, and Win64 versions. I will use the content of Bash Job Clone script in combination with my own practices.

The code is as follows: Copy code

CURL = "curl -- user $ USERNAME: $ PASSWORD-s"



-U/-- user

Set user name and password

-S/-- silent

Quiet mode. The progress table or error message is not displayed.

The code is as follows: Copy code
# Check for existing job
'$ CURL-f-o/dev/null $ TO_CONFIG_URL'
If [$? -Eq 0]; then
Echo "WARN: $ TO_PROJECT already exists on the hudson server. No action taken ."
Continue
Fi



-F/-- fail

(HTTP) shield the error message when a server error occurs (no output ). In this way, we can better support scripts and other attempts to better handle failures. Under normal circumstances, when an HTTP server cannot provide a document, it will return an HTML file that explains the cause. This flag prevents curl from outputting this document and returns error 22.

The code is as follows: Copy code

# Download the config. xml for the job, continue if it doesn't exist
'$ CURL-o $ FROM_CONFIG_XML $ FROM_CONFIG_URL'
Grep "Error 404" "$ FROM_CONFIG_XML">/dev/null
If [$? -Eq 0]; then
Echo "FAIL: $ FROM_CONFIG_URL is not a valid project url"
Continue
Fi


-O/-- output <FILE>

Output the obtained remote file to the file instead of to the screen (stdout ). If you use {} or [] to define batch file acquisition, you can use # in the <FILE> option to follow the digit format to define the file name. In this way, the # in the actual file name will be replaced by the current string in multiple URLs. For example:

Curl http: // {one, two} .site.com-o "file_00001.txt", The file_one and file_two files are output.

Or use multiple variables such:

Curl http: // {site, host }. host [1-5]. com-o "#1 _ #2" #1 will be replaced by "site" or "host", and #2 will be replaced by numbers between 1 and 5

You can use this option as many URLs as you process.

The code is as follows: Copy code

# Create a new job based on the existing job
'$ CURL-o/dev/null -- data "" $ HUDSON_URL/createItem? Name =$ {TO_PROJECT} & mode = copy & from =$ {FROM_PROJECT }"'
  
# Upload new job data
'$ CURL-o/dev/null -- data "@ $ TO_CONFIG_XML" "$ HUDSON_URL/job/$ TO_PROJECT/config. xml "'
  
# Enable the job, there seems to be a bug where we need to first disable and then enable
'$ CURL-o/dev/null -- data disable "$ HUDSON_URL/job/$ TO_PROJECT/disable "'
'$ CURL-o/dev/null -- data enable "$ HUDSON_URL/job/$ TO_PROJECT/enable "'



-D/-- data <data>

(HTTP) POST specific data to the server, which is the same as entering a form in the browser and pressing the submit button.

If your data starts with @, the rest should be a file name used to read data, or you want cURL to read data from standard input (stdin. The content of this file must be URL encoded. You can also specify multiple files. For example, to post the file foobar, the format is -- data @ foobar.


Well, the usage of curl is enough for my needs. At this time, someone should ask, like $ HUDSON_URL/job/$ TO_PROJECT/disable, how do I get this URL string? Here we need to use firebug to obtain.

In the case of create new job, how can I obtain form items.

First, we manually create a job on hudson, New Job --> enter Job name --> select Copy existing job, click OK, and observe firebug

 



Based on the above information, we can see how the browser submits the form, so curl should be written like this

The code is as follows: Copy code
Curl-u username: passwd-s-o/dev/null-d createItem-d name = ACEFEI-d mode = copy-d from = CloneJob "http: // 135.251.123.221: 8080/view/All/createItem"



Where

-D createItem is obtained based on POST createItem in the URL of firebug.

-D name = ACEFEI-d mode = copy-d from = CloneJob according to the json {"name": "ACEFEI", "mode": "copy ", "from": "CloneJob.

The final URL is based on the Reload the page to get source for: http: // 135.251.123.221 in the response options: 8080/view/All/createItem (or move the cursor to POST createItem to display the URL ).

Similarly, we can also write delete jobs and other operations.

The code is as follows: Copy code
Curl-u user: passwd-s-o/dev/null-d doDelete "http: // 135.86.206.146: 8080/job/acefei/doDelete"

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.