jenkins kubernetes插件存档/ junit

da *_*nit 5 jenkins jenkins-plugins

我有一个通过kubernetes-jenkins插件在k8s上运行的多容器作业。一切都很正常,但我无法junitarchiveArtifacts任何东西。我怀疑是因为它仅存在于容器中但不确定。代码如下:

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"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

构建日志显示以下输出:

[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>
Run Code Online (Sandbox Code Playgroud)

将感谢您的帮助!

klu*_*ubi 0

两者junit都并且archiveArtifacts只能归档 WORKSPACE 内的文件,除非您明确这样做,否则容器不会与主机(jenkins WORKSPACE 所在的位置)共享任何卷

我通过以下方法解决了这个问题:
- 在保存文件的地方添加额外的卷

hostPathVolume(hostPath: '/tmp', mountPath: '/tmp')
Run Code Online (Sandbox Code Playgroud)

-使用文件操作插件tmp将文件从工作空间复制到工作空间

dir("/tmp/screenshots") {
    fileOperations([fileCopyOperation(excludes: '', flattenFiles: true, includes: '*.png', targetLocation: "${WORKSPACE}/screenshots")])
}
Run Code Online (Sandbox Code Playgroud)

- 归档工件

archiveArtifacts allowEmptyArchive: true, artifacts: 'screenshots/**/*.png'
Run Code Online (Sandbox Code Playgroud)