一.参数化构建过程
自定义构建所需的变量
变量引用说明:不同的操作环境变量引用方式不同
Shell -- $dstversion
Dos -- %dstversion%
Git源码管理-- ${dstversion}
二.构建环境
运程执行脚本
Pre build script---执行build之前执行的脚本
Post build script --执行build之后执行的脚本
三.流水线
3.1 pipeline:声明式与脚本式
pipeline {
agent {label ‘slave1’} #指定agent
environment { CC = 'clang' bb=’123’} #环境变量,引用”${CC}”
tools {
maven 'M3'
}
stages {
stage('Preparation') {
steps {
git 'https://github.com/jglick/simple.git' #拉取代码
sh ‘./test.sh’ #执行shell
sh ‘ps -ef | grep “${CC}”’ #引用环境变量
sh ''' #多行shell
ps -ef | grep java;
ps -ef | grep "${version}";
ps -ef | grep "${CC}";
pwd
'''
}
}
stage('Build') {
steps {
script{
if (isUnix()) { #执行命令
sh 'mvn -Dmaven.test.failure.ignore clean package'
} else {
bat 'mvn -Dmaven.test.failure.ignore clean package'
}
}
}
}
stage('git action'){
steps{
dir(path: "./project"){ #切换目录
git(
branch: "master",
credentialsid: "xxxx", #已添加到jenkins的凭证
并且此凭证由权限拉取此代码
url : "http://xxx.git",
changelog: true
)
sh """git status
git checkout v1.0
git branch
"""
}
}
}
stage('run other job'){ #此stage执行其他job
steps{
echo “this stage build other job”
build job: ‘demojob2’ #执行其他job
parameters:[
String(name:’myname’,value:’myvalue’)
]
}
}
}
}
3.2参数化构建 vs pipeline内部变量
参数化构建:jenkins页面添加参数==》在pipeline中
3.3 pipeline中拉取代码
git(
branch: "master",
credentialsid: "xxxx", #已添加到jenkins的凭证名并且此凭证由权限拉取此代码
url : "http://xxx.git",
changelog: true
)
1.Linux上生成私钥+公钥==》公钥与代码端用户绑定==》私钥添加jenkins凭证(ssh name+key+创建私钥时输入的密码)==>pipeline中使用凭证拉取
2.Git插件只支持branch不支持tag的解决方法:
git插件拉取完后使用shell :git checkout tag来切换tag
3.4 页面pipeline 与 pipeline from scm
页面pipeline直接在页面写pipeline
From scm 可用不同路径加载jenkinsfile (常用git上拉取)
3.5 jenkins api使用
pip install jenkinsapi
# 实例化==》创建job==》参数化构建job
self.server=Jenkins(url, username=username, password=password)
jk.server['pull_webproject1'].get_config()
self.server.create_job(config["jobname"],str(xml))
self.server.build_job(jobname,params)