没有让Docker在Jenkins中运行 [英] Not getting Docker to run from within Jenkins

查看:109
本文介绍了没有让Docker在Jenkins中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条Jenkins-Pipeline,可以成功构建docker-image。
但是在运行docker-image时会失败。
在docker镜像中,我有一些我想与ctest一起运行的API测试。
可以通过在目录/ testing中调用test来执行测试。

I have a Jenkins-Pipeline that successfully builds a docker-image. But when it comes to running the docker-image it fails. Inside the docker image I have some API-tests that I want to run with ctest. The tests can be executed by calling test in the directory /testing.

有任何建议吗?

pipeline {

agent {label 'apitest-centos7'}
  stages {
    stage('cleanup'){
      steps{
        cleanWs()
      }
    }

    stage('git checkout Dockerfile') {
      steps{
        checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'ebf01b-3gf93-4f8e-9f69-97ffgff308af', url: 'http://gitlab.somecompany.loc/somecompany/MyApiTestingTools.git']]])
        sh 'ls'
      }
    }

    stage('build Dockerimage'){
      steps{
        dir('dockerbuild'){
            fileExists 'Dockerfile'
            sh 'sudo docker build --no-cache=true -t apitestimage .'
        }
      }
    }

    stage('start Dockerimage and Tests') {
      steps{
        dir("dockerbuild"){
          withDockerContainer(args: "-ti apitestimage bash -c 'cd testing && ctest", image: 'apitestimage') {
          }              
        }
      }
    }
  }
  post {
    always {
          /*mattermostSend color: 'good', message: 'API-Tests have been executed'*/
          deleteDir() /* clean up our workspace */
    }
    success {
        mattermostSend color: 'good', message: 'API-Tests succeded', channel: 'api-tests'
    }
    unstable {
        mattermostSend color: 'warning', message: 'API-Tests are unstable', channel: 'api-tests'
    }
    failure {
        mattermostSend color: 'danger', message: 'API-Tests have failed', channel: 'api-tests', icon: 'https://talks.bitexpert.de/zendcon16-jenkins-for-php-projects/images/jenkins.png'
    }
    changed {
          mattermostSend color: 'warning', message: 'API-Tests have changed', channel: 'api-tests'
    }
  }
}


推荐答案

我会猜测s出现错误的3个原因:

I would guess 3 reasons for an error:


  • 您正在尝试使用tty( -it 选项)在没有tty的非交互式环境(jenkins构建环境)中,这可能会引起一些问题

  • 您为图像名称输入了两次: args image

  • 您没有关闭报价

  • you're trying to launch the container interactive mode with tty (-it options) in a non-interactive without tty environment (the jenkins build environment), which may cause some problems
  • you're giving the image name twice: in args and image
  • you're not closing a quote

尝试更改为:

withDockerContainer(args: "bash -c 'cd testing && ctest'", image: 'apitestimage')






顺便说一句,您可以考虑使用 docker管道API

stage('build Dockerimage') {
  steps {
    script {
      apitestimage = docker.build('apitestimage', '--no-cache=true dockerbuild')
    }
  }
}

stage('start Dockerimage and Tests') {
  steps{
    script {
      apitestimage.inside {
        sh 'cd testing && ctest'
      }
    }
  }
}

作为 docker.inside 在当前工作空间上安装一个卷,您甚至可以避免创建映像的步骤(您不发布它,所以我想这仅是测试所必需的),并从基本映像以构建测试环境并启动测试。

As the docker.inside mount a volume on the current workspace, you even could avoid the image creation step (you don't publish it, so I guess it's only necessary for tests) and launch the necessary commands from a base image to build the test environment and launch the tests.

这篇关于没有让Docker在Jenkins中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆