Jenkins Job DSL: Create Maven Job which calls another Job DSL file - jenkins

I am trying to create a Jenkins Job DSL which creates a Maven Project. The created Maven Project should execute another Job DSL which is in my Bitbucket Git repo. I am havin a hard time finding the right Job DSL tags/methods I need for achieving this goal. Can anyone help me out with this?
mavenJob('CD/cd-parent-master') {
scm {
git {
remote {
name('origin')
url('-------')
credentials('Bitbucket_Access')
}
}
}
description 'MavenJob which calls another Job DSL'
/* Tag which specifies that the created MavenJob should process another Job
DSL and the location of that Job DSL on my Jenkins FileSystem.*/
wrappers {
mavenRelease { }
}
jdk('JDK 8')
mavenInstallation('connecteddrive Maven 3')
goals('clean deploy')
}

Related

Configure job-dsl using scriptler

I'm going to write a freestyle job using job-dsl. job should build the scriptler script. I have experience writing a similar job for a pipeline. I don't know what it should look like for a job building scripter script .
pipelineJob('test') {
definition {
cpsScm {
scm {
git {
remote {
url('https://gitlab.com/repo.git')
credentials('cred')
}
branch('*/master')
}
scriptPath('src/test.Jenkinsfile')
}
lightweight()
}
}
}
A freestyle job just needs to run the script from the scriptler catalog. all. Nothing more.

Jenkins - Execute Pipeline from Specific folder

I have a main pipeline in jenkins that will checkout the code , compile, test and build, then push image to docker. This is the high level of CI pipeline that I have. Say job name "MainJobA"
I need to create a new job , just for JavaDoc generation. For that i have created a new script in Git Repo and configured the same in Pipeline job.
Now i need to execute this sub job of javadoc generation and publishing the html reports from the workspace of "MainJobA" . I need to run the SubJobA's pipeline stages from
/home/jenkins/workspace/MainJobA
How can i achieve this?
There is build step exist in jenkins declarative pipelines.
Use it like:
pipeline {
agent any
stages {
stage ("build") {
steps {
build 'Pipeline_B'
}
}
}
}

Jenkins DSL job configuring slave

I have a DSL plugin file which creates couple of jobs like pipeline, freshly jobs. I wanted to know what would be syntax (for dsl file only not jenkinsfile) that i can run these jobs on specific agent or slave. Code sample given below. I tried to use label('JenkinsEC2Slave'),But it is actually running my DSL job on slave, not the one which are created by DSL. Labels are from ec2 plugin and they should be launched on demand.
pipelineJob('Build_Docker_Images') {
label('JenkinsEC2Slave')
configure {
it / definition / lightweight(true)
}
triggers {
scm('#midnight')
}
concurrentBuild(false)
parameters {
stringParam('ECR_REPO', 'xxxxxxxxxxx.dkr.ecr.eu-west-2.amazonaws.com')
}
definition {
cpsScm {
scm {
scriptPath ('ci-cd/pipelines/base_docker_images/Jenkinsfile')
git {
branches('*/master')
remote {
url ('git#github.com:xxxxxxxxxx.git')
credentials ('jenkins-key')
}
}
}
}
}
You can use labels to select build agents in Jenkins. label is also the property of JobDSL, which allows you to specify labels for a job. Quoting the DSL viewer:
job('example') {
label('x86 && ubuntu')
}

Jenkins Job DSL: How to read Pipeline DSL script from file?

I want to generate my pipeline plugin based jobs via Job DSL, which is contained in a git repository that is checked out by Jenkins.
However, I think it is not very nice to have the pipeline scripts as quoted Strings inside of the Job DSL script. So I want to read them into a string and pass that to the script() function:
definition {
cps {
sandbox()
script( new File('Pipeline.groovy').text )
}
}
}
Where do I have to put Pipeline.groovy for this to work? I tried putting it right next to my DSL script, and also in the resources/ folder of my DSL sources. But Jenkins always throws a "file not found".
have you tried readFileFromWorkspace()? it should be able find the files you checkout from git.
Ref the Job DSL pipelineJob: https://jenkinsci.github.io/job-dsl-plugin/#path/pipelineJob, and hack away at it on http://job-dsl.herokuapp.com/ to see the generated config.
The Job DSL below creates a pipeline job, which pulls the actual job from a Jenkinsfile:
pipelineJob('DSL_Pipeline') {
def repo = 'https://github.com/path/to/your/repo.git'
triggers {
scm('H/5 * * * *')
}
description("Pipeline for $repo")
definition {
cpsScm {
scm {
git {
remote { url(repo) }
branches('master', '**/feature*')
scriptPath('misc/Jenkinsfile.v2')
extensions { } // required as otherwise it may try to tag the repo, which you may not want
}
// the single line below also works, but it
// only covers the 'master' branch and may not give you
// enough control.
// git(repo, 'master', { node -> node / 'extensions' << '' } )
}
}
}
}
You/your Jenkins admins may want to separate Jenkins job creation from the actual job definition. That seems sensible to me ... it's not a bad idea to centralize the scripts to create the Jenkins jobs in a single Jenkins DSL repo.

Jenkins job-dsl-plugin: how to inject environment variables to an Ivy job?

I have a Jenkins Ivy job that uses the Inject environment variables to the build process step. I am writing a DSL script so that I can dynamically create this job with the job-dsl-plugin plug-in.
I set up the following lines for this:
steps {
envInjectBuilder {
propertiesFilePath('/tmp/file')
}
}
but the steps method can only be applied to a free-style job and not to an Ivy job. I get this in the console output:
Processing DSL script ivyJob.groovy
java.lang.IllegalStateException: steps cannot be applied for Ivy jobs
Doesn't the DSL plug-in support EnvInject for an Ivy job? If it doesn't, is there a way I can do this programmatically? I know EnvInject is compatible with Ivy jobs since I can manually create the very job.
Thanks.
The EnvInject plugin allows to inject variables at several points in a build's lifecycle. A build step is only one possibility. For a Ivy project type the job property and wrapper options will work.
ivyJob('example') {
environmentVariables {
env('ONE', '1')
propertiesFile('env.properties')
keepBuildVariables(true)
}
wrappers {
environmentVariables {
env('ONE', '1')
envs(FOO: 'bar', TEST: '123')
propertiesFile('env.properties')
}
}
}
See the Job DSL API Viewer for details:
https://jenkinsci.github.io/job-dsl-plugin/#path/ivyJob-environmentVariables
https://jenkinsci.github.io/job-dsl-plugin/#path/ivyJob-wrappers-environmentVariables

Resources