如何在Jenkins声明式管道中将x11转发到Docker容器

时间:2019-07-13 18:49:19

标签: docker jenkins-pipeline x11

我正在尝试将我的Jenkins管道切换为使用docker构建和运行测试。如何在Docker容器周围设置Xvnc?

当前,该构建可在任何可用的VM上运行。我在gradle build命令周围使用wrap([$ class:'Xvnc',...])来设置DISPLAY,如下所示。

// Original working pipeline
pipeline {
    agent  any
    stages {
        // ... some scm checkout logic
        stage("Build") {
            steps {
                wrap([$class: 'Xvnc', takeScreenshot: false, useXauthority: true]) {
                    sh './gradlew build'
                }
            }
        }
    }
}

工作正常。 显然,如果我不使用wrap([$ class:'Xvnc',...]),那么我的测试将失败,因为未设置DISPLAY等。

我希望我可以添加一个代理{docker}块(具有适当的设置)并且可以工作。

如果我这样做,那么wrap([$ class:'Xvnc',...])在容器内执行,这很有意义,但不是我想要的。我收到各种明显的错误,例如未安装vncserver等。

无论如何,据我所知,该wrap([$ class:'Xvnc',...])调用需要绕过“ Build”阶段,该阶段将具有代理{docker}块。

问题是我认为我不能做这样的事情:

stages {
  wrap([$class: 'Xvnc', takeScreenshot: false, useXauthority: true]) {
    stage("Build") {
      agent {
        docker {
          image 'my_image'
          // some more config, DISPLAY, xsocket, xauth, etc.
        }
      }
      steps {
        sh './gradlew build'
      }
    }
  }
}

至少它不起作用。我收到如下错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 38: Expected a stage @ line 38, column 9.
           wrap([$class: 'Xvnc', takeScreenshot: false, useXauthority: true]) {

还有另一种方法可以实现这一目标吗?

1 个答案:

答案 0 :(得分:0)

您可以从以下位置使用docker jenkins步骤,而不是在“代理”部分中添加docker配置:https://jenkins.io/doc/pipeline/steps/docker-workflow

在“步骤”部分制作脚本,例如:

wrap(xvnc) {
    withDockerRegistry(...) {
        withDockerContainer(...) {
            sh
        }
    }
}