How to share a Declarative Pipeline across many projects

前端 未结 3 1679
别跟我提以往
别跟我提以往 2021-02-10 09:57

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 {         


        
3条回答
  •  被撕碎了的回忆
    2021-02-10 10:12

    I am able to use a Shared Library to define a Declarative pipeline that is configurable via a YAML file.

    In my repo/project I define a Jenkinsfile to call the Shared Library:

    @Library('my-shared-library')_
    
    pipelineDefault(); // cannot be named 'pipeline'
    

    and a Jenkinsfile.yaml to configure the build parameters:

    project_name: my_project
    debug: true
    # you get the idea
    

    Then in my vars/pipelineDefault.groovy file a very simple Shared Library could look like this:

    def call() {
      Map pipelineConfig = readYaml(file: "${WORKSPACE}/Jenkinsfile.yaml }")
      node {
        stage('Build'){
          println "Building: ${pipelineConfig.project_name}"
        }
      }
    }  
    

    Of course this is a very simplified example, but the dynamic configuration DOES work.

    NOTE: this requires the pipeline-utility-steps plugin to read the YAML file

提交回复
热议问题