Build every branch but only those newly pushed to - jenkins

I want to have Jenkins CI test every branch but not all existing one, solely the ones which received a recent push.
I have set up a GitHub web hook which triggers new builds. This works fine for the branch specifier set to master. Now I tried ** so every branch is built.
The problem: on the first push it tries to build every branch, which is simply too much and would take ages. Is there a way to limit this?

There is no configuration which supports the feature you are requested and since it looks like there is no plugin yet. Your best shot would be to implement your own plugin that can specify a certain date threshold for branches.
Having said that, to solve your problem I would simply:
Empty the shell script for your job
Trigger a new build
Set your shell script again
This way all remote branches get checked out, run and marked as successful within no time due to the empty shell script. After re-enabling your actual shell script you are good to go.

Related

Gitea Jenkins plugin: discovering branches to build

Gitea recommends a separate Gitea Plugin for Jenkins. I'm puzzled why two identical builds are triggered when a PR is created.
I'm trying to achieve the following:
Without a PR, a push to a branch should NOT trigger a build
Every time a PR is requested in Gitea, a build is triggered for the PR.
If a new revision pushed to the branch for which a PR is created, another check build should be triggered
It's kinda working... But for some reasons two builds are created. Could someone please explain, what are these pipeline/head and pipeline/pr-master builds, and why there are two of them?
Here is the relevant part of my Jenkins configuration. I understand that this selection is a "legacy" one, however it's the only one that allows me to build only on PR. If I select the "recommended" one, then every push triggers a build, which is not what I want.
Thanks!
Answering my own question. Awwww what a silly sausage I am. The only thing that needed to be done was removing the "Discover branches" behaviour. And, naturally, it stops discovering "just branches" :) For some reasons I did not realise I can remove the default behaviours.

Using Multibranch Pipeline Jenkins job, is it possible to run branch indexing without re-running existing branch builds

I'm setting up a new Jenkins job using multibranch pipeline and I have noticed that when a branch is deleted, it only has a strikethrough and isn't actually removed on Jenkins. This is solved by re-running branch indexing. However, I cannot really use this as it will also cause every other branch to rebuild (a consequence of how the repository is updated). Is there some custom code or pipeline/script I can run to re-index without building?
I've already looked at various UI methods such as suppressing SCM triggers, but this also negates push events from Github which is something we want to use.
The deleted/merged branch build will disappear after a period of time (<24 hours). It is not removed immediately to show the recently deleted/merged branches and give a chance to review the prior build statuses. It is relatively harmless since the jobs for these branches are deactivated (read-only).
Note that the removal is based on the branch indexing job running at regular interval, so if you have this disabled, it probably won't do it (not sure the SCM webhook calls are enough).

Can Jenkins job watch one repo for changes, to trigger a build on another repo?

Using Jenkins and pipelines;
I have one repo which drives deployments and cloning its Jenkinsfile should kick off a new build; but I need the jenkins job which wraps it to watch another repo representing the site (not its build process), to watch for and build changes to the site, expected to change often, rather than the build process which hopefully will soon be stable and not subject to much additional change.
Can anyone please advise me how it is I would accomplish this feat?
Use the jenkins build step.
In the repo experiencing the changes, have its Jenkins job kick off a https://jenkins.io/doc/pipeline/steps/pipeline-build-step/ for the repo you want to build. You can configure the build step to either wait or not wait for the result of the kicked off job.
build(job: "org/${jobName}/${BRANCH_NAME}",
parameters: [
new StringParameterValue('ENV', env),
new StringParameterValue('ENV_NO', env_no),
],
propagate: false,
wait: false,
)
Usually I‘d recommend to have the Jenkinsfile in the very same repository like your source code. Only that way you will have a combined history so it‘ll be much easier to reproduce an older build from - let’s say - one year ago.
However if you still want to go for the separation: The git/checkout steps will usually have the possibility to add a hook in Jenkins so the job will get triggered automatically on changes.
If I did understand your use case correctly the Jenkinsfile will go into stable state. If it’s stable it won’t change. When there are no changes it won’t trigger the job, right?
If that still is not enough I think I would need more details on what you’re trying to achieve and why.

Jenkins: launch same job with different parameters

I have a job to maven build our project, we now have one job per release version. As the number of releases grows, there are too many jobs and very hard to find the one we need.
I wonder if there is a way to launch the same job with different parameters? The problem is one job only has one workspace, so I'm not sure if it's possible?
Thanks.
Use This build is Parameterized option to build the jobs. Using this you can build the same job for different parameters. You will be asked to enter the parameter before building or you can also give a default parameter and you can have multiple parameters.
It is good the archive the artifacts which you need later.
You can also have the option keep build forever, this will keep the builds permanently Ir-respective of the number of builds to keep.
To use above option you should enable Discard old build option.
You can also link your repository directly to Jenkins which will trigger the job whenever a new commit is made to master or a new tag is created.

Manually failing a build after it's complete

Is it possible to set the build result for a build after that build is complete?
I could not find any plugins that do this already, and I was considering writing my own, but I wanted to see if this was even possible before going down that path.
(I have looked at existing code and how the "Fail The Build" plugin works as an example, but my understanding of the Jenkins code base is not advanced enough to understand what all the possibilities are.)
Use case: we have a build pipeline, and near the end of the pipeline there is a deploy-to-qa step that deploys the artifact to a QA environment. We have automated tests before this step to try to catch any problems with the artifact, but our test coverage is not very high in some areas so bugs could still slip through the cracks. I'd like to have the ability to mark a deploy-to-qa build as FAILED after the fact, to denote that that particular pipeline was invalid and is not a candidate for production release. (Basically the same as this Build Pipeline Plugin issue)
After some more investigation in the code, I believe that this is not possible.
From hudson.model.Run:
public void setResult(Result r) {
// state can change only when we are building
assert state==State.BUILDING;
// snip
...
}
So the build result cannot change except when in "building" state.
I could try to muck with the lastSuccessful and lastStable symlinks (as is done with the delete() function in hudson.model.AbstractBuild), but then those would be reset as soon as Jenkins reloaded the build results from jobs/JOBNAME/builds/.
I have an untested suggestion: Make a parametrized build, where the parameter determines if build will fail or not (for example simple bat / shell script testing the parameter from the environment variable it sets, and doing exit 0 or exit 1). This assumes that build pipelines manually triggered step will ask the parameters, and not use default values.
If it does not support interactive build parameters, then some other way is needed to tell this extra build step wether it should fail or not. Maybe editing upstream build description or display name to indicate failure, and then allowing build pipeline to continue to this extra build step, which probably has to use system groovy script to dig out upstream build description or display name.
I have seen several debates on this topic previously, and the outcome was always that it is theoretically possible to do so, but the codebase is not designed to allow this and it would have to be a very hacky workaround.
It's also been said that this is a bad practice in general, although I don't remember what the argument against it was.
I am facing the same requirement. I haven't found an appropriate plugin, changing the build status is not just a flag but has other impacts on links (eg latest successful build etc). So instead of changing the status of the build I looked for a possibility for qualifying the build. The Promoted Builds Plugin apply flags to build to define e.g. different quality stages. Build promotions can be performed manually or based on e.g. downstream project successful builds. Any successful build can be qualified, based on the promotion additional build and post build actions can be executed, e.g tagging or archiving.
Actually I was able to do it by changing the build.xml manually to <result>FAILURE</result>.
I've then played a little bit with mklink to create some symbolic links and also renamed the lastSuccessfulBuild to lastFailedBuild and it worked. If you are allowed to access the filesystem from within a Jenkins PlugIn, then it is possible to write one.
In case you are fine to delete the current build and start the same build using a version number and setting the next BUILD_NUMBER to the deleted one, then you could use this plugin to tell it to fail instead of succeed:
Fail The Build Plugin

Resources