Jenkins基础:API:11:Job常见操作

在以前的文章中,已经详细介绍过如何使用Jenkins API进行Job的创建、拷贝、删除、执行、确认以及执行等操作。这篇文章以一个简单的脚本来实现Job相关的8种操作。

环境准备

使用Jenkins LTS 版本 2.164.3进行验证,执行之前需保证CSRF复选框被选中状态。

脚本位置

脚本和相关示例放置在easypack下Jenkins相关的路径之下

脚本路径:https://github.com/liumiaocn/easypack/blob/master/containers/alpine/jenkins/mgnt_jenkins_demo.sh

使用介绍

使用示例如下所示:

liumiaocn:jenkins liumiao$ sh mgnt_jenkins_demo.sh 
[JENKINS Crumb]: Jenkins-Crumb:c8d3e1a4b0761c884af20bb0696afb87
Usage: mgnt_jenkins_demo.sh ACTION DEMO-PATTERN
       ACTION: JOBCREATE|JOBEXEC|JOBDELETE|JOBCHECK|JOBENABLE|JOBDISABLE|JOBCOPY|LOGCHECK
   eg: mgnt_jenkins_demo.sh 
liumiaocn:jenkins liumiao$ 

Job操作1: 创建Job

创建一个带有条件分支的流水线Job,设定名称为test_helloworld,执行日志如下所示:

liumiaocn:jenkins liumiao$ sh mgnt_jenkins_demo.sh JOBCREATE when test_helloworld
[JENKINS Crumb]: Jenkins-Crumb:c8d3e1a4b0761c884af20bb0696afb87
JOB Creation Begins ...
JOB Creation Ends   ...
liumiaocn:jenkins liumiao$ 

执行结果如下图所示,可以看到名为test_helloworld的job已经创建成功。
在这里插入图片描述

Job操作2: 执行Job

执行此Job,具体执行日志如下所示:

liumiaocn:jenkins liumiao$ sh mgnt_jenkins_demo.sh JOBEXEC test_helloworld
[JENKINS Crumb]: Jenkins-Crumb:c8d3e1a4b0761c884af20bb0696afb87
JOB [test_helloworld] Build Begins ...
JOB [test_helloworld] Build Ends   ...
liumiaocn:jenkins liumiao$ 

从blueocean插件上可以确认到结果如下所示
在这里插入图片描述

Job操作3: 确认Job信息

可以确认xml方式的Job信息,详细执行日志如下所示

liumiaocn:jenkins liumiao$ sh mgnt_jenkins_demo.sh JOBCHECK test_helloworld
[JENKINS Crumb]: Jenkins-Crumb:c8d3e1a4b0761c884af20bb0696afb87
JOB [test_helloworld] API Check Begins ...
<?xml version="1.0"?>
<workflowJob _class="org.jenkinsci.plugins.workflow.job.WorkflowJob">
  <action/>
  <action/>
  <action/>
  <action/>
  <action/>
  <action/>
  <action/>
  <action/>
  <action _class="com.cloudbees.plugins.credentials.ViewCredentialsAction"/>
  <description>Pipeline Job Sample</description>
  <displayName>test_helloworld</displayName>
  <fullDisplayName>test_helloworld</fullDisplayName>
  <fullName>test_helloworld</fullName>
  <name>test_helloworld</name>
  <url>http://localhost:32002/job/test_helloworld/</url>
  <buildable>true</buildable>
  <build _class="org.jenkinsci.plugins.workflow.job.WorkflowRun">
    <number>1</number>
    <url>http://localhost:32002/job/test_helloworld/1/</url>
  </build>
  <color>blue</color>
  <firstBuild _class="org.jenkinsci.plugins.workflow.job.WorkflowRun">
    <number>1</number>
    <url>http://localhost:32002/job/test_helloworld/1/</url>
  </firstBuild>
  <healthReport>
    <description>Build stability: No recent builds failed.</description>
    <iconClassName>icon-health-80plus</iconClassName>
    <iconUrl>health-80plus.png</iconUrl>
    <score>100</score>
  </healthReport>
  <inQueue>false</inQueue>
  <keepDependencies>false</keepDependencies>
  <lastBuild _class="org.jenkinsci.plugins.workflow.job.WorkflowRun">
    <number>1</number>
    <url>http://localhost:32002/job/test_helloworld/1/</url>
  </lastBuild>
  <lastCompletedBuild _class="org.jenkinsci.plugins.workflow.job.WorkflowRun">
    <number>1</number>
    <url>http://localhost:32002/job/test_helloworld/1/</url>
  </lastCompletedBuild>
  <lastStableBuild _class="org.jenkinsci.plugins.workflow.job.WorkflowRun">
    <number>1</number>
    <url>http://localhost:32002/job/test_helloworld/1/</url>
  </lastStableBuild>
  <lastSuccessfulBuild _class="org.jenkinsci.plugins.workflow.job.WorkflowRun">
    <number>1</number>
    <url>http://localhost:32002/job/test_helloworld/1/</url>
  </lastSuccessfulBuild>
  <nextBuildNumber>2</nextBuildNumber>
  <property _class="hudson.plugins.jira.JiraProjectProperty"/>
  <property _class="com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty"/>
  <concurrentBuild>true</concurrentBuild>
  <resumeBlocked>false</resumeBlocked>
</workflowJob>
JOB [test_helloworld] API Check Ends   ...
liumiaocn:jenkins liumiao$ 

Job操作4: 确认执行日志

可以通过API方式获得Job执行的日志信息,比如此处获取第一次执行的日志信息

liumiaocn:jenkins liumiao$ sh mgnt_jenkins_demo.sh LOGCHECK test_helloworld 1
[JENKINS Crumb]: Jenkins-Crumb:c8d3e1a4b0761c884af20bb0696afb87
JOB [test_helloworld] LOG Check Begins ...
Started by user root
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /data/jenkins/workspace/test_helloworld
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Init)
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] sh
+ echo Build stage ...
Build stage ...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
Stage "Test" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] sh
+ echo Deploy stage ...
Deploy stage ...
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
JOB [test_helloworld] LOG Check Ends   ...
liumiaocn:jenkins liumiao$ 

Job操作5: 禁用Job

可以将Job设定为禁用状态,执行日志如下所示

liumiaocn:jenkins liumiao$ sh mgnt_jenkins_demo.sh JOBDISABLE test_helloworld
[JENKINS Crumb]: Jenkins-Crumb:c8d3e1a4b0761c884af20bb0696afb87
JOB [test_helloworld] Disable Begins ...
JOB [test_helloworld] Disable Ends   ...
liumiaocn:jenkins liumiao$

此时从Jenkins上也可以看到Job的状态已经发生了变化(变灰)
在这里插入图片描述
打开之后也可以看到更加详细的提示信息
在这里插入图片描述

Job操作6: 启用Job

同样,也可以将已经启用的Job禁用,执行日志如下所示:

liumiaocn:jenkins liumiao$ sh mgnt_jenkins_demo.sh JOBENABLE test_helloworld
[JENKINS Crumb]: Jenkins-Crumb:c8d3e1a4b0761c884af20bb0696afb87
JOB [test_helloworld] Enable Begins ...
JOB [test_helloworld] Enable Ends   ...
liumiaocn:jenkins liumiao$ 

在这里插入图片描述

Job操作7: Job拷贝

以test_helloworld作为源,进行Job拷贝,执行日志如下所示:

liumiaocn:jenkins liumiao$ sh mgnt_jenkins_demo.sh JOBCOPY test_helloworld test_liumiaocn
[JENKINS Crumb]: Jenkins-Crumb:c8d3e1a4b0761c884af20bb0696afb87
JOB [test_helloworld] COPY to [test_liumiaocn] Begins ...
JOB [test_helloworld] COPY to [test_liumiaocn] Ends   ...

liumiaocn:jenkins liumiao$ 

执行结果如下所示
在这里插入图片描述

Job操作8: Job删除

删除名为test_helloworld的Job,执行日志如下所示

liumiaocn:jenkins liumiao$ sh mgnt_jenkins_demo.sh JOBDELETE test_helloworld
[JENKINS Crumb]: Jenkins-Crumb:c8d3e1a4b0761c884af20bb0696afb87
JOB [test_helloworld] Delete Begins ...
JOB [test_helloworld] Delete Ends   ...
liumiaocn:jenkins liumiao$ 

从Jenkins上也能看到结果的变化
在这里插入图片描述

总结

提供了一个简单的脚本用于执行常见的Job操作,非常简陋,没有考虑到各种例外情况,比如在已经创建Job的情况下再次执行,在经删掉Job的情况下再次删除,都会提示Jenkins大量的页面提示,需要直接使用的请认真考虑各种例外情况。

淼叔 CSDN认证博客专家 神经网络 TensorFlow NLP
资深架构师,PMP、OCP、CSM、HPE University讲师,EXIN DevOps Professional与DevOps Master认证讲师,曾担任HPE GD China DevOps & Agile Leader,帮助企业级客户提供DevOps咨询培训以及实施指导。熟悉通信和金融领域,有超过十年金融外汇行业的架构设计、开发、维护经验,在十几年的IT从业生涯中拥有了软件开发设计领域接近全生命周期的经验和知识积累,著有企业级DevOps技术与工具实战。
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页