Jenkins基础:Jenkinsfile使用实例:12:在Jenkins分布式构建环境中执行构建

Jenkins支持多种方式实现分布式构建,这篇文章介绍在一主节点(Master)三从节点(Slave)的Jenkins分布式构建环境中使用Jenkinsfile的构建方式。

环境准备

构建JNLP方式的环境,构成信息如下所示:
Jenkins基础:Jenkinsfile使用实例:12:在Jenkins分布式构建环境中执行构建_第1张图片
环境准备过程可参看如下内容:

  • https://blog.csdn.net/liumiaocn/article/details/94384235

示例Jenkinsfile

示例

本文示例Jenkinsfile将流水线分按照功能分为逻辑上的三段:构建(Build) 、测试(Test)、部署(Deploy),顺序如下所示。

构建
测试
部署

示例所使用的Jenkinsfile信息如下所示,说明如下:

  • pipeline是结构,在其中可以指定agent和stages等相关信息
  • agent用于指定执行job的节点,any为不做限制
  • stages用与设定具体的stage
  • stage为具体的节点,比如本文示例中模拟实际的 Build(构建)、测试(Test)、部署(Deploy)的过程。
pipeline {
    agent any 

    stages {
        stage('Build') { 
            steps { 
                sh 'echo Build stage ...' 
            }
        }
        stage('Test'){
            steps {
                sh 'echo Test stage ...' 
            }
        }
        stage('Deploy') {
            steps {
                sh 'echo Deploy stage ...' 
            }
        }
    }
  }

获取Jenkins-Crumb

使用如下示例代码获取Jenkins-Crumb,为使用API方式生成三个Stage的Pipeline示例作准备。

liumiaocn:jenkins liumiao$ jenkins_host_url=http://localhost:32002
liumiaocn:jenkins liumiao$ user_passwd="root:liumiaocn"
liumiaocn:jenkins liumiao$ jenkins_crumb=`curl -u $user_passwd ${jenkins_host_url}'/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)' 2>/dev/null`
liumiaocn:jenkins liumiao$ echo $jenkins_crumb
Jenkins-Crumb:de06be27cb740cc347943ff72b399d6e
liumiaocn:jenkins liumiao$

创建Job

上述Pipeline以及相关的config.xml文件在easypack中有保存,可以直接使用。

  • git clone 并切换目录

命令:git clone https://github.com/liumiaocn/easypack.git
cd easypack/containers/alpine/jenkins

liumiaocn:jenkins liumiao$ ls demo/pipeline/stage/config.xml
demo/pipeline/stage/config.xml
liumiaocn:jenkins liumiao$ cat demo/pipeline/stage/config.xml
<?xml version='1.1' encoding='UTF-8'?>
<flow-definition plugin="[email protected]">
  <actions>
    <org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobAction plugin="[email protected]"/>
  </actions>
  <description>Pipeline Job Sample</description>
  <keepDependencies>false</keepDependencies>
  <properties>
    <hudson.plugins.jira.JiraProjectProperty plugin="[email protected]"/>
    <com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty plugin="[email protected]">
      <gitLabConnection></gitLabConnection>
    </com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty>
  </properties>
  <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="[email protected]">
  <script>pipeline {
    agent any 

    stages {
        stage('Build') { 
            steps { 
                sh 'echo Build stage ...' 
            }
        }
        stage('Test'){
            steps {
                sh 'echo Test stage ...' 
            }
        }
        stage('Deploy') {
            steps {
                sh 'echo Deploy stage ...' 
            }
        }
    }
  }</script>
  <sandbox>false</sandbox>
  </definition>
  <triggers/>
  <disabled>false</disabled>
</flow-definition>
liumiaocn:jenkins liumiao$ curl -X POST -u $user_passwd -H ${jenkins_crumb} -H "Content-Type:application/xml"  --data-binary  "@demo/pipeline/stage/config.xml" ${jenkins_host_url}/createItem?name=pipeline_job_noparam
liumiaocn:jenkins liumiao$ echo $?
0
liumiaocn:jenkins liumiao$

执行Job

使用如下命令或者直接在Jenkins上点击构建

liumiaocn:jenkins liumiao$ curl -X POST -u $user_passwd -H ${jenkins_crumb} ${jenkins_host_url}/job/pipeline_job_noparam/build
liumiaocn:jenkins liumiao$ echo $?
0
liumiaocn:jenkins liumiao$

确认执行结果

此处的环境为1主3从的构成,主节点不具有执行job的能力,在执行的过程中截取中间的状态可以看到主节点协调从节点完成构建的过程,如下图所示
Jenkins基础:Jenkinsfile使用实例:12:在Jenkins分布式构建环境中执行构建_第2张图片

使用如下命令可以确认相关的执行日志信息,与普通方式不同的有“Running on agent003 ”的信息提示此此构建在具体的那个Jenkins节点上进行的。

liumiaocn:jenkins liumiao$ curl -u $user_passwd ${jenkins_host_url}/job/pipeline_job_noparam/1/consoleText
Started by user root
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on agent003 in /tmp/jenkins/agent003/workspace/pipeline_job_noparam
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] sh
+ echo Build stage ...
Build stage ...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] sh
+ echo Test stage ...
Test stage ...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] sh
+ echo Deploy stage ...
Deploy stage ...
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
liumiaocn:jenkins liumiao$ 

同时也可以通过Jenkins的BlueOcean插件(Easypack集成)来确认,但是无法直接确认到Jenkins节点的详细信息。
Jenkins基础:Jenkinsfile使用实例:12:在Jenkins分布式构建环境中执行构建_第3张图片

同时执行11次构建

构建脚本:使用如下脚本,执行11次构建,以确认中间过程

for i in 1 2 3 4 5 6 7 8 9 10 11
do
echo "jenkins job build $i ..."
curl -X POST -u $user_passwd -H ${jenkins_crumb} ${jenkins_host_url}/job/pipeline_job_noparam/build
echo $?
done

执行日志如下所示

liumiaocn:jenkins liumiao$  for i in 1 2 3 4 5 6 7 8 9 10 11
> do
> echo "jenkins job build $i ..."
> curl -X POST -u $user_passwd -H ${jenkins_crumb} ${jenkins_host_url}/job/pipeline_job_noparam/build
> echo $?
> done
jenkins job build 1 ...
0
jenkins job build 2 ...
0
jenkins job build 3 ...
0
jenkins job build 4 ...
0
jenkins job build 5 ...
0
jenkins job build 6 ...
0
jenkins job build 7 ...
0
jenkins job build 8 ...
0
jenkins job build 9 ...
0
jenkins job build 10 ...
0
jenkins job build 11 ...
0
liumiaocn:jenkins liumiao$ 

截取中间执行过程,可以看到三个节点都有job在并行执行
Jenkins基础:Jenkinsfile使用实例:12:在Jenkins分布式构建环境中执行构建_第4张图片
在BlueOcean上的执行日志上也能看到这些job的执行时间相近
Jenkins基础:Jenkinsfile使用实例:12:在Jenkins分布式构建环境中执行构建_第5张图片

构建信息的确认

构建信息都在Master节点上,可参看如下操作日志

liumiaocn:jenkins liumiao$  docker exec -it jenkins_jenkins_1 sh
/ # cd $JENKINS_HOME
/data/jenkins # cd jobs//pipeline_job_noparam
/data/jenkins/jobs/pipeline_job_noparam # ls
builds	config.xml  lastStable	lastSuccessful	nextBuildNumber
/data/jenkins/jobs/pipeline_job_noparam # ls builds
1  10  11  12  2  3  4	5  6  7  8  9  lastFailedBuild	lastStableBuild  lastSuccessfulBuild  lastUnstableBuild  lastUnsuccessfulBuild
/data/jenkins/jobs/pipeline_job_noparam #

注意事项

Jenkins为了放置同一时间过多的并行构建带来的性能问题,有个设定选项为Quiet period,此项设定缺省值为5,表示5秒,此次并行实验需要将此值设定为0,否则无法确认效果。
Jenkins基础:Jenkinsfile使用实例:12:在Jenkins分布式构建环境中执行构建_第6张图片

你可能感兴趣的:(#,Jenkins,#,深入浅出Docker)