我尝试在AntBuilder
的帮助下为jenkins管道在共享库中创建一个zip文件。可以创建一个简单的zip文件,但是一旦我尝试使用一个块,它就无法工作。我没有收到任何错误消息或异常。如何调试Pipepeline脚本?或者我该如何解决这个问题?
我的代码如下(步骤zipFolder不起作用,步骤zipFolder2起作用)
Jenkinsfile @Library('utils')_
pipeline {
agent any
stages {
stage('Build') {
steps {
zipFolder( )
zipFolder2( )
}
}
}
}
共享库:
vars / zipFolder.groovy:
import com.example.Utilities
def call(){
new Utilities(this).zip(pwd())
}
vars / zipFolder2.groovy
import com.example.Utilities
def call(){
new Utilities(this).zip2(pwd())
}
src / com / example / Utilities.groovy
package com.example
import groovy.util.AntBuilder
import org.apache.tools.ant.DefaultLogger
class Utilities implements Serializable {
def steps
def byteStream
Utilities(steps) {this.steps = steps}
def zip(baseDir){
def ant = setupAnt(baseDir)
def destFolder = "${baseDir}/Dist"
def destfile = "${destFolder}/test.zip"
ant.delete dir: destFolder
ant.mkdir(dir: destFolder)
ant.zip(destfile: destfile, whenempty: 'create', excludes: destfile) {
zipfileset (dir: "${baseDir}/install", includes: "test.txt", erroronmissingdir: false)
}
steps.echo "Zip1"
steps.echo "Ant-Result: " + byteStream.toString()
}
def zip2(baseDir){
def ant = setupAnt(baseDir)
def destFolder = "${baseDir}/Dist2"
def destfile = "${destFolder}/test.zip"
ant.delete dir: destFolder
ant.mkdir(dir: destFolder)
ant.zip(destfile: destfile, whenempty: 'create', excludes: destfile, basedir: baseDir)
steps.echo "Zip2"
steps.echo "Ant-Result: " + byteStream.toString()
}
def setupAnt(baseDir){
def ant = new AntBuilder()
byteStream = new ByteArrayOutputStream()
def printStream = new PrintStream( byteStream )
def project = ant.project
project.buildListeners.each {
if ( it instanceof DefaultLogger ) {
it.setMessageOutputLevel(org.apache.tools.ant.Project.MSG_DEBUG)
it.setOutputPrintStream printStream
it.setErrorPrintStream printStream
}
}
ant
}
}
答案 0 :(得分:1)
AntBuilder
不是Serializable
。我们所做的是将对AntBuilder的调用封装在一个额外的groovy脚本中,该脚本是动态创建的,并通过调用groovy二进制文件来调用,例如:
writeFile file:createZip.groovy text:”””
def antBuilder = new AntBuilder()
...
“””
sh ‘groovy createZip.groovy’
您还可以考虑使用Jenkins Pipeline zip步骤。参见https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#zip-create-zip-file
答案 1 :(得分:0)
在Joerg S的提示下,我使用以下代码解决了该问题:
src/com/example/Utilities.groovy
package com.example
import org.apache.tools.ant.DefaultLogger
package com.example
import org.apache.tools.ant.DefaultLogger
class Utilities implements Serializable {
def steps
def byteStream
Utilities(steps) {this.steps = steps}
def zip(baseDir, Closure callback ){
def ant = setupAnt()
def destFolder = "${baseDir}/Dist"
def destfile = "${destFolder}/test.zip"
ant.delete dir: destFolder
ant.mkdir(dir: destFolder)
ant.zip(destfile: destfile, whenempty: 'create', excludes: destfile, callback)
steps.echo "Ant-Result: " + byteStream.toString()
}
def setupAnt(){
def ant = new AntBuilder()
byteStream = new ByteArrayOutputStream()
def printStream = new PrintStream( byteStream )
def project = ant.project
project.buildListeners.each {
if ( it instanceof DefaultLogger ) {
it.setMessageOutputLevel(org.apache.tools.ant.Project.MSG_DEBUG)
it.setOutputPrintStream printStream
it.setErrorPrintStream printStream
}
}
ant
}
}
vars/zipFolder.groovy
import com.example.Utilities
def call(Closure callback){
new Utilities(this).zip(pwd(), callback)
}
Jenkinsfile
@Library('utils') _
pipeline {
agent any
stages {
stage('Build') {
steps {
cleanWs()
writeFile file: 'test.txt', text: 'Sample Text'
writeFile file: 'test2.txt', text: 'Sample Text 2'
writeFile file: 'src/test3.txt', text: 'Sample Text 3'
zipIt()
}
}
}
}
@NonCPS
def zipIt(){
zipFolder( ) {
delegate.zipfileset (dir: "${pwd()}", includes: "test.txt", excludes: "src/**" , filemode: "0755", prefix: "bin/examples", erroronmissingdir: false)
delegate.zipfileset (dir: "${pwd()}/src", includes: "*.txt", erroronmissingdir: false)
}
}
重要的是使用注释@NonCPS
和delegate.zipfileset
,而不是简单地使用zipfileset
。
编辑:该解决方案不适用于从属服务器。即使构建在从属服务器上运行,该代码也会在主服务器上执行。