jenkins kubernetes-plugin 存档/junit

标签 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>

感谢您的帮助!

最佳答案

两者 junitarchiveArtifacts只能存档 WORKSPACE 内的文件,除非您明确这样做,否则容器不会与主机(jenkins WORKSPACE 所在的位置)一起刮削任何卷

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

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

- 从 tmp 复制文件到工作区 File Operations Plugin
dir("/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/

相关文章:

jenkins - 我在 Jenkins 中的 JavaDoc 在哪里

Jenkins 作业构建失败

jenkins - 如何将值从 Jenkins 传递给 Ansible playbook

ios - 如何在物理 ios 设备上使用 faSTLane?

Jenkins 管道 bat 多行

Jenkins 使用错误版本的 Ruby(rvm)

html - 如何在 Jenkins 构建页面上流式传输 mp4 神器

jenkins - 监控 openstack4j 的请求量

javascript - Jenkins:在 JS 文件中传递变量

java - 作业 API 的 Jenkins 扩展点