首页 > 其他分享 >jdk后端的流水线pipline案例

jdk后端的流水线pipline案例

时间:2023-08-24 09:22:58浏览次数:36  
标签:en name jdk IMAGE TAG env 提测 流水线 pipline

pipeline {
  agent {
    kubernetes {
      cloud 'kubernetes-dev'
      slaveConnectTimeout 1200
      workspaceVolume hostPathWorkspaceVolume(hostPath: "/opt/jenkins/workspace", readOnly: false)
      yaml '''
apiVersion: v1
kind: Pod
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: node-role.kubernetes.io/edge
            operator: DoesNotExist
          - key: build
            operator: In
            values:
            - "true"
  tolerations:
  - key: "groups"
    operator: "Equal"
    effect: "NoExecute"
    value: "dmp"
  containers:
  - name: jnlp
    image: \'10.1.40.69/citools/jnlp:alpine\'
    imagePullPolicy: IfNotPresent
    args: [\'$(JENKINS_SECRET)\', \'$(JENKINS_NAME)\']
    volumeMounts:
    - mountPath: "/etc/localtime"
      name: "localtime"
      readOnly: false
  - name: "sonar-scanner"
    image: "10.1.40.69/citools/sonar-scanner-cli:4.7"
    imagePullPolicy: "IfNotPresent"
    command:
    - "cat"
    env:
    - name: "LANGUAGE"
      value: "en_US:en"
    - name: "LC_ALL"
      value: "en_US.UTF-8"
    - name: "LANG"
      value: "en_US.UTF-8"
    - name: "SONAR_HOST_URL"
      value: "http://10.1.40.51:9000"
    tty: true
    volumeMounts:
    - mountPath: "/etc/localtime"
      name: "localtime"
  - name: "build"
    image: "10.1.40.69/citools/maven:3.6.3-jdk-8"
    imagePullPolicy: "IfNotPresent"
    command:
    - "cat"
    tty: true
    volumeMounts:
    - mountPath: "/etc/localtime"
      name: "localtime"
    - mountPath: "/root/.m2/"
      name: "cachedir"
      readOnly: false
  - name: "kubectl"
    image: "10.1.40.69/citools/kubectl:self-1.17"
    imagePullPolicy: "IfNotPresent"
    command:
    - "cat"
    env:
    - name: "LANGUAGE"
      value: "en_US:en"
    - name: "LC_ALL"
      value: "en_US.UTF-8"
    - name: "LANG"
      value: "en_US.UTF-8"
    tty: true
    volumeMounts:
    - mountPath: "/etc/localtime"
      name: "localtime"
      readOnly: false
  - name: "docker"
    image: "10.1.40.69/citools/docker:19.03.9-git"
    imagePullPolicy: "IfNotPresent"
    command:
    - "cat"
    env:
    - name: "LANGUAGE"
      value: "en_US:en"
    - name: "LC_ALL"
      value: "en_US.UTF-8"
    - name: "LANG"
      value: "en_US.UTF-8"
    tty: true
    volumeMounts:
    - mountPath: "/etc/localtime"
      name: "localtime"
      readOnly: false
    - mountPath: "/var/run/docker.sock"
      name: "dockersock"
      readOnly: false
    - mountPath: /opt/jenkins/workspace
      name: "jenkins-workspace"
      readOnly: false
  - name: "alpine-curl"
    image: "10.1.40.69/publics/alpine-curl:latest"
    imagePullPolicy: "IfNotPresent"
    command:
    - "/bin/sh"
    - "-c"
    - "sleep 1200"
    env:
    - name: "LANGUAGE"
      value: "en_US:en"
    - name: "LC_ALL"
      value: "en_US.UTF-8"
    - name: "LANG"
      value: "en_US.UTF-8"
    volumeMounts:
    - mountPath: /opt/jenkins/workspace
      name: "jenkins-workspace"
      readOnly: false
    - mountPath: "/etc/localtime"
      name: "localtime"
      readOnly: true
  restartPolicy: "Never"
  volumes:
  - hostPath:
      path: "/var/run/docker.sock"
    name: "dockersock"
  - hostPath:
      path: "/usr/share/zoneinfo/Asia/Shanghai"
    name: "localtime"
  - name: "cachedir" 
    hostPath:
      path: "/opt/m2"
  - name: "jenkins-workspace"
    hostPath:
      path: "/opt/jenkins/workspace"
'''
    }
  }

  environment {
    TAG = ''
    COMMIT_ID = ''
    HARBOR_ADDRESS = '10.1.40.69'
    HARBOR_USER = credentials('HARBOR_ACCOUNT')
    REGISTRY_DIR = 'dmp'
    IMAGE_NAME = 'dmp-auth'
    NAMESPACE = 'dmp-dev'
    MY_KUBECONFIG = credentials('kubernetes-dev')
    REPO = 'ssh://[email protected]:22022/dmpgroup/dmp-framework.git'
    REPO_HTTP = 'http://gitlab.xxx:9002/dmpgroup/dmp-framework.git'
    PROJECT_DIR = 'dmp-auth'
    PROJECT_ROOT = 'dmp-auth'
    GIT_AUTH = 'gitlab-key'
    PROJECT_NAME = 'dmp-auth'
    SOURCES_DIR = '.'
    LANGUAGE = 'java'
  }
  
  parameters {
    gitParameter(
      branch: '',
      branchFilter: 'origin/(.*)', 
      defaultValue: 'dev', 
      description: 'Branch for build and deploy', 
      name: 'BRANCH', 
      quickFilterEnabled: false, 
      selectedValue: 'NONE', 
      sortMode: 'NONE', 
      tagFilter: '*', 
      type: 'PT_BRANCH'
    )
    imageTag(
      name: "IMAGE_TAG", 
      description: '需要提测的镜像版本,构建发版开发环境时请忽略该参数',
      image: "dmp/dmp-auth", 
      filter: '.*', 
      defaultTag: '',
      registry: 'http://10.1.40.69', 
      credentialId: 'HARBOR_ACCOUNT', 
      tagOrder: 'DSC_VERSION'
    )
    choice(
      name: 'ACTION',
      description: '执行动作: \nbuild: 只构建镜像,不发版\ndeploy: 构建镜像并发布更新到开发环境\nsummit_the_test: 提交测试申请',
      choices: ['deploy', 'build', 'summit_the_test']
    )
    booleanParam(
      name:'SCANNER',
      defaultValue: false, 
      description: '是否执行代码扫描'
    )
    text(
      defaultValue: '''此次提测需要注意以下事项:''', 
      description: '提测信息说明', 
      name: 'COMMENT'
    )
  }

  stages {
    stage('Pulling Code') {
      when {
        expression { params.ACTION ==~ /(build|deploy)/ }
      }
      parallel {
        stage('Pulling Code by Jenkins') {
          when {
            expression {
              env.gitlabBranch == null
            }
          }
          steps {
            git(
              url: "${env.REPO}", 
              changelog: true, 
              poll: true, 
              branch: "${BRANCH}", 
              credentialsId: "${env.GIT_AUTH}"
            )
            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('Pulling Code by trigger') {
          when {
            expression {
              env.gitlabBranch != null
            }
          }
          steps {
            git(
              url: "${env.REPO}", 
              branch: env.gitlabBranch, 
              changelog: true, 
              poll: true, 
              credentialsId: "${env.GIT_AUTH}"
            )
            script {
              COMMIT_ID = sh(
                returnStdout: true,
                script: "git log -n 1 --pretty=format:'%h'"
              ).trim()
              TAG = BUILD_TAG + '-' + COMMIT_ID
              println "Current branch is ${env.gitlabBranch}, Commit ID is ${COMMIT_ID}, Image TAG is ${TAG}"
            }
          }
        }

      }
    }

    stage('Building') {
      when {
        expression { params.ACTION ==~ /(build|deploy)/ }
      }
      steps {
        container(name: 'build') {
          sh '''
            cd ${PROJECT_ROOT}
            mvn clean package -U -B -DskipTests=true
          '''
        }

      }
    }

    stage('SonarQube analysis') {
      environment {
        SONAR_TOKEN = credentials('sonar-token')
      }
      when {
        expression { return params.SCANNER }
      }

      steps {
        container("sonar-scanner") {
          sh '''
            sonar-scanner \
              -Dsonar.login=${SONAR_TOKEN} \
              -Dsonar.projectKey=${PROJECT_NAME} \
              -Dsonar.projectName=${PROJECT_NAME} \
              -Dsonar.sourceEncoding=UTF-8 \
              -Dsonar.language=${LANGUAGE} \
              -Dsonar.sources=${SOURCES_DIR} \
              -Dsonar.java.binaries=.
          '''
        }
      }
    }

    stage('Docker build for creating image') {
      when {
        expression { params.ACTION ==~ /(build|deploy)/ }
      }
      steps {
        container(name: 'docker') {
          sh """
            cd ${PROJECT_DIR}
            echo ${HARBOR_USER_USR} ${HARBOR_USER_PSW} ${TAG}
            docker build -t ${HARBOR_ADDRESS}/${REGISTRY_DIR}/${IMAGE_NAME}:${TAG} .
            docker login -u ${HARBOR_USER_USR} -p ${HARBOR_USER_PSW} ${HARBOR_ADDRESS}
            docker push ${HARBOR_ADDRESS}/${REGISTRY_DIR}/${IMAGE_NAME}:${TAG}
          """
        }
      }
    }

    stage('Deploying to K8s') {
      when {
        expression { params.ACTION == 'deploy' }
      }
      steps {
        container(name: 'kubectl') {
          sh """
            /usr/local/bin/kubectl --kubeconfig ${MY_KUBECONFIG} \
              set image deploy -l \
              app=${IMAGE_NAME} \
              ${IMAGE_NAME}=${HARBOR_ADDRESS}/${REGISTRY_DIR}/${IMAGE_NAME}:${TAG} \
              -n $NAMESPACE --record
            /usr/local/bin/kubectl --kubeconfig=${MY_KUBECONFIG} \
              get pod -l app=${IMAGE_NAME} \
              -n ${NAMESPACE} -w
          """
        }
      }
    }

    stage('提交到测试') {
      when {
        expression { params.ACTION == 'summit_the_test' }
      }

      steps {
        container(name: 'alpine-curl') {
          wrap([$class: 'BuildUser']) {
            script {
              BUILD_USER = "${env.BUILD_USER}"
              IMAGE_TAG = "${params.IMAGE_TAG}"
              BUILD_TIMESTAMP = "${env.BUILD_TIMESTAMP}"
              sh """
                echo "发送提测消息到企业微信."
                curl 'https://xxxx71-4916-a932-db465bc80c1b' -H 'Content-Type: application/json' -d '
                {
                  "msgtype": "markdown",
                  "markdown": {
                    "content": "`项目 <font color=\'info\'>${IMAGE_NAME}</font> 已经提测,请相关同事注意。`\n>
                               > 提测项目名称: <font color=\'info\'>${IMAGE_NAME}</font>
                               > 提测代码仓库: [${IMAGE_NAME}](${env.REPO_HTTP})
                               > 提测代码分支: <font color=\'warning\'>${BRANCH}</font>
                               > 提测镜像版本: ${IMAGE_TAG}
                               > 提测镜像标签: <font color=\'warning\'>${IMAGE_TAG_TAG}</font>
                               > 提测镜像名称: <font color=\'warning\'>${IMAGE_TAG_IMAGE}</font>
                               > 提测申请人: <font color=\'info\'>${BUILD_USER}</font>
                               > 提测申请时间: <font color=\'comment\'>${BUILD_TIMESTAMP}</font>
                               > 测试环境部署: 确保nacos配置已经同步,请点击:[发布测试环境](http://10.1.30.91:8080/)
                               > 提测注释: ${params.COMMENT}"
                  }
                }'
              """
            }
          }
        }
      }
    }
  }
}

标签:en,name,jdk,IMAGE,TAG,env,提测,流水线,pipline
From: https://www.cnblogs.com/zoujiaojiao/p/17653268.html

相关文章

  • JDK8 LocalDateTime 自定义周开始
    LocalDateTimenow=LocalDateTime.of(2022,12,31,0,1,1);WeekFieldsweekFields=WeekFields.of(DayOfWeek.FRIDAY,1);intdayOfWeek=now.getDayOfWeek().get(weekFields.dayOfWeek());System.out.println("日期:"+......
  • JDK1.8使用sonarqube踩坑
    最近在公司使用sonarqube来扫描Java代码,并使用插件;网上的资料缺少这一块的,总结一下:环境信息JDK1.8;sonarqube7.8;(只能用这个版本)插件:使用了阿里的p3c和findbugs4.0.0;这些版本很重要,谁踩坑最多的地方;踩坑网上都说findbug要用3.10,我用了,但报错搞了我好几天报错:org.sonarsource.sc......
  • 掌握JDK21全新结构化并发编程,轻松提升开发效率!
    1概要通过引入结构化并发编程的API,简化并发编程。结构化并发将在不同线程中运行的相关任务组视为单个工作单元,从而简化错误处理和取消操作,提高可靠性,并增强可观察性。这是一个预览版的API。2历史结构化并发是由JEP428提出的,并在JDK19中作为孵化API发布。它在JDK20中被JEP4......
  • JDK8的卸载与安装
    安装jdk百度搜索JDK8,找到下载地址同意协议下载电脑对应版本双击安装JDK8记住安装的路径配置环境变量1.我的电脑-右键-属性2.环境变量-新建3.配置path变量1.点击path2.打开后点击新建后确定测试JDK是否安装成功1.打开cmd2.java-version......
  • 本地nacos启动报错: Please set the JAVA_HOME variable in your environment, We nee
    编辑startup.cmd文件将模式从cluster改为standalone插入一行指定你的JAVA_HOME路径setJAVA_HOME="C:\dev_files\jdk17"然后启动nacos即可~......
  • 软件安装_安装JDK
           ......
  • centos7 jdk1.8安装教程
    使用root用户安装jdk8下载参考,下载jdk-8u161-linux-x64.tar.gzhttps://blog.csdn.net/qq_21187515/article/details/84850814在/usr/目录下创建java目录#mkdir/usr/java#cd/usr/java4.把下载的jdk-8u161-linux-x64.tar.gz上传到linux上的/us......
  • 运行 'RuoYiApplication' 时出错: 模块 'ruoyi-admin' 没有 JDK
    项目运行良好,关机重新打开项目时出现“运行'RuoYiApplication'时出错:模块'ruoyi-admin'没有JDK”导致运行失败,经检查项目中已经配置SDK解决办法:重新加载Maven,或者直接点项目右侧的刷新按钮  ......
  • linux 安装jdk
    (1)下载安装包JDK安装包下载地址:链接:https://pan.baidu.com/s/1Vt0nqNBOVl7Chuap4Gj3tg提取码:9fy2(2)SSH上传工具SSH安装包下载地址:链接:https://pan.baidu.com/s/1TiFqxSJbmxupcjSHmcLLvQ提取码:tbo9(3)安装SSH成功可直接右键,点击【Upload】上传到/usr/local/java目录......
  • 从JDK源码级别彻底剖析JVM类加载机制
    类加载运行全过程当我们用java命令运行某个类的main函数启动程序时,首先需要通过类加载器把主类加载到JVM。publicclassMath{publicstaticfinalintinitData=666;publicstaticUseruser=newUser();publicintcompute(){//一个方法对应一块栈帧......