griddynamics / mpl

[IT-36925] Jenkins Shared Modular Pipeline Library
https://blog.griddynamics.com/developing-a-modular-pipeline-library-to-improve-devops-collaboration/
Apache License 2.0
156 stars 97 forks source link

[Question] How to pass my own configs to pipeline #72

Closed njZhuMin closed 4 years ago

njZhuMin commented 4 years ago

Hi @sparshev ,

I'm new to Jenkins pipeline and Groovy. I am trying to create my own pipeline and therefore I have this question: can I somehow make my pipeline "inherit" from MPL, or how can I pass my own configs to my pipeline?

For example: Jenkinsfile

@Library('my-pipeline-lib') _

MyPipeline {
    project = [
            name: 'infra-java-lib',
            major_version: '1.0.0'
    ]
    build = [
            builder: 'maven'
    ]
    deploy = [
            type: 'nexus'
    ]
    modules.Test = null
}

MyPipeline.groovy

def call(body) {
    // init MPL library
    MPLInit()

    // Execute the pipeline
    pipeline {
        agent {
            label Configuration.AGENT_LABEL
        }
        stages {
            stage('Build') {
                steps {
                    echo "====++++executing Build++++===="
                    script {
                        if (CFG.build.builder == 'maven') {    **// <---------------- How do I retrieve the configs I passed?**
                            docker.withRegistry(Configuration.CDC_REGISTRY, Configuration.DDEC_WRITE_CREDENTIAL) {
                                docker.image(Configuration.BUILDER_IMAGE_MAVEN).inside {
                                    MPLModule('Maven Build', CFG)
                                }
                            }
                        }
                    }
                }
            }
            stage('Deploy') {
                steps {
                    echo "====++++executing Deploy++++===="
                    script {
                        if (CFG.deploy.type == 'nexus') {
                            docker.withRegistry(Configuration.CDC_REGISTRY, Configuration.DDEC_WRITE_CREDENTIAL) {
                                docker.image(Configuration.BUILDER_IMAGE_MAVEN).inside {
                                    MPLModule('Nexus Deploy', CFG)
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

I know this looks dumb, but I've been stuck here for days trying to understand all this T_T. I also went through all the examples in mpl.wiki repo and found few that help. I really appreciate if you could explain it a little bit.

Thank you for your great work on MPL!

sparshev commented 4 years ago

Hi @njZhuMin ,

You need to check the examples on Wiki page - just download and see how they are prepared.

To use CFG - you need to be in the module (that loads by MPLModule() in the default MPLPipeline). So first of all - try to run the simple example and try to modify it for your usecase, it's the easiest way to learn the basics I think)

njZhuMin commented 4 years ago

Hi @sparshev ,

Thank you so much for your answer. After another dig in the code, I now understand the basics of the whole MPL thing.

The problem is I am using a hosted Jenkins, so I cannot add MPL to global shared library, which leads to a failure in running the demo library due to sandbox limitations.

We decided to use a workaround of this. However I will still keep an eye on this interesting and great project!

I may now kindly proceed to close this issue. Thank you again for your great work here.

Best regards, MIN