jenkins pipline 和 jenkinsfile

Posted imcati

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jenkins pipline 和 jenkinsfile相关的知识,希望对你有一定的参考价值。

Jenkins Pipeline(或简称为 "Pipeline")是一套插件,将持续交付的实现和实施集成到 Jenkins 中。
Jenkins Pipeline 提供了一套可扩展的工具,用于将“简单到复杂”的交付流程实现为“持续交付即代码”。Jenkins Pipeline 的定义通常被写入到一个文本文件(称为Jenkinsfile)中,该文件可以被放入项目的源代码控制库中。
Jenkinsfile 是 Jenkins 2.x 核心特性 Pipeline 的脚本,由Groovy语言实现。
jenkinsfile 能使用两种语法进行编写 - 声明式和脚本化。
声明式和脚本化的流水线从根本上是不同的。 声明式流水线的是 Jenkins 流水线更近的特性:

    a.相比脚本化的流水线语法,它提供更丰富的语法特性,
    b.是为了使编写和读取流水线代码更容易而设计的。

Jenkinsfile (Declarative Pipeline)
pipeline 
    agent any
    stages 
        stage(‘Build‘) 
            steps 
                //
            
        
        stage(‘Test‘) 
            steps 
                //
            
        
        stage(‘Deploy‘) 
            steps 
                //
            
        
    
Jenkinsfile (Scripted Pipeline)
node  
    stage(‘Build‘) 
        //
    
    stage(‘Test‘) 
        //
    
    stage(‘Deploy‘) 
        //
    

下面将以声明式脚本为例,介绍jenkinsfile:
#设置运行的agent

pipeline
agent label ‘jenkins-slave‘ // 配置构建项目在标签为jenkins-slave的机器上运行
.....
使用多个agent

pipeline 
    agent none
    stages 
        stage(‘Build‘) 
            agent any
            steps 
               echo "build..."
            
        
        stage(‘Test on Linux‘) 
            agent 
                label ‘linux‘
            
            steps 
               echo "test..."
            

配置可选参数

agent any
options
       disableConcurrentBuilds() //不允许同时执行流水线
       skipDefaultCheckout() //默认跳过来自源代码控制的代码
       timeout(time: 10, unit: ‘MINUTES‘) //设置流水线运行的超时时间
       timestamps() //预定义由Pipeline生成的所有控制台输出时间
 

配置机密文本、用户名和密码

stage(‘Deploy‘)
steps 
     withCredentials([usernamePassword(credentialsId: ‘aliyun_oss_upload‘, passwordVariable: ‘aliyun_sceret‘, usernameVariable: ‘aliyun_key‘)]) 
     sh ‘~/ossutil config -e $endpoint -i $aliyun_key -k $aliyun_sceret;~/ossutil cp -r -f dist "oss://$name"‘

注:需先在jenkins添加用户凭据

拉取代码

stage(‘Checkout‘) 
steps 
         checkout([$class: ‘GitSCM‘, branches: [[name: ‘*/$branch‘]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: ‘3‘, url: ‘ssh://[email protected]/javacode.git‘]]])


#在job中点击Pipline Syntax ,选择checkout out from version control ,选择git输入仓库地址,生成拉取代码配置

定义构建完成后执行动作

post 
       success 
           echo ‘构建成功‘
       
       failure 
           echo ‘构建失败‘
       
       unstable 
           echo ‘该任务被标记为不稳定任务‘
       
       aborted 
           echo ‘该任务被终止‘
       
   

条件判断

stage(‘Build‘)
  steps 
    script 
      if ("$gitrepo" == "java") 
                         echo "java"
              
      else if ("$gitrepo" == "python")
         echo "python"
       else 
         echo "nodejs"
       
                 
  

#if 需定义在script内

获取命令返回值

stage(‘Push‘)
  steps 
    script
    def pid = sh returnStatus: true, script: " ps -ef|grep tomcat|awk ‘print \$2‘"
    echo ‘$pid‘
    
  

以上是关于jenkins pipline 和 jenkinsfile的主要内容,如果未能解决你的问题,请参考以下文章

jenkins pipline和jenkinsfile

jenkins pipline 和 jenkinsfile

jenkins在pipline中运行后台命令

jenkins结合gitlab实现pipline的自动构建部署

Jenkins Pipline语法

jenkins自动化pipline的ci/cd流水线