jenkins kubernetes-plugin archive / junit

时间:2018-02-22 23:07:31

标签: 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"
            }
        }
    }
}

构建日志显示以下输出:

[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>

非常感谢你的帮助!

2 个答案:

答案 0 :(得分:0)

junitarchiveArtifacts只能归档WORKSPACE内的文件,容器不会使用host(jenkins WORKSPACE所在)刮除任何卷,除非您明确这样做

我解决了这个:
  - 在我保存文件的地方添加额外的卷

hostPathVolume(hostPath: '/tmp', mountPath: '/tmp')

- 使用File Operations Plugin

将文件从tmp复制到WORKSPACE
dir("/tmp/screenshots") {
    fileOperations([fileCopyOperation(excludes: '', flattenFiles: true, includes: '*.png', targetLocation: "${WORKSPACE}/screenshots")])
}

- 归档工件

archiveArtifacts allowEmptyArchive: true, artifacts: 'screenshots/**/*.png'

答案 1 :(得分:0)

将工件复制到 Jenkins 工作区即可解决

age('test') {
        container('c1') {
            sh """
              ./something-that-generates-junit-foo-xml
            """
            sh 'cp /abs/path/to/foo.xml ${WORKSPACE}/abs/foo.xml'
            archiveArtifacts allowEmptyArchive: true, artifacts: 'abs/foo.xml'

            sh "cat /abs/path/to/foo.xml"
        }

如果需要所有目录内容,可以复制目录

sh 'cp -r /abs/path/to/ ${WORKSPACE}/abs/to'
archiveArtifacts allowEmptyArchive: true, artifacts: 'abs/to/*.xml'