티스토리 뷰

728x90
SMALL

Jenkins는 Remote 환경에서 Jenkins의 실행 및 상태를 확인할 수 있는 API를 제공한다.

이러한 API를 통해 Jenkins를 관리하는 별도의 관리 툴을 개발하거나, 다양한 환경에 분배되어 있는 Jenkins를 통합하여 관리할 수도 있다.

본 포스팅에서는 이와 같은 Jenkins API 활용 방법에 대해 알아보자.

Jenkins API 호출

1) User API Token 생성

Jenkins 관리 Manage Users User[admin] 설정 API Token Add new Token → Generate 버튼 클릭

위와 같이 생성된 API Token을 복사한다.

2) Job Build

- Parameter가 없는 build

curl -X POST http://192.168.56.100:8080/job/Kubepipe/build -- user admin:117f4a43ec23853dcda53e85e9d755f3ed -v

parameter가 없는 build의 경우 curl -X POST http://[IP]:[PORT]/job/[JOB_NAME]/build --user [USER_NAME]:[USER_API_TOKEN] 형태로 해당 Job을 실행 시킬 수 있다.

- Build with Parameter

curl -X POST http://192.168.56.100:8080/job/Kubepipe/buildWithParameters --data tag=latest --data buildtag=v1 --data GIT_URL=http://192.168.56.101/root/springboot.git --user admin:117f4a43ec23853dcda53e85e9d755f3ed -v

parameter가 있는 build의 경우 curl -X POST http://[IP]:[PORT]/job/[JOB_NAME]/build buildWithParameters --data [PARAM1]=[VALUE1] --data [PARAM2]=[VALUE2] --user [USER_NAME]:[USER_API_TOKEN] 형태로 해당 Job을 실행 시킬 수 있다.

[root@ciserver jenkins]# curl -X POST http://192.168.56.100:8080/job/Kubepipe/buildWithParameters --data tag=latest --data buildtag=v1 --data GIT_URL=http://192.168.56.101/root/springboot.git --user admin:117f4a43ec23853dcda53e85e9d755f3ed -v
* About to connect() to 192.168.56.100 port 8080 (#0)
*   Trying 192.168.56.100...
* Connected to 192.168.56.100 (192.168.56.100) port 8080 (#0)
* Server auth using Basic with user 'admin'
> POST /job/Kubepipe/buildWithParameters HTTP/1.1
> Authorization: Basic YWRtaW46MTE3ZjRhNDNlYzIzODUzZGNkYTUzZTg1ZTlkNzU1ZjNlZA==
> User-Agent: curl/7.29.0
> Host: 192.168.56.100:8080
> Accept: */*
> Content-Length: 72
> Content-Type: application/x-www-form-urlencoded
> 
* upload completely sent off: 72 out of 72 bytes
< HTTP/1.1 201 Created
< Date: Fri, 07 Aug 2020 14:53:56 GMT
< X-Content-Type-Options: nosniff
< Location: http://192.168.56.100:8080/queue/item/1/
< Content-Length: 0
< Server: Jetty(9.4.22.v20191022)
< 
* Connection #0 to host 192.168.56.100 left intact
[root@ciserver jenkins]#

위와 같이 빌드가 자동으로 실행되는 것을 확인할 수 있다.

728x90

3) Job build 조회

curl -X GET curl -X GET http://192.168.56.100:8080/job/Kubepipe/api/json --user admin:117f4a43ec23853dcda53e85e9d755f3ed -v

Job 빌드 조회는 curl -X GET http://[IP]:[PORT]/job/[JOB_NAME]/api/json --user [USER_NAME]:[USER_API_TOKEN] 형태로 조회할 수 있다.

[root@ciserver jenkins]# curl -X GET http://192.168.56.100:8080/job/Kubepipe/api/json --user admin:117f4a43ec23853dcda53e85e9d755f3ed -v
* About to connect() to 192.168.56.100 port 8080 (#0)
*   Trying 192.168.56.100...
* Connected to 192.168.56.100 (192.168.56.100) port 8080 (#0)
* Server auth using Basic with user 'admin'
> GET /job/Kubepipe/api/json HTTP/1.1
> Authorization: Basic YWRtaW46MTE3ZjRhNDNlYzIzODUzZGNkYTUzZTg1ZTlkNzU1ZjNlZA==
> User-Agent: curl/7.29.0
> Host: 192.168.56.100:8080
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Fri, 07 Aug 2020 15:06:27 GMT
< X-Content-Type-Options: nosniff
< X-Jenkins: 2.208
< X-Jenkins-Session: 3228d2d6
< Content-Type: application/json;charset=utf-8
< Content-Length: 7229
< Server: Jetty(9.4.22.v20191022)
< 
{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowJob","actions":[{},{},{},{},{},{},{},{},{},{"_class":"com.cloudbees.plugins.credentials.ViewCredentialsAction"}],"description":"","displayName":"Kubepipe","displayNameOrNull":null,"fullDisplayName":"Kubepipe","fullName":"Kubepipe","name":"Kubepipe","url":"http://192.168.56.100:8080/job/Kubepipe/","buildable":true,"builds":[{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":35,"url":"http://192.168.56.100:8080/job/Kubepipe/35/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":34,"url":"http://192.168.56.100:8080/job/Kubepipe/34/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":33,"url":"http://192.168.56.100:8080/job/Kubepipe/33/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":32,"url":"http://192.168.56.100:8080/job/Kubepipe/32/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":31,"url":"http://192.168.56.100:8080/job/Kubepipe/31/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":30,"url":"http://192.168.56.100:8080/job/Kubepipe/30/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":29,"url":"http://192.168.56.100:8080/job/Kubepipe/29/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":28,"url":"http://192.168.56.100:8080/job/Kubepipe/28/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":27,"url":"http://192.168.56.100:8080/job/Kubepipe/27/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":26,"url":"http://192.168.56.100:8080/job/Kubepipe/26/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":25,"url":"http://192.168.56.100:8080/job/Kubepipe/25/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":24,"url":"http://192.168.56.100:8080/job/Kubepipe/24/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":23,"url":"http://192.168.56.100:8080/job/Kubepipe/23/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":22,"url":"http://192.168.56.100:8080/job/Kubepipe/22/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":21,"url":"http://192.168.56.100:8080/job/Kubepipe/21/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":20,"url":"http://192.168.56.100:8080/job/Kubepipe/20/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":19,"url":"http://192.168.56.100:8080/job/Kubepipe/19/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":18,"url":"http://192.168.56.100:8080/job/Kubepipe/18/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":17,"url":"http://192.168.56.100:8080/job/Kubepipe/17/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":16,"url":"http://192.168.56.100:8080/job/Kubepipe/16/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":15,"url":"http://192.168.56.100:8080/job/Kubepipe/15/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":14,"url":"http://192.168.56.100:8080/job/Kubepipe/14/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":13,"url":"http://192.168.56.100:8080/job/Kubepipe/13/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":12,"url":"http://192.168.56.100:8080/job/Kubepipe/12/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":11,"url":"http://192.168.56.100:8080/job/Kubepipe/11/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":10,"url":"http://192.168.56.100:8080/job/Kubepipe/10/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":9,"url":"http://192.168.56.100:8080/job/Kubepipe/9/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":8,"url":"http://192.168.56.100:8080/job/Kubepipe/8/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":7,"url":"http://192.168.56.100:8080/job/Kubepipe/7/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":6,"url":"http://192.168.56.100:8080/job/Kubepipe/6/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":5,"url":"http://192.168.56.100:8080/job/Kubepipe/5/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":4,"url":"http://192.168.56.100:8080/job/Kubepipe/4/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":3,"url":"http://192.168.56.100:8080/job/Kubepipe/3/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":2,"url":"http://192.168.56.100:8080/job/Kubepipe/2/"},{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":1,"url":"http://192.168.56.100:8080/job/Kubepipe/1/"}],"color":"red","firstBuild":{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":1,"url":"http://192.168.56.100:8080/job/Kubepipe/1/"},"healthReport":[{"description":"Build stability: 1 out of the last 5 builds failed.","iconClassName":"icon-health-60to79","iconUrl":"health-60to79.png","score":80}],"inQueue":false,"keepDependencies":false,"lastBuild":{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":35,"url":"http://192.168.56.100:8080/job/Kubepipe/35/"},"lastCompletedBuild":{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":35,"url":"http://192.168.56.100:8080/job/Kubepipe/35/"},"lastFailedBuild":{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":35,"url":"http://192.168.56.100:8080/job/Kubepipe/35/"},"lastStableBuild":{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":34,"url":"http://192.168.56.100:8080/job/Kubepipe/34/"},"lastSuccessfulBuild":{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":34,"url":"http://192.168.56.100:8080/job/Kubepipe/34/"},"lastUnstableBuild":null,"lastUnsuccessfulBuild":{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","number":35,"url":"http://192.168.56.100:8080/job/Kubepipe/35/"},"nextBuildNumber":36,"property":[{"_class":"com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty"},{"_class":"com.sonyericsson.rebuild.RebuildSettings"},{"_class":"hudson.model.ParametersDefinitionProperty","parameterDefinitions":[{"_class":"hudson.model.StringParameterDefinition","defaultParameterValue":{"_class":"hudson.model.StringParameterValue","name":"tag","value":"latest"},"description":"tag {YYYYMMDD}{HHMMSS}","name":"tag","type":"StringParameterDefinition"},{"_class":"hudson.model.StringParameterDefinition","defaultParameterValue":{"_class":"hudson.model.StringParameterValue","name":"buildtag","value":"v1"},"description":"tag {YYYYMMDD}{HHMMSS}","name":"buildtag","type":"StringParameterDefinition"},{"_class":"hudson.model.StringParameterDefinition","defaultParameterValue":{"_class":"hudson.model.StringParameterValue","name":"GIT_URL","value":"http://192.168.56.101/root/springboot.git"},"description":"GIT_URL","name":"GIT_URL","type":"StringParameterDefinition"},{"_class":"hudson.model.BooleanParameterDefinition","defaultParameterValue":{"_class":"hudson.model.BooleanParameterValue","name":"VERBOSE","value":false},"description":"","name":"VERBOSE","type":"BooleanParameterDefinition"}]}],"* Connection #0 to host 192.168.56.100 left intact
queueItem":null,"concurrentBuild":true,"resumeBlocked":false}[root@ciserver jenkins]# 

 

4) Job Build 결과 조회

curl -X GET http://192.168.56.100:8080/job/Kubepipe/35/api/json --user admin:117f4a43ec23853dcda53e85e9d755f3ed -v

Job Build 결과를 확인하기 위해서는 curl -X GET http://[IP]:[PORT]/job/[JOB_NAME]/[BUILD_NUM]/api/json --user [USER_NAME]:[USER_API_TOKEN] 형태로 조회할 수 있다.

[root@ciserver jenkins]# curl -X GET http://192.168.56.100:8080/job/Kubepipe/35/api/json --user admin:117f4a43ec23853dcda53e85e9d755f3ed -v
* About to connect() to 192.168.56.100 port 8080 (#0)
*   Trying 192.168.56.100...
* Connected to 192.168.56.100 (192.168.56.100) port 8080 (#0)
* Server auth using Basic with user 'admin'
> GET /job/Kubepipe/35/api/json HTTP/1.1
> Authorization: Basic YWRtaW46MTE3ZjRhNDNlYzIzODUzZGNkYTUzZTg1ZTlkNzU1ZjNlZA==
> User-Agent: curl/7.29.0
> Host: 192.168.56.100:8080
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Fri, 07 Aug 2020 15:11:24 GMT
< X-Content-Type-Options: nosniff
< X-Jenkins: 2.208
< X-Jenkins-Session: 3228d2d6
< Content-Type: application/json;charset=utf-8
< Content-Length: 1359
< Server: Jetty(9.4.22.v20191022)
< 
{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","actions":[{"_class":"hudson.model.ParametersAction","parameters":[{"_class":"hudson.model.StringParameterValue","name":"tag","value":"latest"},{"_class":"hudson.model.StringParameterValue","name":"buildtag","value":"v1"},{"_class":"hudson.model.StringParameterValue","name":"GIT_URL","value":"http://192.168.56.101/root/springboot.git"},{"_class":"hudson.model.BooleanParameterValue","name":"VERBOSE","value":false}]},{"_class":"hudson.model.CauseAction","causes":[{"_class":"hudson.model.Cause$UserIdCause","shortDescription":"Started by user son.nara","userId":"admin","userName":"son.nara"}]},{},{},{"_class":"org.jenkinsci.plugins.workflow.cps.EnvActionImpl"},{},{},{},{},{"_class":"org.jenkinsci.plugins.pipeline.modeldefinition.actions.RestartDeclarativePipelineAction"},{},{"_class":"org.jenkinsci.plugins.workflow.job.views.FlowGraphAction"},{},{},{}],"artifacts":[],"building":false,"description":null,"displayName":"#35","duration":26198,"estimatedDurati* Connection #0 to host 192.168.56.100 left intact
on":17208,"executor":null,"fullDisplayName":"Kubepipe #35","id":"35","keepLog":false,"number":35,"queueId":1,"result":"FAILURE","timestamp":1596812043673,"url":"http://192.168.56.100:8080/job/Kubepipe/35/","changeSets":[],"culprits":[],"nextBuild":null,"previousBuild":{"number":34,"url":"http://192.168.56.100:8080/job/Kubepipe/34/"}}[root@ciserver jenkins]# 

빌드 넘버를 확인하기 어려울 경우

[root@ciserver jenkins]# curl -X GET http://192.168.56.100:8080/job/Kubepipe/lastStableBuild/api/json --user admin:117f4a43ec23853dcda53e85e9d755f3ed -v
* About to connect() to 192.168.56.100 port 8080 (#0)
*   Trying 192.168.56.100...
* Connected to 192.168.56.100 (192.168.56.100) port 8080 (#0)
* Server auth using Basic with user 'admin'
> GET /job/Kubepipe/lastStableBuild/api/json HTTP/1.1
> Authorization: Basic YWRtaW46MTE3ZjRhNDNlYzIzODUzZGNkYTUzZTg1ZTlkNzU1ZjNlZA==
> User-Agent: curl/7.29.0
> Host: 192.168.56.100:8080
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Fri, 07 Aug 2020 15:13:34 GMT
< X-Content-Type-Options: nosniff
< X-Jenkins: 2.208
< X-Jenkins-Session: 3228d2d6
< Content-Type: application/json;charset=utf-8
< Content-Length: 2209
< Server: Jetty(9.4.22.v20191022)
< 
{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","actions":[{"_class":"hudson.model.ParametersAction","parameters":[{"_class":"hudson.model.StringParameterValue","name":"tag","value":"latest"},{"_class":"hudson.model.StringParameterValue","name":"buildtag","value":"v1"},{"_class":"hudson.model.StringParameterValue","name":"GIT_URL","value":"http://192.168.56.101/root/springboot.git"},{"_class":"hudson.model.BooleanParameterValue","name":"VERBOSE","value":false}]},{"_class":"hudson.model.CauseAction","causes":[{"_class":"hudson.model.Cause$UserIdCause","shortDescription":"Started by user son.nara","userId":"admin","userName":"son.nara"}]},{},{},{"_class":"org.jenkinsci.plugins.workflow.cps.EnvActionImpl"},{"_class":"hudson.plugins.git.util.BuildData","buildsByBranchName":{"refs/remotes/origin/master":{"_class":"hudson.plugins.git.util.Build","buildNumber":34,"buildResult":null,"marked":{"SHA1":"836350a52b3a7727b7b877a6912d078bec98bcd3","branch":[{"SHA1":"836350a52b3a7727b7b877a6912d078bec98bcd3","name":"refs/remotes/origin/master"}]},"revision":{"SHA1":"836350a52b3a7727b7b877a6912d078bec98bcd3","branch":[{"SHA1":"836350a52b3a7727b7b877a6912d078bec98bcd3","name":"refs/remotes/origin/master"}]}}},"lastBuiltRevision":{"SHA1":"836350a52b3a7727b7b877a6912d078bec98bcd3","branch":[{"SHA1":"836350a52b3a7727b7b877a6912d078bec98bcd3","name":"refs/remotes/origin/master"}]},"remoteUrls":["http://192.168.56.101/root/springboot.git"],"scmName":""},{"_class":"hudson.plugins.git.GitTagAction"},{},{},{},{},{"_class":"org.jenkinsci.plugins.pipeline.modeldefinition.actions.RestartDeclarativePipelineAction"},{},{"_class":"org.jenkinsci.plugins.workflow.job.views.FlowGraphAction"},{},{},{}],"artifacts":[],"building":false,"description":null,"displayName":"#34","duration":17341,"estimatedDuration":17208,"executor":null,"fullDisplayName":"Kubepipe #34","id":"34","keepLog":false,"number":34,"queueId":11,"result":"SUCCESS","timestamp":1577540480049,"url":"http://192.168.56.100:8080/job/Kubepipe/34/","changeSets":[],"culprits":[]* Connection #0 to host 192.168.56.100 left intact
,"nextBuild":{"number":35,"url":"http://192.168.56.100:8080/job/Kubepipe/35/"},"previousBuild":{"number":33,"url":"http://192.168.56.100:8080/job/Kubepipe/33/"}}[root@ciserver jenkins]#

위와 같이 build_number 대신 lastStableBuild를 입력할 경우 가장 최신의 빌드 결과를 알 수 있다.

각 조회 결과는 json, xml, python 타입을 지정할 수 있다.

728x90
LIST
댓글
댓글쓰기 폼