首页 > 其他分享 >jenkins循序渐进

jenkins循序渐进

时间:2024-02-28 13:35:13浏览次数:24  
标签:################# en name app echo 循序渐进 jenkins true

pipeline流水线

jenkinsfile_docker

pipeline {
    agent {
            docker {
            image 'cdp_build:1.8.0_211-maven_3.5.4'
            args '-v /opt/workspace/m2:/data/lib -v /opt/workspace/deploy/dev:/deploy -v /root/.ssh:/root/.ssh'
                }
            }
    environment {
        DOCKER_REGISTRY = 'my-docker-registry.com'
        DOCKER_CRED_ID = 'my-docker-credential'
        COMMIT_ID = ''
        TAG = ''
        BRANCH = "home-develop"
        app_name = 'cdp-admin-web'
        app_dir = '/data/cdp/tomcat-admin-web'
        repository_url = 'http://192.168.2.18/chinadatapay/cdp-base.git'
        user_id = 'jluo@chinadatapay.com'
        
        
    }
    parameters {
        choice choices: ['false','true'], description: '是否整体编译打包,默认模块打包,即为false。提示:如果打包失败,选择一次整体编译打包。', name: 'is_all_build'

    }

    stages {
        stage('pull code '){
            parallel{
                stage('pulling code by jenkins'){
                    when{
                        expression{
                            env.gitlabBranch == null
                        }
                    }
                    steps{
                        git(changelog: true, poll: true, url: "${repository_url}", branch: "${BRANCH}", credentialsId: "${user_id}")
                        script{
                            COMMIT_ID = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()
                            TAG = BUILD_TAG + '-' + COMMIT_ID
                            println "Current branch is ${BRANCH}, Commit ID is ${COMMIT_ID}, Image TAG is ${TAG}"
                        }
                    }
                }
            }
        }
        stage('Build') {
            
            steps {
                sh """#!/bin/bash
                if [ "${is_all_build}" == 'false' ];then
                    cd ${app_name}
                    pwd
                    echo "#########功能节点编译打包#############"
                
                else
                    echo "#########整体节点编译打包#############"
                    pwd
                
                fi
                pwd
                mvn -T 10 -T 1C clean install -Dmaven.test.skip=true -Ptest
                
                """
            }
        }
        stage('deploy') {
            steps {
                sh """#!/bin/bash
                
                echo "#################开始部署_${app_name}#################"
                pwd
                echo "#################停止${app_name}#################"
                pssh -h /deploy/backend_${app_name}.ip  -i "cd ${app_dir}/bin && ./stop.sh tomcat-admin-web"
                
                echo "#################删除${app_name}#################"
                pssh -h /deploy/backend_${app_name}.ip  -i "rm -rf ${app_dir}/webapps/${app_name}/*"
                
                echo "#################拷贝${app_name}#################"
                pscp.pssh -h /deploy/backend_${app_name}.ip ./${app_name}/target/*.war ${app_dir}/webapps/
                
                echo "#################解压${app_name}#################"
                pssh -h /deploy/backend_${app_name}.ip  -i "unzip ${app_dir}/webapps/*.war -d ${app_dir}/webapps/${app_name}/ && rm -rf ${app_dir}/webapps/*.war "
                
                
                echo "#################启动${app_name}#################"
                pssh -h /deploy/backend_${app_name}.ip  -i "source /etc/profile && cd ${app_dir}/bin && ./startup.sh"
                echo "#################完成部署_${app_name}#################"
                
                """
            }
        }
    }
}


jenkinsfile_k8s

pipeline{
    agent { 
      kubernetes {
        cloud 'GZ-TEST-K8S'
        slaveConnectTimeout 1200
        workspaceVolume hostPathWorkspaceVolume(hostPath: "/opt/workspace", readOnly: false)
        yaml '''
apiVersion: v1
kind: Pod
spec:
  containers:
    - args: [\'$(JENKINS_SECRET)\',\'$(JENKINS_NAME)\']
      #image: 'registry.cn-beijing.aliyuncs.com/citools/jnlp:alpine'
      image: 'docker.io/jenkins/jnlp-agent-alpine'
      name: jnlp
      imagePullPolicy: IfNotPresent
      volumeMounts:
        - mountPath: "/etc/localtime"
          name: "localtime"
          readOnly: false  
      #  - mountPath: "/root/.ssh"
      #    name: "ssh"
      #    readOnly: false    
    - command:
        - "cat"
      env:
        - name: "LANGUAGE"
          value: "en_US:en"
        - name: "LC_ALL"
          value: "en_US.UTF-8"
        - name: "LANG"
          value: "en_US.UTF-8"
      #image: "registry.cn-beijing.aliyuncs.com/citools/maven:3.5.3"
      image: "192.168.6.20:80/library/jdk-maven:1.8.393-3.6.1"
      imagePullPolicy: "IfNotPresent"
      name: "build"
      tty: true
      volumeMounts:
        - mountPath: "/etc/localtime"
          name: "localtime"
        - mountPath: "/root/.m2/"
          name: "cachedir"
          readOnly: false
    - command:
        - "cat"
      env:
        - name: "LANGUAGE"
          value: "en_US:en"
        - name: "LC_ALL"
          value: "en_US.UTF-8"
        - name: "LANG"
          value: "en_US.UTF-8"
      image: "registry.cn-beijing.aliyuncs.com/citools/kubectl:self-1.17"
      imagePullPolicy: "IfNotPresent"
      name: "kubectl"
      tty: true
      volumeMounts:
        - mountPath: "/etc/localtime"
          name: "localtime"
          readOnly: false
    - command:
        - "cat"
      env:
        - name: "LANGUAGE"
          value: "en_US:en"
        - name: "LC_ALL"
          value: "en_US.UTF-8"
        - name: "LANG"
          value: "en_US.UTF-8"
      image: "registry.cn-beijing.aliyuncs.com/citools/docker:19.03.9-git"
      imagePullPolicy: "IfNotPresent"
      name: "docker"
      tty: true
      volumeMounts:
        - mountPath: "/etc/localtime"
          name: "localtime"
          readOnly: false
        - mountPath: "/var/run/docker.sock"
          name: "dockersock"
          readOnly: false
  restartPolicy: "Never"
  nodeSelector:
    build: "true"
  volumes:
    - hostPath:
        path: "/var/run/docker.sock"
      name: "dockersock"
    - hostPath:
        path: "/usr/share/zoneinfo/Asia/Shanghai"
      name: "localtime"
    - name: "cachedir"
      hostPath:
        path: "/opt/m2"
        '''
      }
    }
    stages{
        stage('pull code '){
            parallel{
                stage('pulling code by jenkins'){
                    when{
                        expression{
                            env.gitlabBranch == null
                        }
                    }
                    steps{
                       git(changelog: true, poll: true, url: "${repository_url}", branch: "${BRANCH}", credentialsId: "${user_id}")
                        script{
                            COMMIT_ID = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()
                            TAG = BUILD_TAG + '-' + COMMIT_ID
                            println "Current branch is ${BRANCH}, Commit ID is ${COMMIT_ID}, Image TAG is ${TAG}"
                        }
                    }
                }
            }
        }
        stage('building'){
            steps{
                container(name: 'build') {
                    sh """
                      cd dtp-backend
                       mvn clean 
                       mvn package -Dmaven.test.skip=true -P${project_env} -am -U
                       #ls target/*
                    """
                }
            }
        } 
        stage('docker build for creating image'){
            steps{
                container(name: 'docker') {
                    sh """
					
                    pwd
                    docker login -u ${HARBOR_USER_USR}  -p ${HARBOR_USER_PSW} ${HARBOR_ADDRESS}
					
                    echo "###########################################打包后端镜像开始##############################################"
					cd dtp-backend
					pwd
				

                    for item in  ${params.services}
                    do
                        echo "开始部署\${item}服务"
                    done
					
					echo "###########################################打包后端镜像结束##############################################"
                    """
                }
            }
        }
        stage('deploying to k8s'){
		    environment {
                MY_KUBECONFIG = credentials('k8s-admin')
            }
            steps {
                container(name: 'kubectl'){
                        echo "部署服务到 K8S 集群"
				        sh """
                         echo 'pwd'
                         pwd
						 /usr/local/bin/kubectl --kubeconfig ${MY_KUBECONFIG} get node
						 

                     """
                    
                }
            }
        }     
    }
    parameters {
        extendedChoice(value: 'dtp-admin,dtp-op-admin,dtp-api-gateway,dtp-worker,dtp-trade-admin,dtp-big-screen', description: '请选择需要发布的服务', multiSelectDelimiter: ',', name: 'services', quoteValue: false, type: 'PT_CHECKBOX', visibleItemCount: 15)

    }
    
    environment {
        BRANCH = "project-lvliang"
        COMMIT_ID = ""
        HARBOR_USER_USR = "admin"
        HARBOR_USER_PSW = "Harbor12345"
        HARBOR_ADDRESS = "192.168.6.20"
        REGISTRY_DIR = "lvliang"
        project_env = "dev"
        NAMESPACE = "lvliang"
        TAG = ""
        repository_url = 'http://192.168.2.18/chinadatapay/cdp-base.git'
        user_id = 'jluo@chinadatapay.com'
		env_dtp = "lvliang_dev"
    }
}

标签:#################,en,name,app,echo,循序渐进,jenkins,true
From: https://www.cnblogs.com/jluo/p/18040040

相关文章

  • 学习python自动化——Jenkins
    一、Jenkins概念持续集成,就是CI;持续交付,就是CDjenkins自动编译打包、自动部署二、持续集成的好处1.解放了重复性劳动2.更快的修复问题3.更快的交付成果4.减少手工的错误,人和机器最大的区别就是重复性动作上,人容易犯错,而机器错的几率几乎为零,所以在我们搭建完成集成服务器后......
  • 学习python自动化——pytest+allure+jenkins持续集成平台生成allure报告
    一、安装allure命令行工具具体安装过程查看:学习python自动化——pytest单元测试框架的2.4.4、生成allure的测试文件二、allure与pytest的集成在allure安装完成之后,需要与pytest集成,能够在pytest运行完成之后,生成allure的文件。1、安装pytest的allure支撑插件pipinstal......
  • Jenkins 远程触发 403 No valid crumb was included in the request
    Jenkins使用curl执行远程触发命令,会报403错误打开如下图  系统管理-》scriptConsole在下面脚本命令行中输入hudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION=true  查看-》系统管理-》全局安全配置 最后,请求成功 翻译搜......
  • Jenkins插件开发遇到的问题
    创建模板问题很多教程都是使用这个命令去创建Jenkins插件项目mvn-Uarchetype:generate-Dfilter=io.jenkins.archetypes:我使用这个命令,会报archetype不存在[WARNING]Noarchetypefoundinremotecatalog.Defaultingtointernalcatalog可以使用官方提供的非互动式......
  • Jenkins共享库使用
    简单使用共享库(Sharedlibraries)是一种可以用来封装函数、变量甚至整个Pipeline的机制。通过共享库,可以将常用的功能和流程逻辑定义在单独的Groovy脚本中,然后在多个Pipeline中重复使用,从而实现代码重用和统一管理比如说复用这个转成json的方法//tool.groovyimportgroov......
  • Jenkins流水线使用@Grab 导入Maven库
    有个需求需要在pipeline中调用Java的SDK去执行业务使用@Grab注解可以在Maven中导入Java库,@Grab('org.apache.commons:commons-math3:3.4.1')importorg.apache.commons.math3.primes.Primes引入依赖后,在通过import导入另外的写法@Grab(group='org.springframework',mod......
  • Jenkins下载插件报错
    只要看日志报了什么错下载超时更新代理源https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json报unabletofindvalidcertificationpathtorequestedtarget需要ssl认证,两种解决办法跳过ssl检查https://mirrors.tuna.tsinghua.edu.cn/jenkin......
  • Kubernetes配合Jenkins实现轻量自动持续集成
    Kubernetes配合Jenkins实现轻量自动持续集成原创 王先森sec 王先森Sec 2024-02-1913:16 北京 听全文这是一个分享运维,DevOps,安全等知识的微信公众号。王先森Sec王先森Sec分享运维,DevOps,安全等知识。23篇原创内容公众号背景介绍在当今的软件开......
  • Jenkins数据迁移
    Jenkins需要从一台主机换到另外一台主机怎么迁移数据两台主机的Jenkins版本需要保持一致安装Jenkins#sudowget-O/etc/yum.repos.d/jenkins.repohttps://pkg.jenkins.io/redhat-stable/jenkins.repo#sudorpm--importhttps://pkg.jenkins.io/redhat-stable/jenkins.i......
  • Jenkins构建提示docker命令权限问题解决方法
    参考:https://zhuanlan.zhihu.com/p/568513293使用Jenkins构建时使用的用户为jenkins在使用docker命令时会报以下错误ERROR:permissiondeniedwhiletryingtoconnecttotheDockerdaemonsocketatunix:///var/run/docker.sock:Get"http://%2Fvar%2Frun%2Fdocker.soc......