tomcat--jenkins


pipeline {
    agent any

    options {
        disableConcurrentBuilds abortPrevious: true
    }

    //triggers {
    //   GenericTrigger(
    //       genericVariables: [
    //           [key: 'BRANCH', value: '$.ref'],
    //           [key: 'USEROFGIT', value: '$.user_name'],
    //       ],
    //       token: 'project:SAAS-DEV_CTP-CTMS',
    //       causeString: 'Triggered on $BRANCH',
    //       printContributedVariables: false,
    //       printPostContent: false
    //   )
    //}

    stages {
        stage('Checkout') {
            steps {
                
                script {
                   

                   env.Branchofgit = 'master'
                   env.UserName = 'manual-trigger'
                   sh "echo ${env.Branchofgit}"
                }


                checkout([
                    $class: 'GitSCM',
                    branches: [[name: "${env.Branchofgit}"]],
                    extensions: [
                        [$class: 'RelativeTargetDirectory', relativeTargetDir: 'ctp-version'],
                    ],
                    userRemoteConfigs: [[
                        credentialsId: '311297a3-4eb3-4204-a5e7-bd0e50295dc2',
                        url: 'https://gitlab2.runtrial.net/runtrial/service/ctms-4.6/ctp_version.git'
                    ]]
                ])


                checkout([
                    $class: 'GitSCM',
                    branches: [[name: "${env.Branchofgit}"]],
                    extensions: [
                        [$class: 'RelativeTargetDirectory', relativeTargetDir: 'ctp-api'],
                    ],
                    userRemoteConfigs: [[
                        credentialsId: '311297a3-4eb3-4204-a5e7-bd0e50295dc2',
                        url: 'https://gitlab2.runtrial.net/runtrial/service/ctms-4.6/ctp_api.git'
                    ]]
                ])




                checkout([
                    $class: 'GitSCM',
                    branches: [[name: "${env.Branchofgit}"]],
                    extensions: [
                        [$class: 'RelativeTargetDirectory', relativeTargetDir: 'ctp-dubbo'],
                    ],
                    userRemoteConfigs: [[
                        credentialsId: '311297a3-4eb3-4204-a5e7-bd0e50295dc2',
                        url: 'https://gitlab2.runtrial.net/runtrial/service/ctms-4.6/ctp_dubbo.git'
                    ]]
                ])





                git branch: "${env.Branchofgit}", credentialsId: '311297a3-4eb3-4204-a5e7-bd0e50295dc2', url: 'https://gitlab2.runtrial.net/runtrial/service/ctms-4.6/ctp.git'


                script {
                    env.GIT_COMMIT = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
                    println "${env.GIT_COMMIT}"
                    env.commitSubject = sh(script: "git log -1 --format='%s'", returnStdout: true).trim()
                    def tagOutput = sh(script: 'git tag --points-at HEAD', returnStdout: true).trim()
                    if (tagOutput) {
                        env.GIT_TAG = tagOutput
                        println "Current Git Tag: ${env.GIT_TAG}"
                    } else {
                        env.GIT_TAG = null
                        println "No Git tag found pointing at the current commit."
                    }
                    env.imagetag = "${env.BUILD_NUMBER}-${env.GIT_COMMIT}"
                }
            }

        }

        stage('Compile') { 
            agent {
                docker {
                    image 'harbor.runtrial.net:4443/public/maven:3.6.2-jdk-8-docker' 
                    args """
                        -v /root/.m2:/root/.m2
                        -v /home/jenkins/jenkins-data/workspace/${env.JOB_NAME}:/opt/maven
                    """.stripIndent()
                    reuseNode true
                }
            }
            steps {
                sh 'cd /opt/maven/ctp-version;mvn clean install -T 1C -U'
                sh 'cd /opt/maven/ctp-api;mvn clean install  -T 1C -U'
                sh 'cd /opt/maven/ctp-dubbo;mvn clean install  -T 1C -U'
                sh 'cd /opt/maven;mvn clean install  -T 1C -U'
            }
        }


        stage('parallel tasks'){
            steps{
                script{
                    parallel(
                       // 'SonarQube Scan': {
                       //     sh """
                       //         docker run \
                       //         --rm \
                       //         --cpus 2 \
                       //         --memory 4096m \
                       //         --privileged=true \
                       //         --user=root \
                       //         -v /opt/sonar-scanner:/opt/sonar-scanner/.sonar/cache \
                       //         -v /home/jenkins/jenkins-data/workspace/${env.JOB_NAME}:/usr/src \
                       //         -e SONAR_HOST_URL=http://192.168.0.199:9000 \
                       //         harbor.runtrial.net:4443/public/sonar-scanner-cli \
                       //         sonar-scanner \
                       //         -Dsonar.scanner.socketTimeout=1800 \
                       //         -Dsonar.scm.provider.timeout=1800 \
                       //         -Dsonar.java.binaries=**/target/classes \
                       //         -Dsonar.exclusions=**/target/*.jar,**/*.jar,**/*.war \
                       //         -Dsonar.projectKey=CTP-CTMS \
                       //         -Dsonar.sources=. \
                       //         -Dsonar.host.url=http://192.168.0.199:9000 \
                       //         -Dsonar.token=sqp_e4356cf27e4a302fb9334504c678d48333e5ea77
                       //     """
                       // },
                        'Build Docker Image & Deploy': {
                            sh "mkdir -p ./docker"
                            sh "cp -rf ${WORKSPACE}/ctp/target/ctp.war ./docker/ctp.war"

                             def ctms_dockerfileContent = '''
                                 FROM harbor.runtrial.net:4443/public/ctms-base:latest

                                 WORKDIR /usr/local/tomcat/bin
                                 ADD ctp.war .

                                 RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
                                     && apk add --no-cache unzip curl fontconfig ttf-dejavu bc  \
                                     && mkdir -p /home/ctms/app/ctms \
                                     && addgroup -g 1000 -S ctms && adduser -u 1000 -S -G  ctms ctms \
                                     && mkdir -p /home/ctms/file_server/ctp \
                                     && unzip /usr/local/tomcat/bin/ctp.war -d /home/ctms/app/ctms/ \
                                     && apk del unzip \
                                     && chown -R ctms /home/ \
                                     && chown -R ctms /usr/local/tomcat \
                                     && rm -rf /var/cache/apk/* \
                                     && rm -rf /usr/local/tomcat/bin/ctp.war

                                 USER ctms
                                 ENV MYSQL_VERSION=5.7
                                 ENV SSO=False

                                 EXPOSE 8080
                             '''
                             writeFile file: './docker/Dockerfile_CTMS', text: ctms_dockerfileContent

                            //sh "etcdctl  --endpoints 192.168.0.199:2379  get /LcKj/DockerBuilder/DockerFile/online_env/ctms  --print-value-only > ./docker/Dockerfile_CTMS"

                            sh "docker build -t ctms-dev:${env.imagetag} -f ./docker/Dockerfile_CTMS ./docker"
                            sh "bash -c \"source /root/venv/bin/activate; docker-squash ctms-dev:${env.imagetag} -t harbor.runtrial.net:4443/test_env/ctms-dev:${env.imagetag}\""
                            sh "docker push harbor.runtrial.net:4443/test_env/ctms-dev:${env.imagetag}"
                            sh "echo ${env.UserName}"                 
  	                     
  	                        
  	                        wjw_api_deploy_aliyun_dev()
                        },
                        failFast: true 
                    )
                }
            }
        }
    }
}


// 卫健委wjw-api发布阿里云测试环境-172.21.219.55上 
def wjw_api_deploy_aliyun_dev() {
    def remote = [:]
  	remote.name = 'jumphost'
  	remote.host = '47.103.138.68'
  	remote.port = 12721
    remote.allowAnyHosts = true
    withCredentials([usernamePassword(credentialsId: '47.103.138.68', passwordVariable: 'Jumppassword', usernameVariable: 'Jumpusername')]) {
        remote.user = "${Jumpusername}"
        remote.password = "${Jumppassword}"
        withCredentials([usernamePassword(credentialsId: '172.21.219.55', passwordVariable: 'password', usernameVariable: 'username')]) {
            try {
                sshCommand remote: remote, command: """
                    sshpass -p '${password}' ssh -o StrictHostKeyChecking=no -p 22 ${username}@172.21.219.55 '''
                        sed -i \"s#harbor.runtrial.net:4443/test_env/ctms-dev.*#harbor.runtrial.net:4443/test_env/ctms-dev:${env.imagetag}#g\" /home/swarm/wjw-api/wjw_api.yml
                        docker compose -f /home/swarm/wjw-api/wjw_api.yml up -d --no-deps wjw-api
                    '''
                """
            } catch (Exception e) {
                error("Failed to access or use credential: ${e.getMessage().replace(password, '[MASKED]')}")
            }
        } 
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值