东莞网站没计,删除多余wordpress前端,wordpress钉钉,全国建设市场公共服务网站最基本的语法包含
pipeline#xff1a;所有有效的声明式流水线必须包含在一个 pipeline 块中stages#xff1a;包含一系列一个或多个stage指令stage#xff1a;stage包含在stages中进行#xff0c;比如某个阶段steps#xff1a;在阶段中具体得执行操作#xff0c;一个或…最基本的语法包含
pipeline所有有效的声明式流水线必须包含在一个 pipeline 块中stages包含一系列一个或多个stage指令stagestage包含在stages中进行比如某个阶段steps在阶段中具体得执行操作一个或多个步骤
agent
指定了整个流水线特定的机器比如多台slave节点可以被定义在pipeline块顶层也可以根据stage层定义使用 (jenkins提供比较常用的流水线语法生成输入对应的参数会自动生产)
any在任意可用代理上执行流水线阶段。agent anylabel在配置标签得jenkins环境中可用代理上执行。agent {label ‘my slave’}nodeagent { node { label ‘labelName’ } } 和 agent { label ‘labelName’ } 一样, 但是 node 允许额外的选项 (比如 customWorkspace )
pipeline定义
pipeline {agent {label test1}stages {stage(Hello) {steps {echo Hello World}}}
}
stage阶段定义
pipeline {agent anystages {stage(Hello) {agent {label test1}steps {echo Hello World}}}
}post
post部分定义一个或多个steps这些阶段根据流水线完成情况而运行
always无论流水线完成状态如何都继续运行changed当前流水线完成状态与之前不同时运行该步骤failure当前流水线完成状态失败时运行该步骤success当前流水线完成状态成功时运行该步骤unstable当前流水线完成状态不稳定时运行该步骤aborted当前流水线完成状态中断时运行该步骤
pipeline {agent anystages {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 anystages {stage(Hello) {steps {echo Hello Worldsh datesh pwd}}}
}environment
定义为所有步骤的环境变量全局变量/局部变量取决于environment在流水线的那个位置
示例
pipeline {agent anyenvironment {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 anyoptions {ansiColor(xterm)disableConcurrentBuilds()retry(3)timeout(time: 30, unit: SECONDS)timestamps()skipStagesAfterUnstable()}stages {stage(Hello) {steps {echo hello worldecho \033[32mThis text is green\033[0m}}}
}结果
parameters
提供用户在触发流水线时应该提供的参数列表参数化构建
string字符串类型参数booleanParam布尔参数choice从预定义选项列表中选择password密码参数
示例
pipeline {agent anyparameters {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 anytriggers {cron H(29-30) */4 * * * }stages {stage(Example) {steps {echo Hello World}}}
}upstream示例
pipeline {agent anytriggers {upstream ceshi-pip}stages {stage(Example) {steps {echo 当ceshi-pip任务执行后触发我自动执行}}}
}pollSCM示例
pipeline {agent anytriggers {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 -DskipTeststrue}}stage(deploy) {steps {echo 实际部署步骤如执行脚本部署等操作}}}
}tools
定义自动安装和放置PATH的工具一部分根据全局工具配置选项自定义选择例如不同业务需要不同的jdk8或者jdk11如果 agent none 指定则忽略该操作
maven编译环境jdkjava环境
示例
pipeline {agent anytools {jdk openjdk11.0.18maven maven1}stages {stage(Example) {steps {sh mvn --versionsh java -version}}}
}when
指令允许流水线根据给定的条件决定是否执行该阶段。when必须包含一个条件当包含多个条件时所有子条件必须返回true才会执行
branch用于检查当前构建的分支是否与指定的分支模式匹配时执行environment用于检查环境变量是否满足给定的名称和值。如果环境变量存在且值匹配时执行not用于反转另一个条件的结果。如果内部条件不满足即返回false时执行allOf用于组合多个条件只有当所有子条件都满足时即都返回true时执行anyOf用于组合多个条件但与allOf不同只要至少一个子条件满足即返回true就会执行
bracnch示例
pipeline {agent anystages {stage(build on master) {when {branch master}steps {echo 构建为master分支时执行 //跳过执行} } }
}environment示例
pipeline {agent anystages {stage(deploy to prod) {environment {DEPLOY_X dev}when {environment name: DEPLOY_X, value: prod}steps {echo 当变量参数为prod环境时执行 //跳过执行} } }
}not示例
pipeline {agent anyenvironment {DEPLOY_X dev}stages {stage(not build on master) {when {not {environment name: DEPLOY_X, value: prod}}steps {echo 当变量参数不为prod环境时执行 //会执行} } }
}allOf 示例
pipeline {agent anyenvironment {DEPLOY_X prod}stages {stage(not build on master) {when {allOf {environment name: DEPLOY_X, value: prodnot {branch master}}}steps {echo 当环境变量等于prod 和 分支不属于master时执行(即所有条件都满足)//会执行} } }
}anyOf 示例
pipeline {agent anyenvironment {DEPLOY_X prod}stages {stage(not build on master) {when {anyOf {branch masterenvironment name: DEPLOY_X, value: prod}}steps {echo 当环境变量等于prod 或者 分支属于master时执行(即所有条件满足一项即可) //会执行} } }
}