Skip to content

jenkins流水线脚本

node{
    stage('get clone'){
       git url: 'ssh://git@192.168.0.55:2222/pocket/pocket-order-management.git'
    }

    //定义mvn环境
    def mvnHome = tool 'M3'
    env.PATH = "${mvnHome}/bin:${env.PATH}"

    stage('mvn test'){
        //mvn 测试
        sh "mvn test"
    }

    stage('mvn build'){
        //mvn构建
        sh "mvn clean install -Dmaven.test.skip=true"
    }

    stage('deploy'){
        //执行部署脚本
        echo "正在部署到服务器 ......"
        sshPublisher(
			publishers: [sshPublisherDesc(
				configName: '32g - yun - 120.25.165.220', 
				transfers: [sshTransfer(
					cleanRemote: false, 
					excludes: '', 
					execCommand: '''
						cd /home/mireal/pocket/pocket-order-management/target
						kill -9 `ps -ef | grep pocket-order-management | grep -v grep | awk \'{print $2}\'`
						source /etc/profile
						echo "---------------------正在启动应用程序---------------------"
						nohup java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=0.0.0.0:${debug_port} -jar pocket-order-management-1.0.0-SNAPSHOT.jar &
					''', 
					execTimeout: 120000, 
					flatten: false, 
					makeEmptyDirs: false, 
					noDefaultExcludes: false, 
					patternSeparator: '[, ]+', 
					remoteDirectory: 'pocket-order-management', 
					remoteDirectorySDF: false, 
					removePrefix: '', 
					sourceFiles: '**/*.jar')], 
				usePromotionTimestamp: false, 
				useWorkspaceInPromotion: false, 
				verbose: false
			)])
    }
}
node {
   stage('git') { 
      git branch: '${mybranch}', url: 'git@git.dev.tencent.com:SZkexin/kexin-bnb.git'
   }
   stage('Build') {
      sh label: '', script: '/var/jenkins_home/gradle-4.5/bin/gradle build -x test'
   }
   stage('Results') {
      echo 'ok'
   }
}

分支选择

extend choice parameter

'git ls-remote --refs git@git.dev.tencent.com:mirrorming/Test-Git.git'.execute().text.readLines().collect { it.split('/')[2] }

合并分支

node {

    stage('拉取代码') {
        git 'git@git.dev.tencent.com:mirrorming/Test-Git.git'
    }
    stage('合并分支') {

        def SEL_BRANCHS = "$branchs"
        println SEL_BRANCHS
        
        def tempBranchName = "temp#"
        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-5.0/bin/gradle build -x test'
        sh "git branch -D ${tempBranchName}"
    }
    stage('推送并执行') {
        sshPublisher(publishers: [
                sshPublisherDesc(
                        configName: 'localdeploy',
                        transfers: [sshTransfer(cleanRemote: false, excludes: '',
                                execCommand:
                                        '''
                                        BUILD_ID=DONTKILLME
                                        cd /usr/local/jenkins/pipeline-demo/
                                        cp target/bnb-0.0.1-SNAPSHOT.jar docker
                                        
                                        cd docker
                                        docker-compose down
                                        docker-compose build
                                        docker-compose up -d
                                        ''',
                                execTimeout: 120000,
                                flatten: false, makeEmptyDirs: false, noDefaultExcludes: false,
                                patternSeparator: '[, ]+', remoteDirectory: 'pipeline-demo',
                                remoteDirectorySDF: false, removePrefix: '', sourceFiles: '**/*.jar,docker')],
                        usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
    }
}

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