jenkins 流水线参数化构建例子

前面我介绍了jenkins的安装和基础配置,这一篇我来说一下怎么通过流水线参数化自动部署;

一、新建任务

新建任务 -> 流水线

jenkins 流水线参数化构建例子_第1张图片

二.General

1.勾选参数化构建过程

2.添加参数
jenkins 流水线参数化构建例子_第2张图片

三、 高级项目选项

不需要填

四、 流水线

  1. 选择Pipeline script

  2. 下面是我构建用到的脚本,大家可以参考着改,因为我只有一台服务器,所有没有用到ssh,其中脚本里的参数service_names和project_version就是通过构建时传进去的。

pipeline{
        agentany

        environment {
        git_addr = "填写自己的git项目地址"
        target_dir = "/root/data/buildings/jenkins/exec"
        target_ssh = "[email protected]"
        backup_dir = "/root/data/buildings/jenkins/backup"
        get_branch = "$project_version"
        }

        stages {
        stage('获取代码') {
            steps {
                echo "start fetch code from git:${git_addr} branch:$get_branch"
                sh "pwd"
                deleteDir()
                gitbranch:
                "$get_branch", url:"${git_addr}"
            }
        }

        stage('单元测试') {
            when {
                environment name:'is_test', value:'是'
            }
            steps {
                echo "start test"
                sh "mvn clean test"
                junit '**/target/surefire-reports/TEST-*.xml'
            }
        }

        stage('打包') {
            steps {
                echo "start package"
                sh "java -version&& mvn -v"
                sh "mvn clean package -Dmaven.test.skip=true"
            }
        }

        stage('部署') {
            steps {
                echo "start deploy"

                sh '' '
                if [!-d "${backup_dir}/${project_version}" ];
                then
                mkdir - p $ {
                    backup_dir
                }/$ {
                    project_version
                }
                fi
                '' '


                script {
                    def apps = "${service_names}".split(",")
                    for (int i = 0; i < apps.size(); ++i) {
                        sh "rm -f${backup_dir}/${project_version}/${apps[i]}-0.0.1-SNAPSHOT.jar"
                        sh "cptarget/${apps[i]}-0.0.1-SNAPSHOT.jar ${backup_dir}/${project_version}/"

                        sh "rm -f${target_dir}/${apps[i]}-0.0.1-SNAPSHOT.jar"
                        sh "cptarget/${apps[i]}-0.0.1-SNAPSHOT.jar ${target_dir}"
                        //我用的是本地所以不用目标ip
                        //sh "ssh ${target_ssh} rm -f${target_dir}/${apps[i]}.jar"
                        //sh "scptarget/${apps[i]}-0.0.1-SNAPSHOT.jar ${target_ssh}:${target_dir}"
                    }
                }
            }
        }

        stage('重启') {
            steps {
                echo "start restart"
                script {
                    def apps = "${service_names}".split(",")
                    for (int i = 0; i < apps.size(); ++i) {
                        sh "cd ${target_dir};sh shutdown.sh ${apps[i]}-0.0.1-SNAPSHOT"
                        sleep 30
                        sh "cd${target_dir};sh start.sh ${apps[i]}-0.0.1-SNAPSHOT"
                        //我用的是本地所以不用目标ip
                        //sh "ssh${target_ssh} 'cd ${target_dir};sh shutdown.sh ${apps[i]}'"
                        //sleep 30
                        //sh "ssh${target_ssh} 'cd ${target_dir};sh start.sh ${apps[i]}'"
                    }
                }
            }
        }
    }

    post {
        always {
            echo '构建结束...'
        }
        success {
            echo '恭喜您,构建成功!!!'
        }
        failure {
            echo '抱歉,构建失败!!!'
        }
        unstable {
            echo '该任务已经被标记为不稳定任务....'
        }
        changed {
            echo ''
        }
    }
}

目标服务的shutdown.sh内容:

    jenkins-0.0.1-SNAPSHOT.jar  shutdown.sh start.sh
    pid=`ps ax | grep -i $1 |grep java | grep-v grep | awk '{print $1}'`
    if [ -z "$pid" ] ; then
       echo "No $1 running."
    fi
    echo "The $1(${pid}) isrunning..."
    kill ${pid}
    echo "Send shutdown request to $1(${pid})OK"

目标服务的 start.sh 内容为:

nohup java -jar -Xmx256M -Xms256M $1.jar>> /dev/null 2>&1 &

到此就全部配置完成了,我们就可以构建项目了;
1.点击 build
jenkins 流水线参数化构建例子_第3张图片

下面我标记的就是我们设置的构建参数

jenkins 流水线参数化构建例子_第4张图片

点击开始构建,就会出现构建的进度

jenkins 流水线参数化构建例子_第5张图片

构建成功是蓝色的,失败是红色的

jenkins 流水线参数化构建例子_第6张图片

你可能感兴趣的:(java,开发工具,java,jenkins)