Pipeline 基本结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
pipeline {
agent { label 'agent_label' }
tools {
jdk 'JDK1.8'
maven 'MAVEN3'
nodejs 'NODE'
}
options {
buildDiscarder logRotator(
daysToKeepStr: '',
numToKeepStr: '5'
)
}
environment {
// 环境变量配置
variable_name = 'value'
}
parameters {
choice choices: ['Option 1', 'Option 2'],
description: '请选择选项',
name: 'action'
}
stages {
stage('Stage Name') {
steps {
script {
// 脚本执行块
echo 'Hello, Jenkins'
}
}
}
}
}

参数化构建

1.选择框(Choice)

1
2
3
4
5
parameters {
choice choices: ['Option 1', 'Option 2'],
description: '请选择操作类型',
name: 'Action'
}

2. Git 分支选择(Git Param

1
2
3
gitParameter branch: '', branchFilter: '.*', 
defaultValue: '*/main', description: '选择Git分支',
name: 'git_branch'

3.多选框(Checkbox)

1
2
3
4
5
6
7
8
parameters {
extendedChoice description: '请选择环境',
multiSelectDelimiter: ',',
name: 'ENVS',
defaultValue: 'dev',
value: 'dev,qa,prod',
type: 'PT_CHECKBOX'
}

4.单选框(Radio)

1
2
3
4
5
6
parameters {
extendedChoice description: '是否清空工作区',
defaultValue: 'Yes',
type: 'PT_RADIO',
value: 'Yes,No'
}

环境变量与脚本

1.定义环境变量

1
2
3
environment {
variable_name = 'value'
}

2.执行Shell命令

1
2
3
4
sh '''
echo "Executing shell script"
ls -l
'''

3.执行Groovy脚本

1
2
3
groovs复制代码script {
echo "Executing Groovy script"
}

Git操作

1. Git克隆

1
2
3
checkout([$class: 'GitSCM', 
branches: [[name: '*/main']],
userRemoteConfigs: [[url: 'git_url', credentialsId: 'git_credentials']]])

2. Git清空工作区并拉取新代码

1
2
3
if (env.CleanWorkspace == "Yes") {
deleteDir() // 清理工作区
}

并行处理

并发执行

1
2
3
4
5
def tasks = [:]
tasks['task_name'] = {
sh 'command_to_execute'
}
parallel tasks

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
stage('服务重启') {
when {
environment name: 'Action', value: '重启服务'
}
steps {
script {
for (ENV in ENVS.tokenize(',')) {
for (ProjectName in ProjectNames.tokenize(',')) {
if ( ProjectName == "credit-support" ) {
SubProjectNames = "credit-cache-manage,credit-rule-manage,credit-workflow,credit-uaa,credit-task"
}
if ( ProjectName == "credit-business" ) {
SubProjectNames = "business-account,business-manage,business-credit,business-loan,business-collateral,business-risk,business-afterloan,business-warning,business-query,business-foreign,business-store"
}
if ( ProjectName == "credit-interface-converter" ) {
SubProjectNames = "credit-interface-converter"
}
if ( ProjectName == "credit-gateway" ) {
SubProjectNames = "credit-gateway"
}
if ( ProjectName == "credit-druid-admin" ) {
SubProjectNames = "credit-druid-admin"
}
SubProjectNames.split(',').each { SubProjectName ->
if ( SubProjectName == "credit-gateway" ) {
DeployNodes = "dics-app1,dics-app2"
}
if ( SubProjectName == "credit-cache-manage" ) {
DeployNodes = "dics-app1,dics-app2"
}

def tasks_node = [:] // 任务列表
DeployNodes.split(',').each { DeployNode ->
tasks_node[DeployNode] = {
def remote_path = '/data/docker-compose'
def remote_cmd = "cd ${remote_path}/${SubProjectName}; source compose.profile && docker-compose restart"

sshPublisher(
publishers: [
sshPublisherDesc(
configName: "$ENV-${DeployNode}",
transfers: [sshTransfer(
cleanRemote: false, excludes: '',
execCommand: "${remote_cmd}",
execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+',
remoteDirectory: "", remoteDirectorySDF: false, removePrefix: "",
sourceFiles: ""
)
], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false
)
]
)
}
}
parallel tasks_node // 并行执行所有任务
}
}
}
}
}
}

远程部署

1.SSH部署到远程服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
sshPublisher(
publishers: [
sshPublisherDesc(
configName: 'remote_server',
transfers: [
sshTransfer(
sourceFiles: 'source_file',
remoteDirectory: '/remote/path',
execCommand: 'cd /remote/path && docker-compose up -d'
)
]
)
]
)

条件判断与阶段执行

1.根据参数条件执行阶段

1
2
3
4
5
6
7
8
stage('Build') {
when {
environment name: 'Action', value: 'Build'
}
steps {
echo 'Building application'
}
}

2.执行条件判断(AnyOf、AllOf)

1
2
3
4
5
6
7
8
9
10
11
when {
anyOf {
allOf {
environment name: 'Action', value: 'Build'
environment name: 'MavenBuild', value: 'Yes'
}
allOf {
environment name: 'Action', value: 'Deploy'
}
}
}