在Jenkinsfile

时间:2017-08-23 09:08:05

标签: node.js shell docker jenkins jenkins-pipeline

我有一个基于Nightwatch.js的end2end测试的测试项目,它是一个NodeJS框架。我想在我的项目中使用'Jenkinsfile'为我的end2end测试构建一个管道,以便在Jenkins的{​​{1}}上执行它们。所以,我想开始Docker container并执行此Docker container内的测试。这应该通过Docker container来实现。当我不使用Jenkinsfile但在手动创建的作业中直接使用Jenkinsfile时,一切都很完美。使用shell commands时,我在运行管道时收到Jenkinsfile,但我不知道原因。

这是我的MultipleCompilationErrorsException

Jenkinsfile

这是我在运行管道时遇到的异常:

pipeline {
  agent any

  parameters {
    text(defaultValue: 'grme/nightwatch-chrome-firefox:0.0.3', description: '', name: 'docker_image')
    text(defaultValue: 'npm-test-chrome', description: '', name: 'run_script_method')
    text(defaultValue: '/Applications/Docker.app/Contents/Resources/bin/docker', description: '', name: 'docker')
  }

  stages {
    stage('Test') {
      steps {
        sh 'sudo chmod -R 777 $(pwd)'
        echo "------ stop all Docker containers ------"
        sh '(sudo ${params.docker} stop $(sudo ${params.docker} ps -a -q) || echo "------ all Docker containers are still stopped ------")'
        echo "------ remove all Docker containers ------"
        sh '(sudo ${params.docker} rm $(sudo ${params.docker} ps -a -q) || sudo echo "------ all Docker containers are still removed ------")'
        echo "------ pull Docker image from Docker Cloud ------"
        sh 'sudo ${params.docker} pull "${params.docker_image}"'
        echo "------ start Docker container from image ------"
        sh 'sudo ${params.docker} run -d -t -i -v $(pwd):/my_tests/ "${params.docker_image}" /bin/bash'
        echo "------ execute end2end tests on Docker container ------"
        sh 'sudo ${params.docker} exec -i $(sudo ${params.docker} ps --format "{{.Names}}") bash -c "cd /my_tests && xvfb-run --server-args='-screen 0 1600x1200x24' npm run ${params.run_script_method} || true && google-chrome --version && firefox --version"'
        echo "------ cleanup all temporary files ------"
        sh 'sudo rm -Rf $(pwd)/tmp-*'
        sh 'sudo rm -Rf $(pwd)/.com.google*'
        sh 'sudo rm -Rf $(pwd)/rust_mozprofile*'
        sh 'sudo rm -Rf $(pwd)/.org.chromium*'
        echo "------ stop all Docker containers again ------"
        sh '(sudo ${params.docker} stop $(sudo ${params.docker} ps -a -q) || sudo echo "------ all Docker containers are still stopped ------")'
        echo "------ remove all Docker containers again ------"
        sh '(sudo ${params.docker} rm $(sudo ${params.docker} ps -a -q) || sudo echo "------ all Docker containers are still removed ------")'
      }
    }
  }
}

我错了怎么办?如何解决这个例外?

1 个答案:

答案 0 :(得分:1)

在转义行中的'后,管道不再出现语法错误:)

sh 'sudo ${params.docker} exec -i $(sudo ${params.docker} ps --format "{{.Names}}") bash -c "cd /my_tests && xvfb-run --server-args=\'-screen 0 1600x1200x24\' npm run ${params.run_script_method} || true && google-chrome --version && firefox --version"'