running a shell script once build passes - jenkins

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

Related

how to configure job in jekins if first project is build successful then trigger another project and so on with "Multi-configuration project" job

I want to trigger project in jekins if first project is build successful then trigger another project and so on.. how can do it. I hava project1 , project2, and project3, project4, project5 so on. here I am configuring "Multi-configuration project" job.
for exp:- project1->success then trigger project2->success then trigger project3
if any stage failed then doesn't trigger other project.This is "Multi-configuration project" job, because this single job, I am trigger on multiple slaves remotely.
A simple way to do this would be to define this build pipeline using the Pipeline plugin.
This pipeline script should give you the desired behaviour:
build job: 'project1'
build job: 'project2'
build job: 'project3'
build job: 'project4'
build job: 'project5'
i.e. A build of each job will be started in sequence; if any one of them fails, then the subsequent jobs will not be started.
Even with "Multi-configuration project" you should be able to set a post-build action "Trigger parameterized build on other projects". Then you can specify condition which will define when this project should be build, and add parameters, including "build on the same node".

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.

Triggering the same jenkins job after the build is finished in 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.

Run Nant script as a Jenkins post build action

Is it possible to run a Nant script as a Jenkins post build action?
I get the option to run a script as a Build Step but not as a build action. Is there any particular plugin which enables this option.
The reason I am looking for this functionality is that I need to run a script which depends on the ArtifactDeployer post build action. If i specify the code in the build step it gets executed before the ArtifactDeployer and the build fails
You can use the Post Build Task Plugin
edit
One way of getting the build number if it's not working with this plugin is using the Groovy Post Build Plugin
With it you can execute groovy code as a post build action, get the build number and execute NAnt
the build number is accessible from the following property
manager.build.number
Post-build Actions -> Execute a set of script run after a build when it succeeds or fails. My experience shows that it only sometimes runs when a build is aborted.
As advised above, the Post-build Actions -> Post build task (via the named post task plug in) is always evaluated for run (regardless of the build exit status). Additional setting via phrase in the log ("Build was aborted") works reliable for me.
My problem was to run something even on an aborted build and post build task sorted out this problem.

Resources