Jenkins Pipeline Across Multiple Docker Images

我与影子孤独终老i 提交于 2020-01-02 06:03:47

问题


Using a declarative pipeline in Jenkins, how do I run stages across multiple versions of a docker image. I want to execute the following jenkinsfile on python 2.7, 3.5, and 3.6. Below is a pipeline file for building and testing a python project in a docker container

pipeline {
  agent {
    docker {
      image 'python:2.7.14'
    }
  }

  stages {
    stage('Build') {
      steps {
        sh 'pip install pipenv'
        sh 'pipenv install --dev'
      }
    }

    stage('Test') {
      steps {
        sh 'pipenv run pytest --junitxml=TestResults.xml'
      }
    }
  }

  post {
    always {
      junit 'TestResults.xml'
    }
  }
}

What is minimal amount of code to make sure the same steps succeed across python 3.5 and 3.6? The hope is that if a test fails, it is evident which version(s) the test fails on.

Or is what I'm asking for not possible for declarative pipelines (eg. scripted pipelines may be what would most elegantly solve this problem)

As a comparison, this is how Travis CI let's you specify runs across different python version.


回答1:


I had to resort to a scripted pipeline and combine all the stages

def pythons = ["2.7.14", "3.5.4", "3.6.2"]

def steps = pythons.collectEntries {
    ["python $it": job(it)]
}

parallel steps

def job(version) {
    return {
        docker.image("python:${version}").inside {
            checkout scm
            sh 'pip install pipenv'
            sh 'pipenv install --dev'
            sh 'pipenv run pytest --junitxml=TestResults.xml'
            junit 'TestResults.xml'
        }
    }
}

The resulting pipeline looks like

Ideally we'd be able to break up each job into stages (Setup, Build, Test), but the UI currently doesn't support this (still not supported).



来源:https://stackoverflow.com/questions/46458941/jenkins-pipeline-across-multiple-docker-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!