【问题标题】:create a new Jenkins job by using Process DSL plugin groovy script使用 Process DSL 插件 groovy 脚本创建一个新的 Jenkins 作业
【发布时间】:2020-09-11 05:55:03
【问题描述】:

我需要通过从现有 Maven 项目中复制配置来创建 Jenkins 新作业。 我想通过一个 groovy 脚本并使用 I have Process DSL 插件来做到这一点。我已经编写了下面的脚本,它能够创建一个新工作,但是我遇到了 GIT SSH URL 的问题

String gitRepository = 'ssh://git@stash.abc.com: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"')
    }
}

按照上面的配置,在新工作Source code ManagementRepository URL应该是ssh://git@stash.abc.com:1111/cegp /abc-automation-test.git 因为我只需要做一个 SSH。 但是上面的脚本是填充 Repository URL 归档为 **https://github.com/**ssh://git@stash.abc.com:1111/cegp/abc-automation-test/ 这是错误的。 你能帮我解决同样的问题吗?

【问题讨论】:

  • 运行时需要新建作业吗?
  • 是的,我将读取一个文件以获取作业名称/无论我将获得什么作业名称,我都将使用相同的名称创建作业
  • 请添加有关您的要求的更多详细信息。当您基于文件创建新作业时,原始作业和复制作业有什么区别?我试图了解是否为了在两个工作之间改变parameters,你真的需要制作一份工作副本。可能还有其他方法可以实现您的需求。一些注意事项 - 作业可能嵌套在许多文件夹中,因此您尝试将 query jenkins 转换为 find 正确的作业,并将 config.xml 的副本复制到具有新名称的新作业中。这是你的要求吗?
  • 如果它也可以通过 Process Job DSL 来完成,那就太好了
  • 是的,你的建议奏效了。谢谢 !!管道乔恩

标签: jenkins jenkins-pipeline jenkins-plugins jenkins-groovy


【解决方案1】:
Working code to automate job creation in Jenkins:

    String gitRepository = 'ssh://git@stash.abc.com:<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"')
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多