ElyashivLavi
ElyashivLavi

Reputation: 1811

jenkins groovy with loop

I want to add similar stages to Jenkins pipeline, something like:

pipeline {
       stage('Publish archives to Artifactory - common') {
        steps {
            dir('android/build/artifacts_output/common') {
                script {
                    def server = Artifactory.server 'artifactory'
                        def uploadSpec = """{
                            "files": [
                            {
                                "pattern": "*.*",
                                "target": "my-repo/1.0.0/common"
                            }
                            ]
                        }"""

                    server.upload(uploadSpec)
                }
            }
        }
    }

    stage('Publish archives to Artifactory - core') {
        steps {
            dir('android/core') {
                script {
                    def server = Artifactory.server 'artifactory'
                        def uploadSpec = """{
                            "files": [
                            {
                                "pattern": "*.*",
                                "target": "my-repo/1.0.0/core"
                            }
                            ]
                        }"""

                    server.upload(uploadSpec)
                }
            }
        }
    }
}

I need to add some more stages like this, for different modules. Is there a better way to do it, like adding stages with loop, instead of copy-pasting this code many times?

This snippet is written in Groovy. I'm not familiar enough with the syntax of Groovy...

EDIT

Also found this similar question

Upvotes: 1

Views: 4110

Answers (1)

aventurin
aventurin

Reputation: 2203

One possible approach would be to provide a list of configuration objects and then iterate over the list

def list = ["Conf 1", "Conf 2", "Conf 3", "Conf Last"]

list.each { stageName ->
    node {
        stage(stageName) {
            println(stageName)
        }
    }
}

In your case you would rather need maps or some other kind of parameter objects in the list.

Upvotes: 2

Related Questions