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