Jenkins Job DSL Script For creating a folder creation job - jenkins

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.

Related

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.

Dynamic Properties file in Jenkins for injecting Env variables

So this is my entire Use case:
I have a parameterized build job which accepts file parameters. After the build I need to send a mail with that file and the size of the file. For this, I'm trying to add the name and size of the file as an Env variable using EnvInject Plugin.
But EnvInject is in the Build Environment step. The file parameter gets stored in the Workspace of the build only in the Build step, not in Build environment. So, there will be an error like File not found.
Due to which, I'm trying a crooked way of defining a properties file somewhere on my local system.I'm mentioning this properties file in "Properties File Path" of Inject Environment variables. In the build step I'm adding FOO=BAR and other values in the file so that I can use those values as my env variables down the line, like when I configure my e-mail template in Post Build Actions.
Can this process be done easily? I was initially making the properties file in JENKINS_HOME. I just got to know that I'm not allowed to do that as in master-agent architecture, JENKINS_HOME will be different and build will fail.
PS-1. The workspace gets deleted after every build
2. Any other plugins which can be used? If possible, please suggest without installing some new plugin as I'm not Jenkins admin
One way of solving this problem is by creating 3 different jobs as follows -
job 1 --> should call below 2 jobs(job 2 & job 3)
job 2:
Build --> Trigger/call builds on other projects --> job 2(block until the triggerred projects finish their builds)
select"Build on the same node" from Add Parameters.
job 3:
Build --> Trigger/call builds on other projects --> job 2(block until the triggerred projects finish their builds)
select"Build on the same node" from Add Parameters.
Job 2 (Create this job as follows):
Execution API --> using "GET_FILE" option you can download the required details on to the current working directory of your job.
Execute shell -->
now within "Execute Shell", download "consoleText" using wget command.
process the "consoleText" using unix command prepare a key-value pairs and store it under /tmp folder. i.e. "/tmp/env.prop"
Job 3 (Create this job as follows):
Bindings:
select "Inject environment variable to the build process" and under 'Properties File Path' enter "/tmp/env.prop"
now you can use the variable which you created in Job2 in the current job without any issue.
please note that it is important to select "Build on the same node" in Job 1, because this will preserve the data and it allows other jobs to access this information.
Let me know if its not clear.

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

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')

how to set up /configure test cases before triggering in jenkins

After the build is passed for source code, some .jar files are created.
Those jar files have to be put in specific path before triggering relevant testcases in Jenkins.
In a short,
How to configure/set up the test cases after the successful build and before triggering them in Jenkins?
Have 1 job to build your code.
Add a post exec command to this job to move the jar files to the correct path
After this build job is finished have it trigger a second job that does only the tests.

Jenkins builds loop list

I am trying to figure out a way to have one list of parameters, and have Jenkins create a job or run a build for each of the items in the list.
The parameter is a directory, so I have a list of directories, and I want it to work so for each of them, the build runs several steps - so basically for each directory, run git pull, ant command, ant command, ant command with the directory name, publish test results, next build.
I have looked at a bunch of plugins but I can't figure out how to do this to get it to go to the next item in the list until they're all done.
if I understand correctly you have on job? You can trigger it multiple times with different parameters (directory) by using BuildFlow Plugin. Create build flow job and inside this job call your job with different parameters. In build flow job you can trigger your job with parameters
build("AntJob", parDirectory: "C:\src1")
build("AntJob", parDirectory: "C:\src2")
you can also create smarter DSL and run this job in parallel
def dirTable = [ "C:\src1", "C:\src2", "C:\src3"]
def builds = []
dirTable.each{ d ->
def clr = { build("AntJob", parDirectory: d) }
builds.add(clr)
}
parallel(builds)</code>

Resources