Conditional Post-Build step in Jenkins without plugins - jenkins

For my post-build step, I will be executing Sonar analysis (from SonarQube plugin) and a script that I wrote. I don't want this to run all the time and want to trigger the post-build step with a boolean parameter. Is there anyway to do this? Ideally, I want to have a checkbox that determines if the Post-Build step will execute in the Build-with-Configurations page.

You could use the Promoted Builds Plugin to start further steps after a build. This can be done either manually (I think that's what you're looking for) or based on some logic you've defined.

Related

Jenkins - Pre-build event

How is that possible to add Pre-Build actions in Jenkins, exactly like it's Post-Build ?
I've came across Conditional BuildStep, Run Condition and Parameterized Trigger plugins, but can't find out a way to add that.
Sadly there is no real pre-build event because not every pipeline may build something.
You can disable the default checkout by using the option skipDefaultCheckout and manually do the git checkout later using the command checkout scm. I think this is the closes you will get to reach a Pre-Build-Stage.

Trigger another job if the existing build fails with timeout

In Jenkins, I have used the Build-time plugin to timeout and abort the build if the build takes more than a certain specified time.
Is there a way to trigger another job immediately when this particular job fails with timeout where I can make my work process more efficient.
You can accomplish this by installing the Parameterized Trigger Plugin on Jenkins.
(https://wiki.jenkins.io/display/JENKINS/Parameterized+Trigger+Plugin)
Once installed, as a Post-build Action, add the Trigger parametrized build on other projects, from here you can select the Failure option that you need to trigger the next job.

Jenkins Parameterized Build with Jenkinsfile from SCM

We have some jenkins pipeline jobs defined with Jenkinsfile's located in bitbucket server, as described here. These builds are parameterized, and we'd like to be able to manually run them with non-default parameters.
The problem is, since the Jenkinsfile isn't checked out until we run it, the first time we run the build the build button is just "Build Now" instead of "Build with Parameters". Currently we are running it once with the default values so that it fails, and then running it again with the "Build with Parameters" button so we can pass in what we want.
Obviously not ideal. What is the right way to do this so we can run it with custom parameters the first time?
This is not possible currently, as they are post-processed they need to be executed in the 1st run before being known to jenkins and being available as 'Build with parameters'. Issue tracked here: https://issues.jenkins-ci.org/browse/JENKINS-41929
There are various ways to handle this,
The first is as you have alluded to, run it automatically/manually and let it fail, though maybe if you could set working defaults so it at least succeeds?
Another option, is to evaluate if this is the first run or not, and if so, execute the Jenkins job skipping all steps and purely processing the parameters.

Trigger Jenkins Build with "Poll SCM" AND "Build after other projects are built"

I'd like to trigger the nightly cq build ONLY if last commit build was successful.
Currently I have the cq build being triggered with a scm poll at midnight, but I would like to include the additional condition that another build (the last commit build) was successful. Because it makes no sense letting the slow cq build run just to inevitably fail.
The build triggers section of a build have multiple ways of triggering a build including when another build is finished, but as far as I can tell these triggers happen if any one of them is selected I want to effectively AND the "Build after other projects are built" and "Poll SCM" conditions together.
I've looked at https://wiki.jenkins-ci.org/display/JENKINS/Conditional+BuildStep+Plugin
but that's about putting conditions on a build step, not conditionally triggering the build as a whole.
and
https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Trigger+Plugin
but that's about launching another build from another.
Does anyone know how to do this?
Regards,
Phil.
I think you want the Build Result Trigger plugin

jenkins post build step and action - what is the difference

Might sound like a very basic question - but I am unable to find any article which explains why Jenkins provides a post build step as well as action.
In Jenkins - I do see that the options are different in post build step vs. action, but
what is the order of execution?
When should we use which option?
What are the best practices?
At a glance, here is Jenkins' job workflow (without additional plugins)
[On demand, if needed] Install tools, (such as JDK, Ant, Maven, etc).
[Optional] Perform SCM checkout, (such as SVN or Git).
Perform build step(s), (such as build Ant project, compile code, etc).
[Optional] Perform post-build steps(s), (such as Archive Artifacts, send email, etc).
There are plugins that allow to perform actions right after Tools installation, such as Pre-SCM-step and EnvInject plugins. There are also plugins that add a lot more possible Build Steps and Post-build steps.
The difference between Build and Post-build steps is based partially on logical separation, partially on workflow configuration.
From the logical perspective, when you build/compile a project, that's a "Build" step, whereas when you Archive Artifacts, since that happens after the build, that's a "Post-build" step.
But there is also workflow configuration considerations, and it has everything to do with:
"When do the the builds fail", and with
"Build Status" (such as Success, Unstable, Failed)
When there are multiple "Build" steps, Jenkins:
Executes the first build step
Checks for exit code of the first build step
If exit code is 0 (success), Jenkins continues to next build step (if there is one)
If exit code is not 0 (failure), Jenkins marks build as FAILED, and continues to Post-build actions.
Jenkins executes Post-build steps (regardless if build was marked FAILED or not)
So, in other words:
If Build step succeeds, Jenkins can execute the following Build steps (if any).
If Build step fails, Jenkins will not execute the following Build steps.
If all Build steps succeeded, Jenkins will mark build SUCCESS.
If any Build step failed, Jenkins will mark build FAILED.
Post-build steps are executed regardless of Build Status (FAILED or not).
Technically, all post-build steps should be executed at all times, however in practice, if a Post-build step exceptions, the job never completes which can lead to some Post-build steps not being executed.
Also, generally Post-build steps do not change the Build Status, but there are some that are specifically designed to do that (for example, when Archiving Artifacts, you can choose to mark build FAILED if not all artifacts are found, even if after all Build steps, the build was marked SUCCESS)
So, knowing the above, it's your responsibility to design your job, and decide what steps need to be executed one after another, only if previous was successful, and will affect the Build Status (i.e. Build steps), and what steps need to happen at all times regardless of result (i.e Post-build steps).
EDIT:
Since I keep getting comments, here is a screenshot of a brand new clean installation of Jenkins (Windows) ver.1.634 (as was mentioned in comments).
On the screenshot, note the following:
New Freestyle project.
Scrollbar all the way down, there is nothing more on page.
Version 1.634 (as requested).
Build section with Add build step drop down.
Post-build Actions section with Add post-build action drop down.
So, to re-iterate my previous comment:
There is only one post-build "anything"
whether you want to call it "step" or "action" (Jenkins changed the labeling over the years).
Custom plugins can add a lot of extras, but for a clean install a basic job is just as I have described.
For an item (job) created as a Maven project build steps are not configurable (only the maven goals are configurable for the build), so two new extra categories are available to configure: pre-build step and post-build step.
In my experience pre/post-build step are used to execute actions that can influence the build result (such as Sonar Analysis), and post-build actions for actions to be performed based on the build's result (such as e-mail notifications).
Clarification. Post build Step can be set to only execute when build has a specific status. i.e. if failed do something (send logs to someone?) if passed or unstable do something. i.e. tag the source so that it can be promoted for release?
Post build action is ALWAYS executed. There is where you send emails, archive artifacts, send artifacts to master (if using slaves and have the plugin), etc.
I use the Post build Step to tag/label the successfully built source code in my GIT workspace. I use a small script to do that.
Now I just wish that Post Build step was available in Freestyle jobs since it is such a great option for only tagging successful builds.

Resources