jenkins使用pipeline部署maven项目(简单版)

直接使用shell命令打包、上传、部署,流水线脚本如下:

前置步骤:

  1. jenkins的全局工具配置中配置安装maven,name为M3,后面pipeline脚本中需要用到,如图:
    jenkins使用pipeline部署maven项目(简单版)_第1张图片

  2. 在jenkins的Manage Credentials种创建个gitee的凭据gitee_user,如图:
    jenkins使用pipeline部署maven项目(简单版)_第2张图片

  3. 为了把jar包传到服务器上,要配置两台服务器的免密登录,参考如下连接:
    https://blog.csdn.net/u013415591/article/details/81943189

然后就是流水线任务中的代码:

pipeline {
    agent any

    tools {
        // Install the Maven version configured as "M3" and add it to the path.
        maven "M3"
    }

    stages {
        stage('Build') {
            steps {
                // Get some code from a GitHub repository
                git credentialsId: 'gitee_user', url: 'https://gitee.com/gitee_project.git'

                // Run Maven on a Unix agent.
                sh "mvn -Dmaven.test.failure.ignore=true clean package"
               
                sh 'scp $WORKSPACE/child_project/target/xxx.jar root@ip:/jar_dir_path/'
                sh 'ssh root@ip "/script_path/deploy.sh"'

            }

            post {
                // If Maven was able to run the tests, even if some of the test
                // failed, record the test results and archive the jar file.
                success {
                    archiveArtifacts 'child_project/target/*.jar'
                }
            }
        }
    }
}

你可能感兴趣的:(maven,java,ssh)