groovy - 无法在詹金斯管道的共享库中创建 zip 文件
问题描述
我尝试在 jenkins 管道的共享库中创建 zip 文件AntBuilder
。可以创建一个简单的 zip 文件,但是一旦我尝试使用一个块,它就不起作用。我没有收到任何错误消息或异常。如何调试管道脚本?或者我该如何解决这个问题?
我的代码如下所示(步骤 zipFolder 不起作用,步骤 zipFolder2 起作用)
Jenkinsfile @Library('utils') _
pipeline {
agent any
stages {
stage('Build') {
steps {
zipFolder( )
zipFolder2( )
}
}
}
}
共享库:
变量/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
}
}
解决方案
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
推荐阅读
- django - 如何解决 Django 服务器问题
- python - 文件不在指定的任何路径中
- google-bigquery - 使用 bq 命令创建 hive 分区外部表时缺少 hive 分区键列
- reactjs - 反应 js 与数据库
- google-translate - 如何使用 Google 音译 API 使用 Key
- python - 使用pandas模块创建excel文件时如何保持整列中两个变量之间的公共空间
- python - askdirectory() 关闭 Tkinter 窗口
- php - 严格比较失败 laravel Blade,我犯了一些愚蠢的错误
- arrays - 如何在没有结果的情况下将 sha256 的前 128 位作为 u128?
- sap-cloud-sdk - SuccessFactors 实体上的 SAP Java Cloud SDK 计数查询不起作用