引用自: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后要点击此处才能查看

Jenkins Pipline语法的更多相关文章

  1. jenkins pipline 几个注意细节

    新建jenkins pipline 1)pipeline的脚本语法要正确,sonarqube的projectKey需要做相应的修改 2)先执行一次构建,会报错 3)进到jenkins workspac ...

  2. 持续集成工具之Jenkins pipline简单示例

    前文我们主要聊了下jenkins的插件安装.用户及权限管理.邮件发送.配置凭证到gitlab上拉取项目和创建普通job:回顾请参考https://www.cnblogs.com/qiuhom-1874 ...

  3. jenkins pipline 和 jenkinsfile

    Jenkins Pipeline(或简称为 "Pipeline")是一套插件,将持续交付的实现和实施集成到 Jenkins 中.Jenkins Pipeline 提供了一套可扩展的 ...

  4. jenkins pipline和jenkinsfile

    Jenkins Pipeline(或简称为 "Pipeline")是一套插件,将持续交付的实现和实施集成到 Jenkins 中. Jenkins Pipeline 提供了一套可扩展 ...

  5. 使用Jenkins+Pipline 持构建自动化部署之安卓源码打包、测试、邮件通知

    一.引言 Jenkins 2.x的精髓是Pipeline as Code,那为什么要用Pipeline呢?jenkins1.0也能实现自动化构建,但Pipeline能够将以前project中的配置信息 ...

  6. Jenkins pipeline 语法详解

    原文地址http://www.cnblogs.com/fengjian2016/p/8227532.html pipeline 是一套运行于jenkins上的工作流框架,将原本独立运行于单个或者多个节 ...

  7. ubuntu 16.04 jenkins pipline的实现 最终docker启动服务

    准备工作:两台虚拟机A:192.168.1.60 B:192.168.1.61 C:一个存放代码的代码库(github)A:jenkins git docker openssh-server(ssh) ...

  8. Jenkins Pipeline 语法

    Pipeline语法 先讲Declarative Pipeline,所有声明式管道都必须包含在pipeline块中: 123 pipeline { /* insert Declarative Pipe ...

  9. jenkins pipeline语法

    目录 一.声明式 二.脚本式 基本 判断 异常处理 Steps node withEnv 一.声明式 声明式Pipeline必须包含在名为pipeline的语句块中,典型的声明式Pipeline语法如 ...

随机推荐

  1. .NET面试题系列(十二)Dictionary原理

    序言 Dictionary Dictionary的开发人员都了解,和List相比,字典添加会慢,但是查找会比较快. Dictionary的本质 --- 两个数组.这是典型的用空间换取时间的做法. 先来 ...

  2. C#析构函数与Dispose

    有几种不同的操作方式 方式一: namespace ConsoleApp1 {     class Test     {         ~Test()// 析构函数         {        ...

  3. 集大软件工程15级个人作业Week1

    集大软件工程15级个人作业Week1 孙志威 201521123077 博客园主页 码云地址 阅读参考材料,并回答下面几个问题 (1)回想一下你初入大学时对网络工程专业的畅想 当初你是如何做出选择网络 ...

  4. 变量,id()

    >>> a = 1 >>> print id(a) 2870961640 >>> b = a >>> print id(b) 2 ...

  5. mysql 原理 ~ sql执行

    一 普通sql执行的具体过程1 连接器  管理连接,权限验证2 分析器  词法分析,语法分析   比如 数据表和数据列是否存在, 别名是否有歧义,是否符合标准sql语法等3 优化器检测   执行计划生 ...

  6. mysql 案例 ~查询导致的tmp临时文件问题

    一 简介:之前遇到一个tmp分区暴涨的问题,后来经过大神的指点,遂分析写下 二 分类: 1  select语句出现 using temporay tmp 下出现 #sql_631a_1.MYD #sq ...

  7. 3d图像识别基础论文:pointNet阅读笔记

    PointNet 论文阅读: 主要思路:输入独立的点云数据,进行变换不变性处理(T-net)后,通过pointNet网络训练后,最后通过最大池化和softMax分类器,输出评分结果. 摘要: 相较于之 ...

  8. AutoML相关论文

    本文为Awesome-AutoML-Papers的译文. 1.AutoML简介 Machine Learning几年来取得的不少可观的成绩,越来越多的学科都依赖于它.然而,这些成果都很大程度上取决于人 ...

  9. MySQL— 进阶

    目录 一.视图 二.触发器 三.函数 四.存储过程 五.事务 一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集 ...

  10. Getting started with machine learning in Python

    Getting started with machine learning in Python Machine learning is a field that uses algorithms to ...