Skip to content

pipeline

得到所有分支(过滤)

Extended Choice Parameter -> Choose Source for Value

def getBranch = 'git ls-remote --refs git@git.dev.tencent.com:mirrorming/Test-Git.git'
getBranch.execute().text
        .readLines()
        .collect { it.split('/')[2] }
        .stream()
        .filter({ it -> !it.startsWith("temp") })
        .collect()
def getBranch = 'git ls-remote --heads git@git.dev.tencent.com:SZkexin/kexin-bnb.git'
getBranch.execute().text
        .readLines()
        .collect { it.split('refs/heads/')[1] }
        .stream()
        .filter({ it -> !it.startsWith("tempbranch") })
        .collect()

下次构建自动勾选上次启动参数

Extended Choice Parameter -> Choose Source for Default Value

def ver_keys = ['bash', '-c', 'cd /var/jenkins_home/workspace/test-groovy; git name-rev --name-only HEAD']
ver_keys.execute().text.tokenize('#')

流水线

node {
    environment {
        JENKINS_WORKSPACE="/var/jenkins_home/workspace"    //jenkins存放文件的地址
        PROJECT_NAME="${JOB_NAME}"                      // 项目名
        JAR_NAME="bnb-system-1.17.1-beta.jar"   // 项目生成的jar的名字
        IMAGE_NAME="bnb-system"                    // 镜像名一般和项目名相同
        VERSION_ID="${BUILD_ID}"
    }
    stage('拉取代码') {
        git 'git@git.dev.tencent.com:SZkexin/kexin-bnb.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: '/var/jenkins_home/gradle-4.5.1/bin/gradle build -x test'
        //sh label: '', script: 'chmod 777 gradlew'
        //sh label: '', script: './gradlew'
        
    }
    stage('推送并执行') {
        sshPublisher(publishers: [
        sshPublisherDesc(configName: '测试服内网', transfers: [
                sshTransfer(cleanRemote: false, excludes: '',
                        execCommand:
                                '''
                                    BUILD_ID=DONTKILLME
                                    cd /root/jenkins_docker/bnb/bnb-system/build/libs
                                    jarname=`ls | grep bnb-system | grep -v 'sources'| awk '{print $1}'`
                            
                                    mv /root/jenkins_docker/bnb/bnb-system/build/libs/${jarname} /root/jenkins_docker/bnb/docker/bnb-system.jar
                                    cd /root/jenkins_docker/bnb/docker
                                
                                    docker-compose up -d --build
                                    docker image prune -f
                                    
                                ''',
                        execTimeout: 120000,
                        flatten: false,
                        makeEmptyDirs: false,
                        noDefaultExcludes: false,
                        patternSeparator: '[, ]+',
                        remoteDirectory: 'bnb',
                        remoteDirectorySDF: false,
                        removePrefix: '',
                        sourceFiles: '**/bnb-system-*.jar,docker')],
                usePromotionTimestamp: false,
                useWorkspaceInPromotion: false,
                verbose: false)
        ])
        
        println "==========启动完成==========="
    }
}

附:

Dockerfile

FROM openjdk:8-jre
MAINTAINER mireal <mirrorming@qq.com>

RUN jarname=`ls | grep bnb-system | grep -v 'sources'| awk '{print $1}'`

RUN mkdir /app
COPY $jarname /app/app.jar

ENTRYPOINT ["java", "-jar","-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8060", "/app/app.jar", "--spring.profiles.active=test", "--kxFaceAuto=0", "--verifyFaceAuto=0", "--kexin.supervision.switch=1"]

EXPOSE 9090

docker-compose.yml

version: '3'
services:
  blog:
    build:
      context: .
      dockerfile: Dockerfile
    image: blog:1.0.0
    container_name: blog
    restart: always
    networks:
      - nets
    ports:
      - "8080:8080"
    environment:
      - JAVA_OPTS=-Xmx256M -Duser.timezone=GMT+08
networks:
  nets:
    external: false

杀掉Java进程命令

netstat -tunlp|grep 9090|awk '{print $7}'|awk -F '/' '{print $1}'|xargs kill -9

pipeline

node {
    environment {
        JENKINS_WORKSPACE = "/var/jenkins_home/workspace"    //jenkins存放文件的地址
        PROJECT_NAME = "${JOB_NAME}"                      // 项目名
        JAR_NAME = "bnb-system-1.17.1-beta.jar"   // 项目生成的jar的名字
        VERSION_ID = "${BUILD_ID}"
    }
    stage('拉取代码') {
        git 'git@git.dev.tencent.com:SZkexin/kexin-bnb.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 -f -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: '/var/jenkins_home/gradle-4.5.1/bin/gradle build -x test'
        //sh label: '', script: 'chmod 777 gradlew'
        //sh label: '', script: './gradlew'

    }
    stage('推送') {
        sshPublisher(publishers: [
                sshPublisherDesc(configName: '测试服内网', transfers: [
                        sshTransfer(cleanRemote: false, excludes: '',
                                execCommand:'''
#!/bin/bash
BUILD_ID=DONTKILLME
netstat -tunlp|grep 9090|awk '{print $7}'|awk -F '/' '{print $1}'
cd /root/jenkins_docker/bnb/bnb-system/build/libs
jarname=`ls | grep bnb-system | grep -v 'sources'| awk '{print $1}'`
mv /root/jenkins_docker/bnb/bnb-system/build/libs/${jarname} /root/jenkins_docker/bnb/bnb-system.jar
kill_id=`netstat -tunlp|grep 9090|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
kill_debug_id=`netstat -tunlp | grep 8060 | awk '{print $7}'|awk -F '/' '{print $1}'`
if [ -n "$kill_debug_id" ]
then
    echo "kill -9 kill_debug_id:" $kill_debug_id
    kill -9 $kill_debug_id
fi
sleep 1s
echo "==============应用启动==============" 
source /etc/profile
nohup java -jar -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8060 /root/jenkins_docker/bnb/bnb-system.jar  --spring.profiles.active=test --kxFaceAuto=0 --verifyFaceAuto=0 --sms.send=1 >> /root/jenkins_docker/bnb/bnb-system.log  &

sleep 1s
tail -f /root/jenkins_docker/bnb/bnb-system.log | sed '/com.zaxxer.hikari.HikariDataSource/Q'
''',
                                execTimeout: 120000,
                                flatten: false,
                                makeEmptyDirs: false,
                                noDefaultExcludes: false,
                                patternSeparator: '[, ]+',
                                remoteDirectory: 'bnb',
                                remoteDirectorySDF: false,
                                removePrefix: '',
                                sourceFiles: '**/bnb-system-*.jar')],
                        usePromotionTimestamp: false,
                        useWorkspaceInPromotion: false,
                        verbose: false)
        ])

        println "==========结束==========="
    }
}
node {
    environment {
        JENKINS_WORKSPACE = "/var/jenkins_home/workspace"    //jenkins存放文件的地址
        PROJECT_NAME = "${JOB_NAME}"                      // 项目名
        VERSION_ID = "${BUILD_ID}"
    }
    stage('拉取代码') {
        git 'git@git.dev.tencent.com:SZkexin/kexin-bnb.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 -f -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: '/var/jenkins_home/gradle-4.5.1/bin/gradle build -x test'
        //sh label: '', script: 'chmod 777 gradlew'
        //sh label: '', script: './gradlew'
        output = sh (script: "head -1 gradle.properties", returnStdout: true).trim()
   
        VERSION_NAME = output.split('=')[1]
        
        echo "从gradle读取的版本: ${VERSION_NAME}"

    }
    stage('推送') {
        def DEBUG_PORT = "$debug_port"
        def SMS = "$sms"
        def BNB_JAR_NAME = "bnb-system-"+VERSION_NAME+".jar"
        def BNB_NAME = BNB_JAR_NAME
        println BNB_NAME
        sshPublisher(publishers: [
                sshPublisherDesc(configName: '测试服内网', transfers: [
                        sshTransfer(cleanRemote: false, excludes: 'bnb-system-*-beta-sources.jar',
                                execCommand:'''
#!/bin/bash
BUILD_ID=DONTKILLME
netstat -tunlp | grep 9090 | awk '{print $7}'|awk -F '/' '{print $1}'

cd /root/jenkins_docker/bnb/bnb-system/build/libs
jarname=`ls -t | grep bnb-system | grep -v 'sources'| awk '{print $1}'|head -n 1`

mv /root/jenkins_docker/bnb/bnb-system/build/libs/${jarname} /root/jenkins_docker/bnb/bnb-system.jar
kill_id=`netstat -tunlp|grep 9090|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

kill_debug_id=`netstat -tunlp | grep 8060 | awk '{print $7}'|awk -F '/' '{print $1}'`
if [ -n "$kill_debug_id" ]
then
    echo "kill -9 kill_debug_id:" $kill_debug_id
    kill -9 $kill_debug_id
fi
sleep 1s

echo "==============应用启动 --> ${jarname} ==============" 
source /etc/profile
nohup java -jar -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${DEBUG_PORT} /root/jenkins_docker/bnb/bnb-system.jar  --spring.profiles.active=test --kxFaceAuto=0 --verifyFaceAuto=0 --sms.send=${SMS} >> /root/jenkins_docker/bnb/bnb-system.log  &

sleep 1s
tail -f /root/jenkins_docker/bnb/bnb-system.log | sed '/com.zaxxer.hikari.HikariDataSource/Q'
cd /root/jenkins_docker/bnb/bnb-system/build
rm -fr /libs/
''',
                                execTimeout: 120000,
                                flatten: false,
                                makeEmptyDirs: false,
                                noDefaultExcludes: false,
                                patternSeparator: '[, ]+',
                                remoteDirectory: 'bnb',
                                remoteDirectorySDF: false,
                                removePrefix: '',
                                sourceFiles: '**/bnb-system-*.jar')],
                        usePromotionTimestamp: false,
                        useWorkspaceInPromotion: false,
                        verbose: false)
        ])

        println "==========结束==========="
    }
}

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