Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share a Declarative Pipeline across many projects

I have a lot of projects in different repositories which share the same fundamental CI-workflow, which I can easily express as a Declarative Pipeline:

pipeline {
  agent any

  options {
    buildDiscarder(logRotator(numToKeepStr: '20'))
  }

  stages {
    stage('CI') {
      steps {
        echo 'Do CI'
      }
    }

    stage('QA') {
      steps {
        echo 'Do QA'
      }
    }
  }

  post {
    always {
      junit allowEmptyResults: true, testResults: '**/target/surefire-reports/TEST-*.xml'
      // etc...
    }

    failure {
      echo 'Failure mail'
      // etc
    }
  }
}

I would like to use the same Declarative Pipeline across all my projects and have the ability to change the definition of the Pipeline in just one place and have the changes used in all projects automatically.

Essentially what I would to do in a project;s Jenkinsfile is this:

loadPipelineFromScm 'repository', 'pipeline.groovy'

I can already do this with shared libraries, but then I'm not able to use Declarative Pipeline features anymore.

Is there a way to share a Declarative Pipeline across many repositories?

like image 485
pmr Avatar asked Nov 08 '22 01:11

pmr


1 Answers

While the views are left intact using the suggestion from noober01 the declarative pipeline will not function properly. E.g. when clauses will be ignored, since the pipeline element is expected to be top-level, meaning it is parsed as scripted pipeline instead.

See the following issue rejected by the team behind Jenkins: loading external declarative pipelines issue

like image 199
Nick H. Lauritsen Avatar answered Nov 15 '22 07:11

Nick H. Lauritsen