Jenkins Pipeline Across Multiple Docker Images

2019-02-18 20:11发布

问题:

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).