Options(Plugins) in build section work for post build in jenkins - 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

Related

Ignore failure of post build step in Jenkins

I have Jenkins job with summary plugin step in post build actions. Sometimes it fails because of absence of the necessary file for it(for some branches, and there is no ability to update them nearest time). Is it possible to ignore failure only in post build steps and mark build green?
Finally I found solution - in build steps add shell step with command "touch summaryName.xml". If file exists it won't be changed. And if there is no summary file it will be created
Yes, you can ignore the failure by ticking the check box [Do not fail this build if archiving returns nothing] in advanced section.

How can I add a Jenkins post action to execute two different scripts once the build successed or failed exclusively?

I want to execute a shell scripts if the Jenkins job build successful or another scripts if the Jenkins job build failed.
I added the post build task plugin, but it seems only can execute a shell in all status or just successful status, cannot specify another shell script to be run once build failed, the two scripts should be run exclusively.
are there anyone can help me on this?
use Post Build Task plugin for conditional steps
I use Conditional BuildStep Plugin and Text Finder Run Condition Plugin to execute steps when the build fails.
But there's a limitation with this method: You have to find a text that is displayed ONLY when your build fails.
See the pictures below:
I add exit 0 at the end of the script to force the build to be successful so that the program can execute the next step, which is Conditional steps(multiple).
You will see Execute script after a failed build. being displayed on console output when the job fails.
This is the execution result.
You will also need to find a text that is displayed ONLY when the first step succeeds so that you can trigger another script.

Writing a Jenkins groovy script -- how to determine which build step failed

I am attempting to write a custom pre-send script for Jenkins email-ext plugin. I'm at the point where I can write a simple 'hello world' groovy script and run it with the "Email template testing" tool for the job I want to customize.
Alas, I'm having a bit of trouble navigating Jenkins build API. As a starting point, the job whose email I want to customize has N build steps. I only want to send an email if one of those steps faild, and I'd like to write a different message depending on which step failes.
How can determine the number of steps in the build (N), and which step failed?
Which build step caused the build result to change is only recorded in the console output, e.g.
Build step 'Execute shell' marked build as failure
This isn't otherwise stored as part of the build record, and so isn't available via the API.
So you would have to try scraping the console output to do what you want.
To determine how many build steps there are, you'd need to fetch the job configuration via /job/foo/config.xml and count how many items there are within the <project><builders> tag (assuming a freestyle project).

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