Как добавить к узлу, созданному заданием Jenkins dsl в том же блоке настройки? - PullRequest
0 голосов
/ 18 декабря 2018

У меня есть несколько jobDsl, который выглядит следующим образом:

multibranchPipelineJob('foo/bar') {
        branchSources {
        git {
            remote('https://github.com/jenkinsci/job-dsl-plugin.git')
            credentialsId('github-ci')
            includes('JENKINS-*')
        }
    }
}

, который генерирует следующий xml (фрагмент):

<!-- 1. foo/bar -->
<org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject>
    <sources class='jenkins.branch.MultiBranchProject$BranchSourceList'>
        <data>
            <jenkins.branch.BranchSource>
                <source class='jenkins.plugins.git.GitSCMSource'>
                    <id>d45cd641-7223-4b58-9de5-837c3fe584a7</id>
                    <remote>https://github.com/jenkinsci/job-dsl-plugin.git</remote>
                    <credentialsId>github-ci</credentialsId>
                    <includes>JENKINS-*</includes>
                    <excludes></excludes>
                    <ignoreOnPushNotifications>false</ignoreOnPushNotifications>
                </source>
                <strategy class='jenkins.branch.DefaultBranchPropertyStrategy'>
                    <properties class='empty-list'></properties>
                </strategy>
            </jenkins.branch.BranchSource>
        </data>
        <owner class='org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject' reference='../..'></owner>
    </sources>

Что я хотел быdo добавляет узел (назовите его traits) к узлу jenkins.branch.BranchSource, созданному API-интерфейсом jobDsl multibranchJob через блок конфигурации:

multibranchPipelineJob('foo/bar') {
        branchSources {
        git {
            remote('https://github.com/jenkinsci/job-dsl-plugin.git')
            credentialsId('github-ci')
            includes('JENKINS-*')
        }

            configure {
               def first =  it / 'sources'(class: 'jenkins.branch.MultiBranchProject$BranchSourceList') / 'data' / 'jenkins.branch.BranchSource'
                first << traits {
                    foo('bar')
                }
            }
    }
}

Однако, согласно https://job -dsl.herokuapp.com / , приведенный выше dsl создаст xml следующим образом:

<sources class='jenkins.branch.MultiBranchProject$BranchSourceList'>
    <data>
        <jenkins.branch.BranchSource>
            <traits>
                <foo>bar</foo>
            </traits>
        </jenkins.branch.BranchSource>
        <jenkins.branch.BranchSource>
            <source class='jenkins.plugins.git.GitSCMSource'>
                <id>7fd47865-fffa-4f8f-98f1-ac6de65249f7</id>
                <remote>https://github.com/jenkinsci/job-dsl-plugin.git</remote>
                <credentialsId>github-ci</credentialsId>
                <includes>JENKINS-*</includes>
                <excludes></excludes>
                <ignoreOnPushNotifications>false</ignoreOnPushNotifications>
            </source>
            <strategy class='jenkins.branch.DefaultBranchPropertyStrategy'>
                <properties class='empty-list'></properties>
            </strategy>
        </jenkins.branch.BranchSource>
    </data>
</sources>

По сути, есть ли способ добавить узел BranchSource, созданный из branchSourcesвызов?

1 Ответ

0 голосов
/ 05 января 2019

Проблема в том, что в контексте branchSources нет метода configure.Таким образом, будет вызван метод configure из внешнего контекста.В этом случае из контекста multibranchPipelineJob.Поскольку метод branchSources не был завершен к моменту вызова метода configure, ни один из исходных узлов ветвлений не был создан, и, таким образом, блок конфигурации создаст новый узел.

Попробуйте вместо этого:

multibranchPipelineJob('foo/bar') {
  branchSources {
    git {
      remote('https://github.com/jenkinsci/job-dsl-plugin.git')
      credentialsId('github-ci')
      includes('JENKINS-*')
    }
  }
  configure {
    def source =  it / 'sources'(class: 'jenkins.branch.MultiBranchProject$BranchSourceList') / 'data' / 'jenkins.branch.BranchSource'
    source << traits {
      foo('bar')
    }
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...