我有一个多容器作业,它通过 kubernetes-jenkins 插件在 k8s 上运行。一切正常,但我无法 junit
或 archiveArtifacts
任何事物。我怀疑是因为它只存在于容器中但不确定。代码如下:
def label = "foo-${UUID.randomUUID().toString()}"
podTemplate(
label: label,
containers: [
containerTemplate(name: 'c1', image: 'c1'),
containerTemplate(name: 'c2', image: 'c2'),
containerTemplate(name: 'c3', image: 'c3'),
],
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
],
) {
node(label) {
stage('test') {
container('c1') {
sh """
cd /some-path
./generate-junit-xml
"""
archiveArtifacts allowEmptyArchive: true, artifacts: '/some-path/foo.xml'
sh "cat /some-path/foo.xml"
}
}
}
}
def label = "foo-${UUID.randomUUID().toString()}"
podTemplate(
label: label,
namespace: 'jenkins',
imagePullSecrets: [ 'myreg' ],
containers: [
containerTemplate(name: 'c1', image: 'c1'),
containerTemplate(name: 'c2', image: 'c2'),
containerTemplate(name: 'c3', image: 'c3'),
],
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
],
) {
node(label) {
stage('test') {
container('c1') {
sh """
./something-that-generates-junit-foo-xml
"""
archiveArtifacts allowEmptyArchive: true, artifacts: '/abs/path/to/foo.xml'
sh "cat /abs/path/to/foo.xml"
}
}
}
}
构建日志显示以下输出:
[Pipeline] archiveArtifacts
Archiving artifacts
WARN: No artifacts found that match the file pattern "/some-path/foo.xml". Configuration error?
[Pipeline] sh
[test-pipeline] Running shell script
+ cat /some-path/unittest.xml
<?xml version="1.0" encoding="utf-8"?>...</xml>
感谢您的帮助!
最佳答案
两者 junit
和 archiveArtifacts
只能存档 WORKSPACE 内的文件,除非您明确这样做,否则容器不会与主机(jenkins WORKSPACE 所在的位置)一起刮削任何卷
我用以下方法解决了这个问题:
- 在我保存文件的地方添加额外的卷
hostPathVolume(hostPath: '/tmp', mountPath: '/tmp')
- 从
tmp
复制文件到工作区 File Operations Plugindir("/tmp/screenshots") {
fileOperations([fileCopyOperation(excludes: '', flattenFiles: true, includes: '*.png', targetLocation: "${WORKSPACE}/screenshots")])
}
- 存档工件
archiveArtifacts allowEmptyArchive: true, artifacts: 'screenshots/**/*.png'
关于jenkins kubernetes-plugin 存档/junit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48938704/