简体   繁体   中英

How to forward x11 to Docker container in Jenkins declarative pipeline

I am trying to switch my Jenkins pipeline to build and run tests using docker. How can I setup Xvnc around the docker container?

Currently the build runs on any available VM. I use wrap([$class: 'Xvnc', ...]) around the gradle build command to setup the DISPLAY as below.

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

It works fine. Obviously if I don't use wrap([$class: 'Xvnc', ...]) then my tests fail because the DISPLAY is not setup etc.

I was hoping I could add an agent { docker } block (with the appropriate setup) and it would work.

If I do just that, then wrap([$class: 'Xvnc', ...]) is executed inside the container, which makes sense, but is not what I want. I get all sorts of obvious errors like vncserver is not installed etc.

Anyway, as I understand this wrap([$class: 'Xvnc', ...]) call would need to go around the "Build" stage which would have the agent { docker } block.

Problem is I don't think I can do something like this:

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'
      }
    }
  }
}

At least it doesn't work. I get errors like:

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

Is there another way to achieve this?

Instead of adding the docker config in the "agent" section you can use the docker jenkins steps from here: https://jenkins.io/doc/pipeline/steps/docker-workflow .

To make script in the "steps" section such as:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM