Jenkins Job DSL:如何在Configure块中构建特定节点?

时间:2015-12-08 17:09:35

标签: jenkins jenkins-job-dsl

我想要的特别之处是阻止在Flexible Publish部分执行某些步骤。我使用字符串(不)匹配的条件,我不想在检查失败后执行某些操作。

手动配置,预期步骤如下所示: Expected postbuild step

由于我在Jenkins Jobs DSL API中找不到合适的方法,我尝试使用配置块重现它。参考文献说我可以使用' project'对于作业的根元素和节点'用于将子项附加到它们的特定节点。它还说如果存在尖端节点,则不会再次创建新节点。所以这是我的配置:

job("flexible_condition") {

  publishers {

    flexiblePublish {
      configure { node ->
          node / publishers /
          'org.jenkins__ci.plugins.flexible__publish.ConditionalPublisher' << 'runner'(class:
          'org.jenkins_ci.plugins.run_condition.BuildStepRunner$DontRun')
        }

      condition {
        not { stringsMatch('string_placeholder', '', false) }
      }

      publisher {
        debianPackage('common') {
          commitMessage('new feature')
        }
      }

      publisher {
        git {
          pushOnlyIfSuccess(true)
          branch('origin', 'master')
        }
      }
    }
  }
}

尽管在游戏中我的jenkins中没有引用所希望的xml。我已经重复了节点,似乎节点&#39;节点&#39;被解释为&#39; project&#39;并始终把它放在根。

<!-- 1. flexible_condition -->
<project>
    <actions></actions>
    <description></description>
    <keepDependencies>false</keepDependencies>
    <properties></properties>
    <scm class='hudson.scm.NullSCM'></scm>
    <canRoam>true</canRoam>
    <disabled>false</disabled>
    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
    <triggers class='vector'></triggers>
    <concurrentBuild>false</concurrentBuild>
    <builders></builders>
    <publishers>
        <org.jenkins__ci.plugins.flexible__publish.ConditionalPublisher>
            <runner     class='org.jenkins_ci.plugins.run_condition.BuildStepRunner$DontRun'></runner>
        </org.jenkins__ci.plugins.flexible__publish.ConditionalPublisher>
        <org.jenkins__ci.plugins.flexible__publish.FlexiblePublisher>
            <publishers>
                <org.jenkins__ci.plugins.flexible__publish.ConditionalPublisher>
                    <condition     class='org.jenkins_ci.plugins.run_condition.logic.Not'>
                        <condition     class='org.jenkins_ci.plugins.run_condition.core.StringsMatchCondition'>
                            <arg1>string_placeholder</arg1>
                            <arg2></arg2>
                            <ignoreCase>false</ignoreCase>
                        </condition>
                    </condition>
                    <publisherList>
                        <ru.yandex.jenkins.plugins.debuilder.DebianPackagePublisher>
                            <repoId>common</repoId>
                            <commitMessage>new feature</commitMessage>
                            <commitChanges>true</commitChanges>
                            </ru.yandex.jenkins.plugins.debuilder.DebianPackagePublisher>
                        <hudson.plugins.git.GitPublisher>
                            <configVersion>2</configVersion>
                            <pushMerge>false</pushMerge>
                            <pushOnlyIfSuccess>true</pushOnlyIfSuccess>
                            <forcePush>false</forcePush>
                            <tagsToPush></tagsToPush>
                            <branchesToPush>
                                <hudson.plugins.git.GitPublisher_-BranchToPush>
                                        <targetRepoName>origin</targetRepoName>
                                    <branchName>master</branchName>
                                </hudson.plugins.git.GitPublisher_-BranchToPush>
                            </branchesToPush>
                        </hudson.plugins.git.GitPublisher>
                    </publisherList>
                    <runner class='org.jenkins_ci.plugins.run_condition.BuildStepRunner$Fail'></runner>
                </org.jenkins__ci.plugins.flexible__publish.ConditionalPublisher>
            </publishers>
        </org.jenkins__ci.plugins.flexible__publish.FlexiblePublisher>
    </publishers>
    <buildWrappers></buildWrappers>
</project>

我已经通过Job DSL版本1.40在Playground中查看此示例,但我的jenkins中的1.39给出了相同的结果。

出了什么问题?

谢谢。

UPD 我已将配置块置于SevenEleven建议的下方,几乎有所帮助。跑步者节点现在位于正确的位置,但仍然是重复的。

<publishers>
    <org.jenkins__ci.plugins.flexible__publish.ConditionalPublisher>
        <condition .../>
        <publisherList>
            ...
        </publisherList>
        <runner class='org.jenkins_ci.plugins.run_condition.BuildStepRunner$Fail'></runner>
        <runner class='org.jenkins_ci.plugins.run_condition.BuildStepRunner$DontRun'></runner>
    </org.jenkins__ci.plugins.flexible__publish.ConditionalPublisher>
</publishers>

UPD 2 虽然xml中有两个不同的行,但较新的运行器会替换生成的作业中的默认行。所以我得到了预期的结果。感谢。

UPD 3 。好极了!发现现在可以简单地使用JobDSL语法并且它可以工作。

publishers {

  flexiblePublish {
    conditionalAction {
      condition {
          not { stringsMatch('string_placeholder', '', false) }
      }

      publishers {
        debianPackage('common') {
          commitMessage('Automatic Commit')
        }
        git {
          pushOnlyIfSuccess(true)
          branch('origin', '$GIT_BRANCH')
        }
      }

      runner('DontRun')
    }
  }
}

1 个答案:

答案 0 :(得分:2)

编辑配置,您必须在下面放置configure - 块,而不是在发布商配置中:

job("flexible_condition") {
    publishers {
        flexiblePublish {
        //...
        }
    }

    configure { 
        it / publishers / 'org.jenkins__ci.plugins.flexible__publish.FlexiblePublisher' / publishers / 'org.jenkins__ci.plugins.flexible__publish.ConditionalPublisher' /  'runner'(class:'org.jenkins_ci.plugins.run_condition.BuildStepRunner$DontRun') {
        }
    }
}