1.pipeline概念
Pipeline是Jenkins中最为灵活的job构建方式,可实现像流水线一样调度Jenkins任务,
通过Jenkinsfile描述整个持续集成流程
2.pipeline编写风格:声明式风格、脚本式风格
声明式:格式有强规范性(优势:可读性强。缺点:不灵活、代码冗长)
脚本式:使用groovy语言编写,灵活性高,可读性差。优点:实现自定义逻辑更方便、可对功能代码段封装为方法(函数)或类
声明式Pipeline结构:
pipline: 固定语法,代表整条流水线
agent:流水线执行位置,执行机位置
stages:流水线中多个stage容器,stages部分至少包含一个stage
stage:一个pipeline可以从逻辑上划分若干stage,每个 Stage 代表一组操作,如:Build、Test、Deploy
steps:阶段中一个或多个具体步骤的容器,steps至少包含一个步骤
echo: 写一个简单的字符串到控制台输出
其他:
Post:构建后操作
Environment:定义pipeline或stage运行时环境变量,无参数
脚本式Pipeline结构: //脚本化pipeline 顶层是node{};支持stage;可以直接使用groovy语言进行编码
node:节点,master或agent,执行step具体运行环境
stage:阶段,一个pipeline可分为若干stage,每个stage代表一组操作
例子:
node {
stage('Build'){
//
}
stage('Test'){
//
}
stage('Deploy'){
//
}
}
node:节点,master或agent,执行step具体运行环境
stage:阶段,一个pipeline可分为若干stage,每个stage代表一组操作
3.pipeline声明式与脚本式区别
(1)pipeline代码校验差异:声明式 Pipeline 会在执行前就会校验 Pipeline 语法是否正确,而脚本式不会
(2)重启stage差异:某个stage执行失败,修复后声明式pipeline可以执行跳过该stage重新执行,而脚本式重新来过
(3)option指令差异:声明式options和pipeline代码逻辑分开,而脚本式嵌套在一起 多个options需要设置 代码可读性差
(4)语法逻辑差异:声明式支持stages、steps,脚本式只支持stage
(5)声明式官方推荐写法,语法简单。脚本式灵活,适合大量特定要求、逻辑负责任务
4.声明式pipeline例子
(1)输出一个hello world
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
// 这里的hello2 是我加的,就是说明,这是stages下的第二个任务 ,就是在pipeline中加单行注释 用 // 就行
stage('Hello2') {
steps {
echo 'Hello World'
}
}
}
}
(2)指定节点、拉取代码并执行shell命令
pipeline {
//agent any
agent {
node {
label "gitee"
//customWorkspace "Workspace"
}
}
stages {
stage('拉取前端代码') {
steps {
git credentialsId: 'cbc969cf-709a-459e-8fed-3dad7d20caaa', url: 'https://gitee.com/Github-liwei/yshop_admin_web.git'
}
}
stage('拉取后端代码') {
steps {
git credentialsId: 'cbc969cf-709a-459e-8fed-3dad7d20caaa', url: 'https://gitee.com/Github-liwei/yshop.git'
echo 'Hello World'
sh "ls -trl && pwd "
}
}
}
5.脚本式pipeline例子
node {
stage('Hello') {
for(int i=0;i<3;i++){
println i
println 'Hello World!'
}
}
}
标签:脚本,pipeline,stages,编译,steps,jenkinsfile,Hello,stage From: https://www.cnblogs.com/huchao12/p/17723298.html