Jenkinsfile 入门指南

本人也刚刚入门学习Jenkinsfile,本文给出简单的使用说明,不正确的步骤望大佬指教。

一、Blue Ocean 安装

在插件库安装

image.png

重启jenkins
二、工程创建

image.png
三、第一个Jenkinsfile

由于Jenkinsfile支持groovy语法,所以下面有用该方式写的一些工具文件

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
62
63
64
65
66
67
php复制代码/**
*
*/
return this

/**
* 拉取代码
* @param gitUrl
* @param gitBranch
* @param credentialsId
* @return
*/
def gitClone(String gitUrl, String gitBranch, String credentialsId='11eb2bd6-050b-4853-9dd6-8118a8d3e370') {
checkout([
$class: 'GitSCM',
branches: [[name: gitBranch]],
doGenerateSubmoduleConfigurations: false,
extensions: [[
$class: 'SubmoduleOption',
disableSubmodules: false,
parentCredentials: true,
recursiveSubmodules: true,
reference: '',
trackingSubmodules: false
]],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: credentialsId, url: gitUrl]]
])
}

/**
* ssh发布 如果有文件上传,则会上传到对应用户的根目录下
* @param ip
* @param sourceFiles
* @param remoteDirectory
* @param removePrefix
* @param execCommand 远程执行命令
* @return
*/
def sshPublish(String ip,String sourceFiles,String remoteDirectory,String removePrefix,String execCommand){
sshPublisher(
publishers: [
sshPublisherDesc(
configName: '192.168.137.100',
transfers: [
sshTransfer(
cleanRemote: false,
excludes: '',
execCommand: 'ls',
execTimeout: 120000,
flatten: false,
makeEmptyDirs: false,
noDefaultExcludes: false,
patternSeparator: '[, ]+',
remoteDirectory: '/temp/springboot-ci/',
remoteDirectorySDF: false,
removePrefix: 'target',
sourceFiles: 'target/*.jar'
)
],
usePromotionTimestamp: false,
useWorkspaceInPromotion: false,
verbose: true
)
]
)
}
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
python复制代码pipeline {
agent any
environment {
APP_VERSION = ''
APP_DIR = 'APP_WORKSPACE'
APP_WORKSPACE = '${WORKSPACE}/${APP_DIR}'
COMMIT_MSG = ''
APP_CODE = """${sh(
returnStdout: true,
script: 'echo ${SYSTEM_NAME#*:}'
).trim()}"""
APP_QCBD = """${sh(
returnStdout: true,
script: 'echo ${QCBD#*:}'
).trim()}"""
}
parameters {
choice(name: 'SYSTEM_NAME', choices: ['门店系统:sms', '运维系统:cos'], description:'请选择服务')
string(name: 'BRANCH', defaultValue: '', description: '分支名')
choice(name: 'QCBD', choices: ['普通发布:NO', '快速发布:YES'], description:'发布类型')
}
post {
always {
pwd()
}
}
stages {
stage('Init') {
steps {
script {
utils = load "./script/utils.groovy"
utils.gitClone('http://192.168.137.200/CI/springboot-ci.git','*/${BRANCH}')
}
}
}
stage('Build') {
steps {
script {
echo 'Building..'
sh "pwd"
if(env.APP_QCBD == 'YES'){
echo "执行快速发布不打包,利用上次打包后的文件"
}else{
sh '/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true'
}
}
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
script{
utils = load "./script/utils.groovy"
utils.sshPublish('192.168.137.100','target/*.jar','/temp/springboot-ci/','target','ls')
}
}
}
}
}

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%