티스토리 뷰

Remote Access API

 

Remote Access API

Jenkins – an open source automation server which enables developers around the world to reliably build, test, and deploy their software

www.jenkins.io

  • Jenkins는 rest api를 사용하여 job생성, job 정보 조회, build, 빌드 완료 내역, 마지막 빌드 내역등을 얻을 수 가 있다.
  • Rest api를 사용하기 위해서는 젠킨스에 옵션을 변경해 줘야한다. (403 에러가 발생)
    • 아래 configure global security > Prevent Corss Site Request Forgery exploits 를 해제 해준다
    • Curl로 api 호출시 사용자 정보가 포함되기 때문에 api token을 발급받아 사용하는 것이 좋다
      • 계정 설정 (사용자 계정) > API Token을 사용하면 된다.
    • Python에서도 jenkinsapi를 사용할수 있다.
      • Pip install jenkinsapi
    • API 호출
      • json으로 데이터를 받아올 때 pretty=true 설정을 주면 tree 모양으로 리턴을 받는다 (json?pretty=true)
      • Build 실행 : curl -X POST http://ip:port/jenkins/job/job이름/build --user 아이디:API토근
      • Job 정보 조회 : curl -X POST http://ip:port/jenkins/job/job 이름/api/json --user 아이디:API토근
      • 마지막 빌드 내용확인 : curl -X POST http://ip:port/jenkins/job/job 이름/ lastStableBuild/api/json--user 아이디:API토근
    • Api 로 리턴받은 json 데이터는 서버에서 jq 라는 것을 이용해서 내용을 쉽게 확인할 수 있다.
    • bash에서 json 작업(https://taetaetae.github.io/2017/02/28/shell-script-json/)
wget http://stedolan.github.io/jq/download/linux64/jq

chmod +x ./jqcp jq 

/usr/bin

cat 파일명 | jq '.name' 식으로 사용 "a":[ { } , { } ] 형식의 경우 a[0] 배열로 주면된다**

Remote API [Jenkins]

 

Remote API [Jenkins]

REST API Many objects of Jenkins provide the remote access API. They are available at /.../api/ where "..." portion is the object for which you'd like to access. XML API Access data exposed in HTML as XML for machine consumption. Schema is also available.

ci.jenkins.io

Java API

cdancy/jenkins-rest

 

cdancy/jenkins-rest

Java client, built on top of jclouds, for working with Jenkins REST API - cdancy/jenkins-rest

github.com

<dependency>
  <groupId>com.cdancy</groupId>
  <artifactId>jenkins-rest</artifactId>
  <version>X.Y.Z</version>
  <classifier>sources|tests|javadoc|all</classifier> (Optional)
</dependency>

Job

Info

  • GET
http://{jenkins url}/job/{JOBNAME}/api/json
  • response
    • Status : 200 OK

Create

- Job 생성은 config.xml 파일을 이용해서 요청 배치를 구분해서 base job config 파일을 만들어서 활용

- job 생성 API 호출

  • POST
  • paramter
    • name
  • body
    • binary
      • xml file path
  • Content-Type : text/xml
http://{jenkins url}/jenkins/createItem?name={JOBNAME}
  • respons
  • Status : 200 OK

- Status : 400 Bad Request

 

 

...
<div id="page-body" class="clear">
  <div id="side-panel"></div>
    <div id="main-panel"><a name="skip2content"></a>
    <h1>Error</h1>
    <p>A job already exists with the name ‘test project’
  </div>
</div>
...

 

- Status : 500 Server Error... A problem occurred while processing the request ...

...
A problem occurred while processing the request
...

Build - Start

  • POST
http://{jenkins url}/jenkins/job/{JOBNAME}/build

curl JENKINS_URL/job/JOB_NAME/buildWithParameters \
  --user USER:TOKEN \
  --data id=123 --data verbosity=high

#파일 경로 - 절대
curl JENKINS_URL/job/JOB_NAME/buildWithParameters \
  --user USER:PASSWORD \
  --form FILE_LOCATION_AS_SET_IN_JENKINS=@PATH_TO_FILE
  • response
    • status : 201 Created

Build - Result

  • POST
#빌드결과
http://{jenkins url}/jenkins/job/{JOBNAME}/{build number}/api/json

#마지막 성공 빌드
http://{jenkins url}/jenkins/job/{JOBNAME}/lastStableBuild/api/json
  • response
    • status : 200 OK

 

'Programming > Jenkins' 카테고리의 다른 글

[Jenkins] Jenkins 설치 (Mac & WSL2)  (0) 2021.05.09
[Jenkins] Jenkins?  (0) 2021.05.09
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함