Skip to content

docker-compose.yml

version: '3.1'
services:
  jenkins:
    restart: always
    image: jenkins/jenkins
    container_name: jenkins
    ports:
      - 8080:8080
      - 50000:50000
    environment:
      TZ: Asia/Shanghai
      JAVA_OPTS: "-Dhudson.model.DownloadService.noSignatureCheck=true"
    volumes:
      - ./data:/var/jenkins_home

手动新建 data 目录

mkdir data

修改data 目录权限

chmod 777 data

启动:docker-compose up -d

启动之后,打开Jenkins:http://192.168.1.210:8080/

进入Jenkins容器获取密码

docker exec -it -u root jenkins  bash

cat /var/jenkins_home/secrets/initialAdminPassword

输入密码之后,打开另一个浏览器窗口: http://192.168.1.210:8080/pluginManager/advanced

配置:Update Site 为

https://jenkins-update.davidz.cn/update-center.json

脚本

node {
    environment {
        JENKINS_WORKSPACE="/var/jenkins_home/workspace"    //jenkins存放文件的地址
        PROJECT_NAME="${JOB_NAME}"                      // 项目名
        JAR_NAME="xxx.jar"   // 项目生成的jar的名字
        IMAGE_NAME="bnb-system"                    // 镜像名一般和项目名相同
        VERSION_ID="${BUILD_ID}"
    }
    stage('拉取代码') {
        git 'git@code.aliyun.com:gryo-card-new/tuoluo-ecard-enterprise.git'
    }
    
    stage('合并分支') {

        def SEL_BRANCHS = "$branchs"
        println SEL_BRANCHS
        
        def tempBranchName = "tempbranch"
        def asList = Arrays.asList(branchs.split(","))
        ArrayList allBranchs = new ArrayList<String>(asList);
        allBranchs.each { branch -> tempBranchName = tempBranchName.concat("#").concat(branch) }
        tempBranchName = tempBranchName.concat("#").concat(System.currentTimeMillis().toString().substring(10))
        
        println "选择的分支为:" + allBranchs
        def firstBranchName = allBranchs.remove(0)
        println "选择第一支分支:" + firstBranchName
        println "临时分支名为:" + tempBranchName


        sh "git checkout -b ${tempBranchName} origin/${firstBranchName}"
        println "还需要融合的分支有:" + allBranchs

        allBranchs.each { branch ->
            sh "git merge origin/${branch} -m 'merge'"
        }

        //def pushCommand = "git push --set-upstream origin $tempBranchName"
        //sh "git push --set-upstream origin ${tempBranchName}"

        println "==========ok==========="
        
 
    }
    stage('编译打包') {
        sh label: '', script: 'cd card-enterprise-crm-api'
        sh label: '', script: '/var/jenkins_home/env/maven/bin/mvn package -Dmaven.test.skip=true'

        //sh label: '', script: 'chmod 777 gradlew'
        //sh label: '', script: './gradlew'
        
    }
    stage('推送并执行') {
        sshPublisher(publishers: [
        sshPublisherDesc(configName: 'local', transfers: [
                sshTransfer(cleanRemote: false, excludes: '',
                        execCommand:
                                '''

#!/bin/bash
BUILD_ID=DONTKILLME
netstat -tunlp | grep 8093 | awk '{print $7}'|awk -F '/' '{print $1}'

cd /var/jenkins/bixiaoweb/card-enterprise-crm-api/target
jarname=`ls -t | grep card-enterprise-crm | grep -v 'sources'| awk '{print $1}'|head -n 1`

kill_id=`netstat -tunlp|grep 8093|awk '{print $7}'|awk -F '/' '{print $1}'`

if [ -n "$kill_id" ]
then
    echo "kill -9 的pid:" $kill_id
    kill -9 $kill_id
fi
sleep 1s

echo "==============应用启动 --> ${jarname} ==============" 

source /etc/profile
nohup java -jar -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=28093 /var/jenkins/bixiaoweb/card-enterprise-crm-api/target/${jarname}  --spring.profiles.active=test >> /var/jenkins/bixiaoweb/card-enterprise-crm-api/target/out.log  &

sleep 1s
tail -f /var/jenkins/bixiaoweb/card-enterprise-crm-api/target/out.log | sed '/com.alibaba.druid.pool.DruidDataSource/Q'

                                    
                                ''',
                        execTimeout: 120000,
                        flatten: false,
                        makeEmptyDirs: false,
                        noDefaultExcludes: false,
                        patternSeparator: '[, ]+',
                        remoteDirectory: '',
                        remoteDirectorySDF: false,
                        removePrefix: '',
                        sourceFiles: '**/card-enterprise-crm-api-*.jar')],
                usePromotionTimestamp: false,
                useWorkspaceInPromotion: false,
                verbose: false)
        ])
        
        println "==========启动完成==========="
    }
}

文章来源于自己总结和网络转载,内容如有任何问题,请大佬斧正!联系我