简介
pipeline是一套运行在jenkins上的工作流框架,可以实现复杂流程的编排和可视化运行
pipeline支持两种语法:Declarative Pipeline(声明式流水线)和Scripted Pipeline (脚本式流水线)
本文重点介绍主流的声明式流水线
创建流水线项目
在Jenkins中如果要使用pipeline流水线的话,必须安装pipeline插件
创建一个流水线项目
pipeline结构(声明式)
基础结构
pipeline {
agent any
stages {
stage("stage") {
steps {
echo "hello pipeline"
}
}
}
}
持续集成结构(部分)
pipeline {
agent any
stages {
stage('pull project code') {
steps {
sh 'echox pull project code'
}
}
stage('mvn package') {
steps {
sh 'echo mvn package'
}
}
stage('build and push image') {
steps {
sh 'echo build and push image'
}
}
stage('deploy to k8s') {
steps {
sh 'echo deploy to k8s'
}
}
stage('pull autotest code') {
steps {
sh 'echo pull autotest code'
}
}
stage('run autotest') {
steps {
sh 'echo run autotest'
}
}
stage('allure report') {
steps {
sh 'echo allure report'
}
}
}
}
pipeline结构解析:pipeline
声明式流水线根节点,也是声明式流水线的开始
pipeline {}
pipeline结构解析:agent
agent指定流水线运行在哪个环境之中,有如下选项:
any:在任何环境中执行流水线
pipeline {
agent any
}
none:表示没有全局的agent配置,每个stage需要指定自己的agent
pipeline {
agent none
stages {
stage('allure report'){
agent any
}
}
}
node:指定在某个标签的节点上运行
pipeline {
agent none
stages {
stage('allure report'){
agent { label 'allureReport-slave-label' }
}
}
}
Dockerfile:通过dockerfile创建镜像并运行容器,在这个容器中运行,用的不多
docker:在docker中运行
k8s:运行在某一个k8s集群中,可以实现动态slave,示例:https://www.cnblogs.com/uncleyong/p/16721826.html
pipeline结构解析:stages、stage
stages:所有阶段的集合
stage:某一个阶段,stage的名称要唯一
pipeline结构解析:steps
steps:某个阶段要运行的具体步骤,包含一个或多个步骤
示例:
pipeline {
agent any
stages {
stage('pull project code') {
steps {
sh """
echo "pull project code"