简体   繁体   中英

Multiple jobs running the same pipeline

I have a project using a declarative pipeline for a c++ project. I wish to have multiple jobs running the same pipeline with different configurations. Eg Release or Debug build. Using different compilers. Some jobs using the address sanitizer or thread sanitizer.

Some of these jobs should run on every commit. Other jobs should run once a day.

My idea was to control this by having the pipeline depend on environment variables or parameters.

I now have a JenkinsFile where all the stages depends on environment variables. Unfortunately I can't find a place to control the variables from the outside. Is there any way I can control these environment variables on a job level without violating the DRY principle?

A simplified example depending on two variables.

pipeline {
    agent {
        label 'linux_x64'
    }
    environment {
        CC = '/usr/bin/gcc-4.9'
        BUILD_CONF = 'Debug'
    }
    stages {
        stage('Build') {
            steps {
                cmakeBuild buildDir: 'build', buildType: "${env.BUILD_CONF}", installation: 'InSearchPath', sourceDir: 'src', steps: [[args: '-j4']]
            }
        }
        stage('Test') {
             sh 'test.sh'
        }
    }

I now want two create a second job running the same pipeline but with CC='/usr/bin/clang' and BUILD_CONF='Release'.

In my real project I have more variables and want to test approximately ten combinations.

yes, It possible in one pipeline but it will be more easy with Jenkins scripted pipeline, like below example:

node {
    def x=[
            [CC: 10, DD: 15],
            [CC: 11, DD: 16],
            [CC: 12, DD: 17]
        ]
    x.each{
        stage("stage for CC - ${it.CC}") {
            echo "CC = ${it.CC} & DD = ${it.DD}"
        }
    }
}

so on your case you can decorate your pipleine like below:

node {
  // define all your expected key and value here like an array
  def e = [
             [CC: '/usr/bin/gcc-4.9', BUILD_CONF: 'Debug'],
             [CC: '/usr/bin/clang', BUILD_CONF: 'Release']
          ]
  // Iterate and prepare the pipeline
  e.each {
      stage("build - ${it.BUILD_CONF}") {
         cmakeBuild buildDir: 'build', buildType: "${it.BUILD_CONF}", installation: 'InSearchPath', sourceDir: 'src', steps: [[args: '-j4']]
      }
      stgae("test - ${it.BUILD_CONF}") {
         sh 'test.sh'
      }
  }
}

with this way, you can reuse your stages with defined array like e

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