Triggering the same jenkins job after the build is finished in Jenkins - jenkins

Is there a way to trigger the same job when the build is finished. I have one job that needed to be run until I aborted it manually. Is there a way to accomplish this?

The easiest way to do this is to add a post build step that builds the same project. Set "Post-build Actions" - "Build other projects" - "Projects to build" to the name of your project and it will loop forever.

This is a pretty crazy request. Are you sure that's what you want to do? If you just want to keep up to date you could just have the job build when the SCM system changes- even down to using the filesystem as an SCM.
If you really want to do it though, it is possible. You can't just tell it to trigger itself, but you can use the REST api.
Add a shell build step with the line
curl -X POST http://localhost:8080/job/Tester/build
and a new job will get scheduled each time you run build.

There is "Build after other projects are built" under "Build Triggers" in job configuration page in which you can specify which project to build after which project. So if you want to continuously run a particular job, you can add Project name i.e your job name in "Projects to watch" field under "Build after other projects are built" and can run it with options :- Trigger only if build is stable
Trigger even if the build is unstable
Trigger even if the build fails

you can try
add build step to create few files - file per condition set
add build step Trigger/call builds on other projects with add parameter factories with For every matching file, invoke one build
the called project might have input file as a parameter - it would be passed from the parent project from #2.

Related

How can I use the value from "Run Parameter" in "Copy artifacts from another project" for our Jenkins job?

I have a Jenkins job I want to use to run automation on a build created in another job. I want the user to specify that build job's build number using the drop-down box created by the Run Parameter plug-in and then have the automation job copy the artifacts from the user-specified build job.
These are the settings for Run Parameter:
Name: BUILD_SELECTOR
Project: Foo
Filter: All Builds
The user then selects a build like which is saved to BUILD_SELECTOR like "https://blah.com/job/Foo/4/"
Then later in my job, I have Copy artifacts from another project and I want to copy the artifact from the job selected above:
Project Name: Foo
Which build: ?
This is where I get stumped. I have tried the URL above as a permanent link and also tried stripping out the "4" above for Specific build, but nothing seems to work. How can I use that value to do what I need to do?
As stated in https://issues.jenkins-ci.org/browse/JENKINS-21519
You can use ${BUILD_SELECTOR_NUMBER} to get just the build number which you can use with specific build.

How to clean Jenkins workspace only if build fails

In the interest of reducing build times I would like to only clean down the Jenkins job workspace if a build fails. Is there of doing this using post build action? Does Jenkins set a variable to the effect of 'BUILD FAILED' that can be read in a script that could be use in the post build action section of the job config?
Thanks in advance.
Post-build Task plugin can read your console output and execute scripts if regex is found. When a build step fails, it will print "BUILD FAILED" into console output.
Configure post-build task to look for that, and execute the cleanup in that case.
Workspace Cleanup Plugin has an option just for that. Add its post build step, tick particular checkbox and it will clean only failed (unstable?) builds.

Jenkins MultiJob Plugin does not aggregate downstream test results

I am using the jenkins multijob plugin to execute a number of parallel builds in the same build phase and i want to display the test results in the main multijob project so i select a post-build action step to 'Aggregate down stream test results' and select both options 'Automatically aggregate all downstream tests' and 'Include failed builds in results' but when the jobs complete and i go into the main multijob project it shows 'no tests' under 'Latest Test Result' link...
Has anyone else encountered this issue? My downstream 'child' projects that run in parallel are multi-configuration projects.
As a previous poster indicated, this is an open issue in the Jenkins JIRA and does not work. There is a workaround to achieve what your looking for. Your going to need the Copy Artifact Plugin and to also Archive the test result files as Artifacts in your jobs that are doing the test runs.
After you have installed this and configured your test run jobs properly, go to your Multijob and after all your test phases add a build step "Copy artifacts from another project" for each of the jobs you want the test results from. You can use "Specified by permalink" and use the "Last build" permalink to always retrieve the latest artifacts. Select the artifacts you want to copy (i.e. *.xml), and input your target directory as something like "job1". If you add multiple build steps to copy artifacts from another project, just name your target directories for the copied artifacts something similar like "job2", "job3", etc.
Then select a Post-build action in your Multijob as you would to Publish JUnit test result report (or whatever you prefer) and input **/job*/*.xml (or similar).
This is what I did, and it works just fine. It is a bit manual in the setup, but it works great once its configured.

running a shell script once build passes

I have a job that is running unit tests on a project build and then ssh into a staging server and pulling down from the master branch. Right now I'm using the post-build-script but this is running regardless of pass/fail. I'm trying to use the parameterized build plugin to trigger a new job when the build is passed. So far I've created the new job and set to trigger in the configuration of the original.
The new job is building ok on its own but the original job isn't triggering it. From 'Add post-build action' I've selected 'Trigger parameterized build on other projects' with build triggers:
Projects to build: new_job, Trigger when build is: Stable or unstable but not failed.
Any ideas appreciated!
C
If you don't actually need to pass a parameter to the second build, make sure that "Trigger build without parameters" is checked in the parameterized build trigger options.
The "Post build task" allows you to query the console log of the build step, and is executed only when criteria is met.
Jenkins writes BUILD SUCCESSFUL in the console log for every build step that had passed.
In your "Post build task" step, under Log text just put BUILD SUCCESSFUL, and under Script put your linux script/commands.
This way your script/commands will only be executed if the Build Step was successful

Options(Plugins) in build section work for post build in jenkins

In jenkins, we have a requirement like having the options that appear under build step like (say)
Execute Shell
Execute commands over ssh
Invoke Ant
To appear in the post build section. And it should do the same work as it did under build section.
I made all the options in build section appear in UI under post build section by doing it.getbuilddescriptors in config.jelly of my plugin. And it appeared in my jenkins UI under post build section as a hetero list.
But the problem is I don't know how to make it work as a bost build step.
For shell we did
Shell s = new Shell(command);
s.perform(build, listener, launcher);
and it worked.
If this is possible, then it may even be possible for all build section items. Is there a direct way to do without doing as I did for 'Execute shell'?
You may consider looking at https://wiki.jenkins-ci.org/display/JENKINS/Any+Build+Step+Plugin
They provide ability to use Build Steps as Post-Build actions, and vice-versa

Resources