自学内容网 自学内容网

jenkins声明式流水线语法详解

最基本的语法包含

  • pipeline:所有有效的声明式流水线必须包含在一个 pipeline 块中
  • stages:包含一系列一个或多个stage指令
  • stage:stage包含在stages中进行,比如某个阶段
  • steps:在阶段中具体得执行操作,一个或多个步骤

agent

指定了整个流水线特定的机器,比如多台slave节点,可以被定义在pipeline块顶层也可以根据stage层定义使用
(jenkins提供比较常用的流水线语法生成,输入对应的参数会自动生产)
在这里插入图片描述

  • any:在任意可用代理上执行流水线阶段。agent any
  • label:在配置标签得jenkins环境中可用代理上执行。agent {label ‘my slave’}
  • node:agent { node { label ‘labelName’ } } 和 agent { label ‘labelName’ } 一样, 但是 node 允许额外的选项 (比如 customWorkspace )

pipeline定义:

pipeline {
    agent {
        label 'test1'
    }

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

stage阶段定义:

pipeline {
    agent any

    stages {
        stage('Hello') {
            agent {
                label 'test1'
            }
            steps {
                echo 'Hello World'
            }
        }
    }
}

post

post部分定义一个或多个steps,这些阶段根据流水线完成情况而运行

  • always:无论流水线完成状态如何,都继续运行
  • changed:当前流水线完成状态与之前不同时,运行该步骤
  • failure:当前流水线完成状态失败时,运行该步骤
  • success:当前流水线完成状态成功时,运行该步骤
  • unstable:当前流水线完成状态不稳定时,运行该步骤
  • aborted:当前流水线完成状态中断时,运行该步骤
pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
                //echox 'hello world' (失败时)
            }
        }
    }
    post {
        always {
            echo "无论如何都运行"
        }
        failure {
            echo "失败后运行"
        }
        success {
            echo "执行成功后运行"
        }
        aborted {
            echo "异常中断运行"
        }
    }
}

模拟不同状态
always + success
在这里插入图片描述
always + aborted
在这里插入图片描述
always + failure
在这里插入图片描述

stages

包含一个或多个stage指令,至少包含一个

stage

一个stage标识一个阶段

steps

定义一些列一个或多个步骤操作

pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
                sh 'date'
                sh 'pwd'
            }
        }
    }
}

environment

定义为所有步骤的环境变量,全局变量/局部变量取决于environment在流水线的那个位置

示例

pipeline {
    agent any
    environment {
        CC = '全局变量'
    }

    stages {
        stage('Hello') {
            environment {
                VV = '局部变量'
            }
            steps {
                echo "this is ${CC}"
                echo "this is ${VV}"
            }
        }
    }
}

结果
在这里插入图片描述

options

允许从流水线内部配置特定的选项,比如timestamps

  • disableConcurrentBuilds:禁止并发构建
  • skipStagesAfterUnstable:构建状态不稳定时,跳过
  • timeout:运行超时时间
  • retry:失败时,重新构建的次数
  • timestamps:控制台时间输出
  • ansiColor:日志输出中启用ANSI颜色代码的支持

示例

pipeline {
    agent any
    options {
        ansiColor('xterm')
        disableConcurrentBuilds()
        retry(3)
        timeout(time: 30, unit: 'SECONDS')
        timestamps()
        skipStagesAfterUnstable()
    }

    stages {
        stage('Hello') {
            steps {
                echo "hello world"
                echo "\033[32mThis text is green\033[0m"
            }
        }
    }
}

结果
在这里插入图片描述

parameters

提供用户在触发流水线时应该提供的参数列表,参数化构建

  • string:字符串类型参数
  • booleanParam:布尔参数
  • choice:从预定义选项列表中选择
  • password:密码参数

示例

pipeline {
    agent any
    parameters {
        string (defaultValue: 'master', description: '主分支区分', name: 'BRANCH_NAME')
        choice (choices: ['dev', 'prod', 'pre'], description: '环境区分', name: 'ENVIRONMENT')
    }

    stages {
        stage('Example') {
            steps {
                checkout scmGit(branches: [[name: '${BRANCH_NAME}']], extensions: [], userRemoteConfigs: [[credentialsId: 'ae008f10-597f-4332-b2f7-790c9b01074e', url: 'https://gitee.com/liujiangxu/dubbo-demo-web.git']])
            }
        }
        stage('deploy') {
            steps {
                script {
                    echo "Deploying to ${ENVIRONMENT} environment"
                }
            }
        }    
    }
}

结果
在这里插入图片描述
在这里插入图片描述

triggers

定义流水线被重新触发的自动化方式

  • cron:执行间隔,类似任务计划
  • pollSCM:在设置间隔中,jenkins会检查新的源代码更新,如果更新,会被触发
  • upstream:其他工程构建后触发

cron示例

pipeline {
    agent any
    triggers {
        cron 'H(29-30) */4 * * * '
    }

    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

upstream示例

pipeline {
    agent any
    triggers {
        upstream 'ceshi-pip'
    }

    stages {
        stage('Example') {
            steps {
                echo '当ceshi-pip任务执行后,触发我自动执行'
            }
        }
    }
}

pollSCM示例

pipeline {
    agent any
    triggers {
        pollSCM '* * * * *'
    }

    stages {
        stage('checkout') {
            steps {
                checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'ae008f10-597f-4332-b2f7-790c9b01074e', url: 'https://gitee.com/liujiangxu/dubbo-demo-web.git']])
                echo "源代码已更新"
            }
        }
        stage('build') {
            steps {
                sh 'mvn clean package -DskipTests=true'
            }
        }
        stage('deploy') {
            steps {
                echo "实际部署步骤,如执行脚本部署等操作"
            }
        }
    }
}

tools

定义自动安装和放置PATH的工具一部分,根据全局工具配置选项自定义选择(例如不同业务需要不同的jdk8或者jdk11)如果 agent none 指定,则忽略该操作

  • maven:编译环境
  • jdk:java环境

示例

pipeline {
    agent any
    tools {
        jdk 'openjdk11.0.18'
        maven 'maven1'
    }

    stages {
        stage('Example') {
            steps {
                sh 'mvn --version'
                sh 'java -version'
            }
        }
    }
}

when

指令允许流水线根据给定的条件决定是否执行该阶段。when必须包含一个条件,当包含多个条件时,所有子条件必须返回true才会执行

  • branch:用于检查当前构建的分支是否与指定的分支模式匹配时执行
  • environment:用于检查环境变量是否满足给定的名称和值。如果环境变量存在且值匹配时执行
  • not:用于反转另一个条件的结果。如果内部条件不满足(即返回false)时执行
  • allOf:用于组合多个条件,只有当所有子条件都满足时(即都返回true)时执行
  • anyOf:用于组合多个条件,但与allOf不同,只要至少一个子条件满足(即返回true),就会执行

bracnch示例

pipeline {
    agent any

    stages {
        stage('build on master') {
            when {
                branch 'master'
            }
            steps {
                echo '构建为master分支时执行' //跳过执行
            } 
        }    
    }
}

environment示例

pipeline {
    agent any
    
    stages {
        stage('deploy to prod') {
            environment {
                DEPLOY_X = 'dev'
            }
            when {
                environment name: 'DEPLOY_X', value: 'prod'
            }
            steps {
                echo '当变量参数为prod环境时执行' //跳过执行
            } 
        }    
    }
}

not示例

pipeline {
    agent any
    environment {
        DEPLOY_X = "dev"
    }
    
    stages {
        stage('not build on master') {
            when {
                 not {
                    environment name: 'DEPLOY_X', value: 'prod'
                }
            }
            steps {
                echo '当变量参数不为prod环境时执行' //会执行
            } 
        }    
    }
}

allOf 示例

pipeline {
    agent any
    environment {
        DEPLOY_X = "prod"
    }
    
    stages {
        stage('not build on master') {
            when {
                allOf {
                    environment name: 'DEPLOY_X', value: 'prod'
                    not {
                        branch 'master'
                    }
                }
            }

            steps {
                echo '当环境变量等于prod 和 分支不属于master时执行(即所有条件都满足)'//会执行
            } 
        }    
    }
}

anyOf 示例

pipeline {
    agent any
    environment {
        DEPLOY_X = "prod"
    }
    
    stages {
        stage('not build on master') {
            when {
                anyOf {
                    branch 'master'
                    environment name: 'DEPLOY_X', value: 'prod'
                }
            }

            steps {
                echo '当环境变量等于prod 或者 分支属于master时执行(即所有条件满足一项即可)' //会执行
            } 
        }    
    }
}

原文地址:https://blog.csdn.net/weixin_43757402/article/details/142362298

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!