0
点赞
收藏
分享

微信扫一扫

DevOps系列之Jenkins pipeline-03Jenkins核心语法1

皮皮球场 2022-03-17 阅读 76
jenkins

声明式流水线的定义,所有的语句块都在pipeline{ }中。

pipeline {
     //pipeline 
}

1. agent构建节点

参数:

  • any: 运行在任一可用节点。
  • none:当pipeline全局指定agent为none,则根据每个stage中定义的agent运行(stage必须指定)。
  • label:在指定的标签的节点运行。
  • node:支持自定义流水线的工作目录。
## 任意节点
pipeline {
	agent any
}

## 指定标签节点
pipeline {
	agent { label "label Name" }
}


## 自定义节点工作空间
pipeline {
  agent { 
     node {
        label "labelName"
        customWorkspace "/opt/agent/workspace"
     }
  }
}

 自定义工作空间

2. stages构建阶段

关系: stages > stage > steps > script

定义:

  • stages:包含多个stage阶段
  • stage:包含多个steps步骤
  • steps: 包含一组特定的脚本(加上script后就可以实现在声明式脚本中嵌入脚本式语法了)
pipeline {
	agent { label "build" }
  
  stages {
  		stage("build") {
      		steps {
          		echo "hello"
                script {
                   sh "echo 'this is script'"    
                }
          }
      }
   }
}

在stage中指定agent

## 在stage阶段中定义agent

pipeline {
   //全局设置为none
  agent none 
  
      stages{
  	    stage('Build'){
    	    agent { label "build" }
            steps {
                echo "building......"
            }
         }
      }
}

正常运行,在build标签的agent上。 

3. post构建后操作

定义: 根据流水线的最终状态匹配后做一些操作。

状态:

  • always: 不管什么状态总是执行
  • success: 仅流水线成功后执行
  • failure: 仅流水线失败后执行
  • aborted: 仅流水线被取消后执行
  • unstable:不稳定状态,单侧失败等等
pipeline {

 //全局设置为none
  agent none 
  
      stages{
  	    stage('Build'){
    	    agent { label "build" }
            steps {
                script { 
                    sh 
                        """   
                            sleep 5
                        """
                }
            }
         }
      }
        
    post {
        always{
            script{
                println("流水线结束后,经常做的事情")
            }
        }
        
        success{
            script{
                println("流水线成功后,要做的事情")
            }
        
        }
        failure{
            script{
                println("流水线失败后,要做的事情")
            }
        }
        
        aborted{
            script{
                println("流水线取消后,要做的事情")
            }
        
        }
    }
}

正常运行时:

 取消运行时:

 运行失败时:(可以写错shell命令进行测试)

4. env构建时变量

定义: 通过键值对格式定义流水线在运行时的环境变量, 分为流水线级别和阶段级别。(全局变量、局部变量)

变量定义在environment {}中

4.1 流水线级别环境变量

pipeline {

 //全局设置为none
  agent none 
//全局变量
  environment {
     	NAME = "liyanyi"
        VERSION = "1.1.1"
        ENVTYPE = "DEV"
    }
  
      stages{
  	    stage('Build'){
    	    agent { label "build" }
            steps {
                script { 
                    sh """
                    echo "${NAME} ${VERSION}"
                    """
                }
            }
         }
      }
        
    
}

输出结果为全局变量值

 4.2 阶段级别环境变量

pipeline {

 //全局设置为none
  agent none 
//全局变量
  environment {
     	NAME = "liyanyi"
        VERSION = "1.1.1"
        ENVTYPE = "DEV"
    }
  
      stages{
  	    stage('Build'){
    	    agent { label "build" }
            //局部变量
            environment {
                VERSION = "1.1.2"
                ENVTYPE = "PROD"
            }
            steps {
                script { 
                    sh """
                    echo "${ENVTYPE} ${VERSION}"
                    """
                }
            }
         }
      }
    
}

当局部变量与全局变量相同时,局部变量优先级高

 

举报

相关推荐

0 条评论