Pipline Jenkinsfile 使用方法
2019-03-14 本文已影响0人
mocobk
Jenkinsfile
是一个包含Jenkins Pipeline定义的文本文件, 使用Groovy语法
使用Pipline
- jenkins 创建任务时选择 pipline(流水线)
- 配置Pipline Definition:
Pipeline script from SCM
Script Path:Jenkinsfile


Jenkinsfile 例子
示例1
#!/usr/bin/env groovy
pipeline{
agent any
// 可以设置环境变量
environment {
PATH = "/home/testhadoop/.pyenv/shims:$PATH"
runEnv = 'server'
}
//默认命令运行的pwd 为项目workspace
stages {
stage('Build') {
steps{
echo '='*50 + '开始构建' + '='*50
echo '---> 更新依赖包'
sh 'pipenv install --skip-lock'
echo '---> 扫描用例信息'
sh 'pipenv run python case_scanner.py'
echo 'build success!'
}
}
// stage可以添加或减少
stage('Test') {
steps{
echo 'This is a test step!'
}
}
stage('Deploy') {
steps{
echo 'This is a deploy step'
}
}
}
post {
failure{
script{
emailext attachLog: true,
// 邮件模板这里的引号一定要注意写对(坑)
body: '''${SCRIPT, template="groovy-html.template"}''',
mimeType: 'text/html',
charset:'UTF-8',
// PlatformGroup #10 构建失败
subject: "${currentBuild.fullDisplayName} 构建失败",
//用逗号或空格分隔多个邮箱
to: 'xxx@xx.com xxxx@xxx.com'
}
}
}
}
示例2
#!/usr/bin/env groovy
pipeline{
agent {
node {
label ''
// 指定空间目录
customWorkspace '/home/testhadoop/www/HttpRunnerLoan-web'
}
}
environment {
PATH = "/home/testhadoop/.pyenv/shims:$PATH"
}
stages {
stage('Build') {
steps{
echo '='*50 + '开始构建' + '='*50
echo '---> 更新依赖包'
sh 'pipenv install --skip-lock'
echo '---> 重启 HttpRunnerLoan-web 服务'
sh 'supervisorctl restart httpRunnerLoan'
// sh 执行shell 命令,当exit 1 时意味着构建失败
sh '''\
if [[ `supervisorctl status httpRunnerLoan` =~ 'RUNNING' ]]
then
echo '---> 服务启动成功'
else
echo '---> 服务启动失败!!! 日志信息如下:'
tail -n 50 flask.log
exit 1
fi
'''
echo 'build success!'
}
}
stage('Test') {
steps{
echo 'This is a test step'
}
}
stage('Deploy') {
steps{
echo 'This is a deploy step'
}
}
}
post {
failure{
script{
emailext attachLog: true,
body: '''${SCRIPT, template="groovy-html.template"}''',
mimeType: 'text/html',
charset:'UTF-8',
subject: "${currentBuild.fullDisplayName} 构建失败",
to: 'xxx@xx.com xxxx@xxx.com'
}
}
}
}
Note: 编写时一定要注意引号的使用 ' ' 和 ''' ''' 包含的字符串都会以普通字符处理, " " 会计算插值字符串的结果
具体可以参考 https://www.jianshu.com/p/34875c63f232