博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Jenkins Pipline语法
阅读量:5294 次
发布时间:2019-06-14

本文共 11747 字,大约阅读时间需要 39 分钟。

引用自:http://baijiahao.baidu.com/s?id=1582812185263227836&wfr=spider&for=pc

引用自:https://www.cnblogs.com/YatHo/p/7856556.html

一、多参数选择

pipeline{

agent {
label '192.168.0.156'
}
parameters{
choice(name:'test',
choices:'test_a\ntest_b',
description:'this is test')
}
environment{
b = "/bbb"
}
stages{
stage('阶段1'){
steps{
script{
svn_addr="svn://192.168.0.151/pycf/Projects/fof2.0/fof2.0-product_daily_data"
a = "/home"
sh "ls"
sh "pwd"
sh "echo \"test1\""
sh "echo ${test}"
sh "echo \"当前构建ID:${env.BUILD_ID}\""
sh "echo \"当前构建JOB:${env.JOB_NAME}\""
sh "echo \"当前构建URL:${env.JENKINS_URL}\""
}
checkout([$class: 'SubversionSCM',
additionalCredentials: [],
excludedCommitMessages: '',
excludedRegions: '',
excludedRevprop: '',
excludedUsers: '',
filterChangelog: false,
ignoreDirPropChanges: false,
includedRegions: '',
locations: [[credentialsId: 'ce2f6f79-ccf0-4c8b-a02a-4d52885cbce3',
depthOption: 'infinity',
ignoreExternalsOption: true,
local: '.',
remote: svn_addr ]],
quietOperation: true,
workspaceUpdater: [$class: 'UpdateUpdater']])
}
}
stage('阶段2'){
steps{
script{
sh "ls"
sh "echo \"test2\""
sh "echo ${a}"
sh "echo ${b}"
}
}
}
stage('阶段3'){
steps{
script{
sh "echo \"test3\""
}
}
}
stage('阶段4'){
steps{
script{
sh "echo \"test4\""
}
}
}
}
}

二、单参数选择

pipeline {

agent {
label '192.168.0.156'
}
options {
timeout(time: 30, unit: 'MINUTES')
}
parameters {
//构建选项,选择构建环境
choice(name: 'release_env',
choices:'pro',
description: 'choose release environment\nserver:47.98.172.183\npro port:4564')
}
stages {
stage('准备源码') {
steps {
script {
// 类似项目执行更改以下两个参数
svn_addr="svn://192.168.0.151/pycf/Projects/fof2.0/fof2.0-product_daily_data"
module_name="product_daily_data"
workdir="/application/docker_hub/java/fof2"
project_name="fof2"
r_env=release_env
docker_file_path="${workdir}/${module_name}_${r_env}"
docker_compose_file="${workdir}/docker-compose-${r_env}.yml"
docker_compose_module_name="${project_name}_${module_name}_${r_env}"
}
checkout([$class: 'SubversionSCM',
additionalCredentials: [],
excludedCommitMessages: '',
excludedRegions: '',
excludedRevprop: '',
excludedUsers: '',
filterChangelog: false,
ignoreDirPropChanges: false,
includedRegions: '',
locations: [[credentialsId: 'dffd18a8-473e-477e-a4dc-4d528fa3e55c',
depthOption: 'infinity',
ignoreExternalsOption: true,
local: '.',
remote: svn_addr ]],
quietOperation: true,
workspaceUpdater: [$class: 'UpdateUpdater']])
}
}
stage('构建运行') {
steps {
sh "mkdir -p ${docker_file_path}; rm -rf ${docker_file_path}/*"
sh "/bin/cp -a ./* ${docker_file_path};"
sh "echo 'svn url: ${svn_addr}' > ${docker_file_path}/README.txt"
sh "svn upgrade . &> /dev/null||echo &> /dev/null"
sh "export LANG=en_US.UTF-8;svn info . 2>/dev/null|grep -i Revision|awk '{print \"svn revision: \"\$2}' >> ${docker_file_path}/README.txt"
sh "echo 'jenkins job: ${env.JOB_NAME}' >> ${docker_file_path}/README.txt"
sh "echo 'jenkins build env: ${r_env}' >> ${docker_file_path}/README.txt"
sh "echo 'jenkins build number: ${env.BUILD_NUMBER}' >> ${docker_file_path}/README.txt"
sh "cd /application/docker_hub/fof2.0-deploy/;/usr/bin/python main.py 10 yes;"
}
}
stage('等待30s检查业务') {
steps {
script {
sh "sleep 30;"
sh " cd /application/docker_hub/fof2.0-deploy/;/usr/bin/python check.py ${docker_compose_module_name};"
}
}
}
}
}

三、git或svn代码拉取方式如下:

四、java上线

  

pipeline {

agent {
label '192.168.0.156'
}
options {
timeout(time: 30, unit: 'MINUTES')
}
parameters {
//构建选项,选择构建环境
choice(name: 'release_env',
choices:'dev\npro',
description: 'choose release environment\ndev port:4900\npro port:4950 \nserver:192.168.0.156')
}
tools {
maven 'M3' #此处要在全局变量设置,如果节点没有,会在master自动去获取
jdk 'jdk_1.8.0_161' #此处要在全局变量设置,如果节点没有,会在master自动去获取
}
stages {
stage('准备源码') {
steps {
script {
// 类似项目执行更改以下两个参数
svn_addr="svn://192.168.0.151/pycf/Projects/pySecondSystemUpdate"
module_name="secondsystem_update"

workdir="/application/docker_hub/java/secondsystem"

project_name="secondsystem"
r_env=release_env
docker_file_path="${workdir}/${module_name}_${r_env}"
docker_compose_file="${workdir}/docker-compose-${r_env}.yml"
docker_compose_module_name="${module_name}_${r_env}"
send_mail_to = 'huruizhi@pystandard.com'
// logs
old_date=sh returnStdout: true ,script: "echo \$(date -d '-1 hour' '+%F-%H-%M-%S')"
old_date=old_date.trim()
date=sh returnStdout: true ,script: "echo \$(date '+%F-%H-%M-%S')"
date=date.trim()
logsdir="/application/docker_hub/logs/pySecondSystemUpdate"
module_logs_path="${logsdir}/${module_name}_${r_env}"
sh "mkdir -p ${module_logs_path}"
docker_containers_path="/var/lib/docker/containers"
}
checkout([$class: 'SubversionSCM',
additionalCredentials: [],
excludedCommitMessages: '',
excludedRegions: '',
excludedRevprop: '',
excludedUsers: '',
filterChangelog: false,
ignoreDirPropChanges: false,
includedRegions: '',
locations: [[credentialsId: 'dffd18a8-473e-477e-a4dc-4d528fa3e55c',
depthOption: 'infinity',
ignoreExternalsOption: true,
local: '.',
remote: svn_addr ]],
quietOperation: true,
workspaceUpdater: [$class: 'UpdateUpdater']])
}
}
stage('MVN打包') {
steps {
sh 'mvn -B -DskipTests clean package'
}
}
stage('MVN测试') {
steps {
sh 'mvn test'
}
}

stage('生成Dockerfile') {

steps {
sh "mkdir -p ${docker_file_path}; rm -rf ${docker_file_path}/*"
sh "/bin/cp -a ./target/*.jar ${docker_file_path}; /bin/cp -a /usr/share/zoneinfo/Asia/Shanghai ${docker_file_path}"
sh "echo '#!/bin/bash\n\
app=\$(ps aux|grep -w ${docker_compose_module_name}.jar|grep -v grep|wc -l)\n\
if [[ \${app} != 0 ]];then\n\
exit 0\n\
fi\n\
exit 1\n\
' > ${docker_file_path}/docker-healthcheck"
sh "sed -i 's/^[ \t]*//g' ${docker_file_path}/docker-healthcheck"
sh "echo 'FROM java:8\n\
VOLUME /tmp\n\
ADD *.jar /${docker_compose_module_name}.jar\n\
ADD Shanghai /etc/localtime\n\
ADD docker-healthcheck /usr/bin\n\
RUN chmod 755 /usr/bin/docker-healthcheck\n\
RUN echo \'Asia/Shanghai\' > /etc/timezone\n\
RUN echo \"export TZ=\'Asia/Shanghai\'\" >> /etc/profile\n\
EXPOSE 5000\n\
HEALTHCHECK --interval=5s --timeout=2s --retries=3 CMD [\"/usr/bin/docker-healthcheck\"]\n\
ENTRYPOINT [\"java\",\"-jar\",\"/${docker_compose_module_name}.jar\",\"--server.port=5000\", \"--spring.profiles.active=${r_env}\"]\n\
' > ${docker_file_path}/Dockerfile"
sh "sed -i 's/^[ \t]*//g' ${docker_file_path}/Dockerfile"
sh "echo 'svn url: ${svn_addr}' > ${docker_file_path}/README.txt"
sh "svn upgrade . &> /dev/null||echo &> /dev/null"
sh "export LANG=en_US.UTF-8;svn info . 2>/dev/null|grep -i Revision|awk '{print \"svn revision: \"\$2}' >> ${docker_file_path}/README.txt"
sh "echo 'jenkins job: ${env.JOB_NAME}' >> ${docker_file_path}/README.txt"
sh "echo 'jenkins build env: ${r_env}' >> ${docker_file_path}/README.txt"
sh "echo 'jenkins build number: ${env.BUILD_NUMBER}' >> ${docker_file_path}/README.txt"
}
}
stage('停止业务') {
steps {
sh "cat ${docker_compose_file}|grep build:|awk '{print \$2}'|xargs mkdir -p||echo ''"
sh "docker-compose -f ${docker_compose_file} stop ${docker_compose_module_name}||echo '${docker_compose_module_name}' is not exist"
}
}
stage('备份日志') {
steps {
script {
docker_old_container_id=sh returnStdout: true ,script: "/usr/bin/docker ps -aqf 'name=${docker_compose_module_name}'"
docker_old_container_id=docker_old_container_id.trim()
if (docker_old_container_id != '' ) {
docker_old_container_id_log_file=sh returnStdout: true ,script: "find ${module_logs_path} -type l | xargs basename || echo ${old_date}.log"
docker_old_container_id_log_file=docker_old_container_id_log_file.trim()
sh "rm -rf ${module_logs_path}/${docker_old_container_id_log_file}||echo ''"
sh "/bin/cp -a ${docker_containers_path}/${docker_old_container_id}*/${docker_old_container_id}*.log ${module_logs_path}/${docker_old_container_id_log_file}"
}
}
}
}
stage('构建运行') {
steps {
sh "docker-compose -f ${docker_compose_file} rm -f ${docker_compose_module_name}||echo '${docker_compose_module_name}' is not exist"
sh "docker-compose -f ${docker_compose_file} up --build -d ${docker_compose_module_name}"
}
}
stage('链接日志') {
steps {
script {
docker_new_container_id=sh returnStdout: true ,script: "/usr/bin/docker ps -aqf 'name=${docker_compose_module_name}'"
docker_new_container_id=docker_new_container_id.trim()
sh "/usr/bin/ln -sf ${docker_containers_path}/${docker_new_container_id}*/${docker_new_container_id}*.log ${module_logs_path}/${date}.log"
sh "chmod 755 -R ${docker_containers_path}"
}
}
}
stage('等待30s检查业务') {
steps {
script {
sh "sleep 30;"
docker_health_check=sh returnStdout: true ,script: "/usr/bin/docker ps 2>/dev/null|grep -w ${docker_compose_module_name}|grep -w healthy|wc -l"
docker_health_check=docker_health_check.trim()
if (docker_health_check == '0') {
sh "echo '${docker_compose_module_name} status is unhealthy';docker-compose -f ${docker_compose_file} logs ${docker_compose_module_name} || echo '${docker_compose_module_name}' is not exist;exit 1"
}
sh "echo '${docker_compose_module_name} status is healthy';exit 0"
}
}
}
}
}

四、python + docker

pipeline {

agent {
label '192.168.0.156'
}
options {
timeout(time: 30, unit: 'MINUTES')
}
parameters {
//构建选项,选择构建环境
choice(name: 'release_env',
choices:'dev\npro',
description: 'choose release environment\ndev port:4801\npro port:4851\nserver:192.168.0.156')
}
stages {
stage('准备源码') {
steps {
script {
// 类似项目执行更改以下两个参数
svn_addr="svn://192.168.0.151/pycf/Projects/PythonProjects/quality"
module_name="qc_bond_stock"
workdir="/application/docker_hub/quality_control"
project_name="quality_control"
r_env=release_env
docker_file_path="${workdir}/${module_name}_${r_env}"
docker_compose_file="${workdir}/docker-compose-${r_env}.yml"
docker_compose_module_name="${project_name}_${module_name}_${r_env}"
}
checkout([$class: 'SubversionSCM',
additionalCredentials: [],
excludedCommitMessages: '',
excludedRegions: '',
excludedRevprop: '',
excludedUsers: '',
filterChangelog: false,
ignoreDirPropChanges: false,
includedRegions: '',
locations: [[credentialsId: 'dffd18a8-473e-477e-a4dc-4d528fa3e55c',
depthOption: 'infinity',
ignoreExternalsOption: true,
local: '.',
remote: svn_addr ]],
quietOperation: true,
workspaceUpdater: [$class: 'UpdateUpdater']])
}
}
stage('构建运行') {
steps {
sh "mkdir -p ${docker_file_path}; rm -rf ${docker_file_path}/*"
sh "/bin/cp -a ./* ${docker_file_path};"
sh "echo 'svn url: ${svn_addr}' > ${docker_file_path}/README.txt"
sh "svn upgrade . &> /dev/null||echo &> /dev/null"
sh "export LANG=en_US.UTF-8;svn info . 2>/dev/null|grep -i Revision|awk '{print \"svn revision: \"\$2}' >> ${docker_file_path}/README.txt"
sh "echo 'jenkins job: ${env.JOB_NAME}' >> ${docker_file_path}/README.txt"
sh "echo 'jenkins build env: ${r_env}' >> ${docker_file_path}/README.txt"
sh "echo 'jenkins build number: ${env.BUILD_NUMBER}' >> ${docker_file_path}/README.txt"
sh "cat ${docker_compose_file}|grep build:|awk '{print \$2}'|xargs mkdir -p||echo 'no path' to mk"
sh "docker-compose -f ${docker_compose_file} stop ${docker_compose_module_name}||echo '${docker_compose_module_name}' is not exist"
sh "docker-compose -f ${docker_compose_file} rm -f ${docker_compose_module_name}||echo '${docker_compose_module_name}' is not exist"
sh "docker-compose -f ${docker_compose_file} up --build -d ${docker_compose_module_name}"
}
}
stage('等待30s检查业务') {
steps {
script {
sh "sleep 30;"
docker_health_check=sh returnStdout: true ,script: "/usr/bin/docker ps 2>/dev/null|grep -w ${docker_compose_module_name}|grep -w healthy|wc -l"
docker_health_check=docker_health_check.trim()
if (docker_health_check == '0') {
sh "echo '${docker_compose_module_name} status is unhealthy';docker-compose -f ${docker_compose_file} logs ${docker_compose_module_name} || echo '${docker_compose_module_name}' is not exist;exit 1"
}
sh "echo '${docker_compose_module_name} status is healthy';exit 0"
}
}
}
}
}

五、配置好maven和jdk后要点击此处才能查看

 

转载于:https://www.cnblogs.com/Presley-lpc/p/9166223.html

你可能感兴趣的文章
ajax连接服务器框架
查看>>
wpf样式绑定 行为绑定 事件关联 路由事件实例
查看>>
利用maven管理项目之POM文件配置
查看>>
用HttpCombiner来减少js和css的请问次数
查看>>
FUSE-用户空间文件系统
查看>>
将tiff文件转化为jpg文件并保存
查看>>
ubuntu 16.04 开机脚本
查看>>
 VS2012 C#调用C++ dll
查看>>
TCL:表格(xls)中写入数据
查看>>
SQL SERVER 2005中如何获取日期(一个月的最后一日、一年的第一日等等)
查看>>
django 学习笔记(转)
查看>>
控制台程序秒变Windows服务(Topshelf)
查看>>
字节流与字符流的区别详解
查看>>
20141026--娱乐-箱子
查看>>
自定义分页
查看>>
Oracle事务
查看>>
任意输入10个int类型数据,把这10个数据首先按照排序输出,挑出这些数据里面的素数...
查看>>
String类中的equals方法总结(转载)
查看>>
图片问题
查看>>
bash使用规则
查看>>