How to add "single conditional steps" under build section using dsl script - jenkins

I'm currently trying to develop a DSL script that can create a jenkins job with all required plugins and options.
I think I've almost completed all the section. But, I stuck up under build section where I've to include "conditional steps (single)" under Build.
Actually what I wanted is this
But, what I get is this
Here's the code that I used,
job('Sample_dev') {
steps {
conditionalSteps {
condition {
alwaysRun()
}
}
maven {
goals('install')
}
}
}

You have done few mistakes there:
Using multi-step DSL for achieving single step.
Pushed maven outside context like individual step.
Wrong DSL for Maven Step declaration.
Try following
job('Sample_dev')
{
steps{
singleConditionalBuilder{
condition{
alwaysRun()
}
buildStep {
maven{
targets('install')
name('')
pom('')
properties('')
jvmOptions('')
usePrivateRepository(false)
settings {
standard()
}
globalSettings {
standard()
}
injectBuildVariables(false)
}
}
runner {
fail()
}
}
}
}
The creator has deployed most on this url https://jenkinsci.github.io/job-dsl-plugin. But I would suggest you install in you local instance and access it via http://<your-jenkins-host>:<port> /plugin/job-dsl/api-viewer/index.html as Job DSL support auto generation so there is bright chance that plugin not listed above still has DSL support.

Related

matrix trigger mode in extendedEmail - Jenkins job DSL

I've a multi configuration project on Jenkins(ver. 2.222.1) We use JenkinsDSL(v1.77) to create the free-style jobs from the groovy scripts. Email notifications is with extendedEmail(v2.69)
It seems like trigger mode for notifications cannot be specified in the groovy script but it could be done via the UI as below(ONLY_PARENT or ONLY_CONFIGURATIONS or BOTH)
As I see here, below are the configurations we could specify for extendedEmail in groovy for DSL plugin
job('example') {
publishers {
extendedEmail {
recipientList('me#halfempty.org')
defaultSubject('Oops')
defaultContent('Something broken')
contentType('text/html')
triggers {
beforeBuild()
stillUnstable {
subject('Subject')
content('Body')
sendTo {
developers()
requester()
culprits()
}
}
}
}
}
}
It does not say anything in case of multi configuration project where I need the notifications to be sent only if parent job fails
I believe it exists on hipChatNotifier where we could specify as matrixTriggerMode
Is there a way to do this via extendedEmail publisher as we could do it on the UI?
Seems that this isn't supported by the Job DSL plugin, yet.
You can work around by using a custom configure block. For example
publishers {
extendedEmail {
...
}
}
configure { project -> project / 'publishers' / 'hudson.plugins.emailext.ExtendedEmailPublisher' << {
matrixTriggerMode('ONLY_PARENT')
}
}
Note the << to append a node to the publisher configuration instead of overwriting it.

How to pass paramaters to a pipelineJob in DSL

I have very similar pipeline jobs that have only differences are parameters. The goal is to create these jobs by passing parameters in a DSL script without any code duplication.
I followed this article. So If you run the DSL script below after you implemented the steps as mentioned in the article, my script able to run.
TL;DR
In that article adds a shared library and also have Jenkinsfile use that shared library.
I have a very similar approach. The difference is I want to create my build jobs via DSL and, changes default parameters of the Jenkinsfile by settings on the DSL.
The question is how can I pass/override parameters in the Jenkinsfile.
// BTW I'll run this code below in a loop. Open for any suggesstion
pipelineJob('AwesomeBild') {
description("A pipeline created by dsl")
definition {
cpsScm {
scm {
git {
remote { url('https://github.com/jalogut/jenkinsfile-shared-library-sample.git') }
branches('master')
// how can I pass params to the file
scriptPath('Jenkinsfile')
extensions { }
}
}
}
}
}
Edit
Parameters worked well. Here is the lastest version of the DSL file.
pipelineJob('AwesomeBild') {
description("A pipeline created by dsl")
parameters {
stringParam( "key", "value" )
}
definition {
cpsScm {
scm {
git {
remote { url('https://github.com/jalogut/jenkinsfile-shared-library-sample.git') }
branches('master')
// how can I pass params to the file
scriptPath('Jenkinsfile')
extensions { }
}
}
}
}
}
The solution is simple:
just use $key, but leave the single quotes:
scriptPath('Jenkinsfile$key')

Jenkins DSL API for copyartifact permissions

I am trying to add a call to my jenkins job dsl that will configure the job to give permission to another build to copy artifacts. However, I am unable to find a command for it in the Jenkins Job DSL API:
https://jenkinsci.github.io/job-dsl-plugin/
Here is the option I am trying to set using the DSL:
Does this command exist? Is there anyways to setup my groovy to do this if it doesnt?
There is no built-in DSL to set that permission, but you can use the Dynamic DSL.
The Job DSL API viewer can be opened at http://localhost:8080/plugin/job-dsl/api-viewer/index.html where localhost is your Jenkins host. Search for copyArtifactPermission as an example:
job('example') {
properties {
copyArtifactPermissionProperty {
projectNames('one, two')
}
}
}
is it this one?
job('example') {
steps {
copyArtifacts('upstream') {
includePatterns('*.xml', '*.properties')
excludePatterns('test.xml', 'test.properties')
targetDirectory('files')
flatten()
optional()
buildSelector {
latestSuccessful(true)
}
}
}
}
EDIT
It seems this may have been fixed in the google group for job-dsl
configure { project ->
project / 'properties' / 'hudson.plugins.copyartifact.CopyArtifactPermissionProperty' / 'projectNameList' {
'string' "*-foo"
}
}
I think they may have changed the interface though and you need to provide explicit job names now, but I haven't got the plugin so I can't check

Jenkins project generated by Job DSL is not triggered.

I have a project, named Demo, which doesn't do anything in particular.
I have a DSL script, like the following:
def gitUrl = 'GIT_URL'
job('unit-tests') {
scm {
git(gitUrl)
}
triggers {
buildResult('H/* * * * *') {
combinedJobs()
triggerInfo('Demo', BuildResult.SUCCESS, BuildResult.UNSTABLE)
}
}
}
Now what I'm wanting to do, is that when the Demo project runs successfully (it checks out a PHP application from Github), I want the unit-tests job to run.
Currently, when the Demo project is built, the unit-tests job never gets run.
I'm guessing my DSL script is incorrect, but I'm not sure why
I can reproduce your problem. The check box is not set when running the seed job for the first time. But it's set after running the seed job a second time. Must be a problem in the BuildResultTrigger plugin. Please file a bug report in the Jenkins JIRA: https://issues.jenkins-ci.org/projects/JENKINS
But you do not necessarily need to use the BuildResultTrigger plugin. You can use the built-in "Build after other projects are built" option, see https://jenkinsci.github.io/job-dsl-plugin/#path/job-triggers-upstream.
job('unit-tests') {
triggers {
upstream('Demo', 'UNSTABLE')
}
}
Use upstream which adds the "Build after other projects are built" trigger. see https://jenkinsci.github.io/job-dsl-plugin/#path/job-triggers-upstream
def gitUrl = 'GIT_URL'
job('unit-tests') {
scm {
git(gitUrl)
}
triggers {
buildResult('H/* * * * *') {
upstream('Demo', 'UNSTABLE')
}
}
}

Jenkins job dsl and MSTest integration

Jenkins Job DSL plugin is an extremely nice way to store CI config in repo and vary it from branch to branch.
The question is - is there a natural or close to natural way to run MSTest tests, parse results and display them.
Right now I do a powershell call, but that gives me only logs, not UI integration.
def testSomeProjectJob = job(testSomeProjectJobName) {
steps {
powerShell("& ${vstest} '${root}/SomeProject/SomeProject.Tests/bin/Debug/SomeProject.Tests.dll' ")
}
}
May be there is a publisher or a trick with templating, or some tips of writing a plugin to JOB DSL for that
UPD: final script template for MSTest and VSTest using #daspilker answer, jenkins xUnit Plugin and archiveXUnit
job('RunTests') {
steps {
// VSTEST
powerShell("& ${vstest} 'path/to/Tests.dll' /logger:trx ")
// Or MSBUILD
powerShell("& ${msbuild} /testcontainer:'path/to/Tests.dll' ")
}
publishers {
archiveXUnit {
msTest {
pattern('**/*.trx')
// deleteOutputFiles()
}
}
}
}
Using a PowerShell step is a good start. Install the xUnit Plugin to parse and display the results. It can parse all sorts of test results including MSTest. And you can use the DSL to configure the plugin.
Example:
job('example') {
steps {
powerShell('...')
}
publishers {
archiveXUnit {
msTest {
pattern('path/to/test/results')
}
}
}
}
Its for VSTest but I had to end up using the configure block to be able to use it in the DSL jobs.
static Closure useVsTest(List<String> dlls) {
return {
it / 'builders' << 'org.jenkinsci.plugins.vstest__runner.VsTestBuilder' {
vsTestName 'VS 14.0'
testFiles dlls.join('\n')
settings ''
testCaseFilter ''
enablecodecoverage false
useVsixExtensions true
platform 'x86'
otherPlatform ''
framework 'framework45'
otherFramework ''
logger 'trx'
otherLogger ''
cmdLineArgs '/TestAdapterPath:"."'
failBuild true
}
}
}

Resources