Jenkins pipeline语法

Declarative Pipeline语法


pipeline{

   agentany

   options{

       timeout(time: 1,unit:'HOURS')//超时设置     timeout(10)//设置超时,单位:分钟

       retry(2)//重试次数

       buildDiscarder(logRotator(numToKeepStr:'3'))//保持构建的最大个数

    }

   tools{//工具名称必须在Jenkins 管理Jenkins → 全局工具配置中预配置。如果agent none,这将被忽略

       maven'Maven3.5'

       jdk'java8'

    }

   stages{

       stage('download code') {

             agent{

             label"docker-manager-26"

            }

           steps{

             dir('/home/pipline/build/aliyun'){

                           sh'/home/maven/apache-maven-3.5.0/bin/mvn install'

                           }                           

            }

           post{//定义Pipeline或stage运行结束时的操作

             always{

                    dir('/home/pipline/build/sharebuy'){

                                 sh'/home/maven/apache-maven-3.5.0/bin/mvn install'

                           }      

             }

            }

        }

       stage('mvn-code'){

             agent{

             label"docker-manager-26"

            }

             steps{

                    script{//在Declarative Pipeline中执行script Pipeline代码

                           dir('/home/pipline/build/aliyun'){

                                 defmvnHome = tool'Maven3.5'

                                 env.PATH ="${mvnHome}/bin:${env.PATH}"

                                 defJAVA_HOME = tool'java8'

                                 env.PATH ="${JAVA_HOME}/bin:${env.PATH}"

                                 sh'/home/maven/apache-maven-3.5.0/bin/mvn install'

                           }

                    }

             }

        }


    }

   post{

       always{

             echo'I will always say Hello again!'  # 一般用于执行步骤后发送通知

       }

    }

}            

补充:pipeline加入钉钉通知(首先需要在插件管理界面搜索钉钉插件安装,安装完成后重启生效)
post{

//发送构建失败通知

    failure{

        dingTalk accessToken: 'xxxxxx',

imageUrl: 'http://xxx/failure.png', jenkinsUrl: 'http://xxxx/', message: '下载代码失败', notifyPeople: 'phone'

    }

}

post{

//发送构建成功通知

    success{

        dingTalk accessToken: 'xxxx',

imageUrl: 'http://xxxxx/success.png', jenkinsUrl: 'http://xxxxx/', message: '构建成功', notifyPeople: 'phone'

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容