How can I get the joblist in jenkins after I execute the jobdsl - jenkins

How can I get the job list in jenkins after I execute the jobdsl ?
Jenkin JobDSL is nice to manage the jenkins jobs. When you execute the jobDSL, jenkins can help to generate the jobs are expected. Even more, if the job is created, you can choose to skip or overwrite.
Now I want to trigger the build directly after it is new generated.
See sample console output from jenkins build.
Processing DSL script demoJob.groovy
Added items:
GeneratedJob{name='simpliest-job-ever'}
Existing items:
GeneratedJob{name=’existing-job'}
How can I get the jobname simpliest-job-ever in jenkins? And in this case, I don't want to build existing-job
Scan the console log could be choice, but it is not elegant enough.

You can trigger a build from the DSL script using the queue method (docs).
job('simpliest-job-ever') {
// ...
}
queue('simpliest-job-ever')

Related

How to run a job from a Jenkins Pipeline on the same executor (declarative syntax)

I want to use the Jenkins "PRQA" plugin, which seems not to have the option to use it from a pipeline. The plugin would run static code analysis and publish the results.
In my case, it requires some preparations that are already done in a pipelinejob. Because of that, I want to include the job into that pipeline, but on the same executor with the data prepared by the pipeline as some kind of inlined job-step.
I have tried to create a job for the PRQA-Plugin-Step and execute this with the build step from the pipeline. But this tries to start the job on a new executor (and stalls because I have only one executor).
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Prepare'
}
}
stage('SCA') {
steps {
//Run this without using a new executor with the Environment that exists now
build 'PRQA_Job'
}
}
}
}
What is the correct way to run the job on the same executor with the current working directory.
With specified build 'PRQA_Job' it's not possible to run second job on the same executor (1 job = 1 executor), since main job just waiting for a triggered job to be finished. But you can run another job on the same agent with more than 1 executor to reach workspace from main job.
For a test porpose specify agent name in both jobs: agent 'agent_name_here'
If you want to use plugin functionality for a plugin, which has no native pipeline support, you could try using "step: General Build step" feature for Jenkins Pipelines. You can use the Pipeline Syntax wizzard linked in the Job configuration windows to generate the needed Pipeline description.
If the plugin does not show up in the "step: General Build step" part of Jenkins you can use a separate Job. To copy all the needed files/Data into this second Job you will require to use Archive Artifact/Copy Artifact functionality of Jenkins to save files from your Pipeline build.
For more information on how to sue Archive Artifact/Copy Artifact see https://plugins.jenkins.io/copyartifact/ and
https://www.jenkins.io/doc/pipeline/tour/tests-and-artifacts/

How can i add a post build step to an existing job using job dsl?

How can i add a post build step to an existing job using job dsl ?
Note: I need to append to the existing job. It should not delete the existing steps.
You can not append something to an existing job. You need to code the complete job definition in Job DSL.
But you can use the Jenkins API to add a post build step:
FreeStyleProject job = Jenkins.instance.getItem('job-a')
job.publishersList << new hudson.tasks.BuildTrigger('job-b', false)
You can try the code in Jenkins Script Console.
Note that a post build step will be added each time you run the script. If you code the complete job definition in Job DSL, the Job DSL engine will modify the job only if the script changed or your job configuration does not match the definition.

Jenkins Job DSL Script For creating a folder creation job

When i run Jenkins Jobs that runs DSL script as a built step to generate folder in jnkins it shows unreferenced Items which contains the job details of previously build of the same job. Is there aby way to get rid of the Unreference Item
DSL Script :
folder('project-e') {
displayName('project-e')
description('Folder for project e')
}
Console output :
Processing provided DSL script
Added items:
GeneratedJob{name='project-e'}
Unreferenced items:
GeneratedJob{name='project-b'}
No, you can not get rid of the unreference items output. But you can ignore it if you set "Action for removed jobs" to "Ignore" in the "Process Job DSLs" build step.
Job DSL scripts are not meant to be one-time generator scripts. A script should describe a part of your Jenkins configuration. If you want to add a folder, you add a folder to the script. If you want to delete a folder, you delete the folder from the script. Then you run the Job DSL seed job to apply the changes.

How can I analyze console output data from a triggered jenkins job?

I have 2 jobs 'job1' and 'job2'. I will be triggering 'job2' from 'job1'. I need to get the console output of the 'job1' build which triggered 'job2' and want to process it in 'job2'.
The difficult part is to find out the build number of the upstream job (job1) that triggered the downstream job (job2). Once you have it, you can access the console log, e.g. via ${BUILD_URL}consoleOutput, as pointed out by ivoruJavaBoy.
How can a downstream build determine by what job and build number it has been triggered? This is surprisingly difficult in Jenkins:
Solution A ("explicit" approach): use the Parameterized Trigger Plugin to "manually" pass a build number parameter from job1 to job2. Apart from the administrational overhead, there is one bigger drawback with that approach: job1 and job2 will no longer run asynchronously; there will be exactly one run of job2 for each run of job1. If job2 is slower than job1, then you need to plan your resources to avoid builds of job2 piling up in the queue.
So, some solution "B" will be better, where you extract the implicit upstream information from Jenkins' internal data. You can use Groovy for that; a simpler approch may be to use the Job Exporter Plugin in job2. That plugin will create a "properties" (text) file in the workspace of a job2 build. That file contains two entries that contain exactly the information that you're looking for:
build.upstream.number Number of upstream job that triggered this job. Only filled if the build was triggered by an upstream project.
build.upstream.project Upstream project that triggered this job.
Read that file, then use the information to read the console log via the URL above.
You can use the Post Build Task plugin, then you can get the console output with a wget command:
wget -O console-output.log ${BUILD_URL}consoleOutput

What are seed jobs in Jenkins and how does it work?

What are seed jobs in Jenkins and how does it work ?
Can we create a new job from seed job without using github ?
That depends on context. Jenkins itself does not provide "seed jobs".
There's plugins that allow creating jobs from other jobs, like the excellent Job-DSL plugin. With that, you can create jobs where a groovy script creates a larger number of jobs for you.
The Job-DSL plugin refers to those jobs as "seed jobs" (but they're regular freestyle or pipeline jobs). The Job-DSL plugin does not require a github connection.
The seed job is a normal Jenkins job that runs the Job DSL script; in turn, the script contains instructions that create additional jobs. In short, the seed job is a job that creates more jobs. In this step, you will construct a Job DSL script and incorporate it into a seed job. The Job DSL script that you’ll define will create a single freestyle job that prints a 'Hello World!' message in the job’s console output.
A Job DSL script consists of API methods provided by the Job DSL plugin; you can use these API methods to configure different aspects of a job, such as its type (freestyle versus pipeline jobs), build triggers, build parameters, post-build actions, and so on. You can find all supported methods on the API reference site.
The jobs we used for creating new jobs are called Seed Jobs and this seed job generates new jobs using Jenkins files (using JobDSL plugin).
Here, we disabling this feature (Enable script security for Job DSL scripts)
Jenkins Dashboard→ Manage Jenkins → Configure Global Security
Way to create seed job :
JobDSL scripts for generating new jobs.
Job1.groovy
job("Job1"){
description("First job")
authenticationToken('secret')
label('dynamic')
scm {
github('Asad/jenkins_jobDSL1', 'master')
}
triggers {
gitHubPushTrigger()
}
steps {
shell ('''
echo "test"
''')
}
}
buildPipelineView('project-A') {
title('Project A CI Pipeline')
displayedBuilds(5)
selectedJob('Job1')
showPipelineParameters()
refreshFrequency(60)
}
and create same way others Job2.groovy and so on.
For Jenkins Job DSL documentation:-
Follow https://jenkinsci.github.io/job-dsl-plugin/
Think about a job - what is it actually ?
It is actually just a java/jre object that represents like this
How you generates such job/build ?
Configure Jenkins UI -> rest api to Jenkins url -> Jenkins service receive your call on the relevant endpoint -> calling to the relevant code/method and generate this new job
How Seed job will make it ?
Configure seed job on Jenkins UI only once -> run this seed job - > this code run against the internal Jenkins methods and skip all the manual process describes above
Now, when your code can talk directly to Jenkins code , things are much easier.just update your code on the relevant repo - and you are done

Resources