Windows:
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:
把bat改为sh
注意: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插件。
点击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
测试通过的脚本如下:
def CONFIG_NAME = "LocalCentos" // SSH Server配置的名字
def SOURCE_FILES = "**/target/*.jar"
def REMOTE_DIRECTORY = "" //追加SSH Server配置的目录
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 //重试次数
]
)
]
)
}
}
}
}