我正在尝试在Jenkins流水线(工作流)中使用Jenkins复制构件插件的示例。
是否有人能提供一个使用它的Groovy代码示例?
我正在尝试在Jenkins流水线(工作流)中使用Jenkins复制构件插件的示例。
是否有人能提供一个使用它的Groovy代码示例?
使用声明式 Jenkinsfile,您可以使用以下流水线:
pipeline {
agent any
stages {
stage ('push artifact') {
steps {
sh 'mkdir archive'
sh 'echo test > archive/test.txt'
zip zipFile: 'test.zip', archive: false, dir: 'archive'
archiveArtifacts artifacts: 'test.zip', fingerprint: true
}
}
stage('pull artifact') {
steps {
copyArtifacts filter: 'test.zip', fingerprintArtifacts: true, projectName: env.JOB_NAME, selector: specific(env.BUILD_NUMBER)
unzip zipFile: 'test.zip', dir: './archive_new'
sh 'cat archive_new/test.txt'
}
}
}
}
在CopyArtifact的1.39版本之前,您必须将第二阶段替换为以下内容(感谢@Yeroc):
stage('pull artifact') {
steps {
step([ $class: 'CopyArtifact',
filter: 'test.zip',
fingerprintArtifacts: true,
projectName: '${JOB_NAME}',
selector: [$class: 'SpecificBuildSelector', buildNumber: '${BUILD_NUMBER}']
])
unzip zipFile: 'test.zip', dir: './archive_new'
sh 'cat archive_new/test.txt'
}
}
使用 CopyArtifact
,我将 '${JOB_NAME}' 作为项目名称,该名称是当前正在运行的项目。
CopyArtifact
使用默认选择器使用上一个成功的项目构建编号,而不是当前的编号(因为它尚未成功或不存在)。使用 SpecificBuildSelector
,您可以选择包含当前运行项目构建编号的 '${BUILD_NUMBER}'。
此流水线适用于并行阶段,并且可以管理大型文件(我正在使用 300Mb 文件,无法使用 stash/unstash)
在我的 Jenkins 2.74 上,只要安装了所有必需的插件,此流水线就可以完美运行
如果您在控制器中使用代理,并且想要在它们之间复制构件,您可以使用stash
/unstash
,例如:
如果您在控制器中使用代理,并且想要在它们之间复制构件,您可以使用stash
/unstash
,例如:
stage 'build'
node{
git 'https://github.com/cloudbees/todo-api.git'
stash includes: 'pom.xml', name: 'pom'
}
stage name: 'test', concurrency: 3
node {
unstash 'pom'
sh 'cat pom.xml'
}
您可以在此链接中查看此示例:
https://dzone.com/refcardz/continuous-delivery-with-jenkins-workflow
如果构建不在同一条流水线中运行,您可以使用直接的 CopyArtifact
插件,这里是一个示例:https://www.cloudbees.com/blog/copying-artifacts-between-builds-jenkins-workflow 和示例代码:
node {
// setup env..
// copy the deployment unit from another Job...
step ([$class: 'CopyArtifact',
projectName: 'webapp_build',
filter: 'target/orders.war']);
// deploy 'target/orders.war' to an app host
}
name = "/" + "${env.JOB_NAME}"
def archiveName = 'relNum'
try {
step($class: 'hudson.plugins.copyartifact.CopyArtifact', projectName: name, filter: archiveName)
} catch (none) {
echo 'No artifact to copy from ' + name + ' with name relNum'
writeFile file: archiveName, text: '3'
}
def content = readFile(archiveName).trim()
echo 'value archived: ' + content
尝试使用复制工件插件
`copyArtifacts filter: 'test.zip', fingerprintArtifacts: true, projectName: '${JOB_NAME}', selector: specific('${BUILD_NUMBER}')`
该语句的作用是在当前构建中复制指定项目${JOB_NAME}
中具有指定构建号${BUILD_NUMBER}
的名为test.zip
的构建产物,并创建指纹(fingerprint)。 - YerocprojectName: '${JOB_NAME}', selector: specific('${BUILD_NUMBER}')
更改为projectName: env.JOB_NAME, selector: specific(env.BUILD_NUMBER)
对我有用(另请参阅 https://dev59.com/u1UM5IYBdhLWcg3wc_2c#55377899)。 - Pit