1
votes

I need to create a Jenkins new job by copying the configurations from an existing maven project. I wanted to do this via a groovy script and using the I have Process DSL plugin. I have written the below script which is able to create a new job but I am getting an issue with the GIT SSH URL

String gitRepository = 'ssh://[email protected]:1111/cegp/abc-automation-test'
String buildBranch = 'develop'
String projectName = 'APMSmokeTesting'
String credentialIDGithub = '61668d1b-3336-4c4d-90d7-721017049e36'


// job definition
mavenJob(projectName) {
    logRotator {
        numToKeep(20)
    }
    wrappers {
        preBuildCleanup()
    }
    description('Build the Java project: ' + gitRepository)
    
    scm {
        git {
            branch(buildBranch)
            remote {
                github (gitRepository)
                credentials(credentialIDGithub)
            }
        }
    }
    
    triggers {
        scm('@daily')
    }
    wrappers {
        goals('clean verify -Dtags="APMSmokeTesting"')
    }
}

As per the above configuration, in the new job Source code Management the Repository URL should be ssh://[email protected]:1111/cegp/abc-automation-test.git as I need to do an SSH only. But the above script is population Repository URL filed as **https://github.com/**ssh://[email protected]:1111/cegp/abc-automation-test/ which is wrong. Could you please help me to resolve the same.

1
Do you need to create a new job during runtime? - mdabdullah
Yes, I will read a file to get the Job names/ Whatever job name I will get I will create jobs using the same names - subrat padhi
Please add more details about your requirement. When you create a new job based on file, what is the difference between the original job and the copied job? I am trying to understand if for sake of varying parameters between the two jobs you really need to make a job copy. There may be other ways to achieve what you need. Some considerations- Jobs may be nested in many folders so you are trying to query jenkins to find the right job and the making a copy of the config.xml into a new job with a new name. Is this your requirement? - mdabdullah
It will grat if it can be done by Process Job DSLs also - subrat padhi
Yes, your suggestion worked. Thanks !! pipeline jon - subrat padhi

1 Answers

1
votes
Working code to automate job creation in Jenkins:

    String gitRepository = 'ssh://[email protected]:<port>/cegp/gsc-automation-test'
    String buildBranch = 'develop'
    String projectName = 'APMSmokeTesting'
    String credentialIDGithub = '61668d1b-3336-4c4d-90d7-721017049e36'

    
    // job definition
    mavenJob(projectName) {
        logRotator {
            numToKeep(20)
        }
        wrappers {
            preBuildCleanup()
        }
        description('Build the Java project: ' + gitRepository)
        
        scm {
            git {
                branch(buildBranch)
                remote {
                    url (gitRepository)
                    credentials(credentialIDGithub)
                }
            }
        }
        
        triggers {
            scm('@daily')
        }
        wrappers {
            goals('clean verify -Dtags="APMSmokeTesting"')
        }
    }