Jenkins构建项目的几种方式
参考简书很有用的一篇文章
Jenkins简单的PipeLine流水线脚本
Windows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| pipeline { agent any stages { stage('Pull code') { steps { checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '4f77bfb0-49cc-4359-9b4a-99b190e5c92b', url: 'https://gitee.com/chuenhung/export-java-bean.git']]]) } } stage('Build project') { steps { bat "mvn clean package" archiveArtifacts '**/target/*.jar' } } } }
|
Linux:
注意:pipeline、stages、stage、steps、checkout…这些词不能改
构建遇到问题:java.lang.NoSuchMethodError: No such DSL method ‘pipeline’ found among steps [bat, build, checkout, container, containerLog, input, junit, library, libraryResource, load, milestone, node, parallel, podTemplate, powershell, properties
解决方法:成功安装PipeLine插件。
Jenkins项目构建细节(定时构建、参数化构建等)
参考链接
Jenkins远程部署Jar包(非流水线方式)
安装Publish Over SSH 插件
配置Publish Over SSH
点击Manage Jenkins->Configure System
找到Publish over SSH->修改用户密码
填写服务名、服务器ip、用户名、服务器文件夹等信息
填写完成后,点击Test Configuration 返回Success说明配置没问题。
项目构建配置
在对应构建的项目配置中(非PipeLine),Post Steps选择Send files or execute commands over SSH
填写下面的配置:
其中,Remote directory是在之前的 Publish over SSH配置追加。比如之前配置了/usr/local/testJenkins/,这里填写/test/upload,最终上传的路径就会变成/usr/local/testJenkins/test/upload
最后,重新构建,看日志等着构建成功就好。这里传输如果文件过大速度会比较慢。
参考链接1、参考链接2
Jenkins远程部署Jar包(流水线方式)
测试通过的脚本如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| def CONFIG_NAME = "LocalCentos" def SOURCE_FILES = "**/target/*.jar" def REMOTE_DIRECTORY = "" def REMOVE_PREFIX = "/target"
pipeline { agent any stages { stage('Pull code') { steps { checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '4f77bfb0-49cc-4359-9b4a-99b190e5c92b', url: 'https://gitee.com/chuenhung/export-java-bean.git']]]) } } stage('Build project') { steps { bat "mvn clean package" archiveArtifacts '**/target/*.jar' } } stage('Deploy project') { steps { sshPublisher( failOnError: false, publishers: [ sshPublisherDesc( configName: "${CONFIG_NAME}", transfers: [ sshTransfer( sourceFiles: "${SOURCE_FILES}", remoteDirectory: "${REMOTE_DIRECTORY}", removePrefix: "", flatten: true, execCommand: "", execTimeout: 1800000 ) ], sshRetry: [ retries: 0 ] ) ] ) } } } }
|
参考链接