Search code examples
jenkinsjenkins-pipelinejenkins-pluginsjenkins-groovy

create a new Jenkins job by using Process DSL plugin groovy script


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.


Solution

  • 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"')
            }
        }