Build Job with link to commit (not branch) - jenkins

I have the following code in my Jenkinsfile:
build job: 'project_1', propagate: false, wait: false
Which builds a separate project, which works great. However, I would like a link to the particular job. Unfortunately, it just links to the branch where it is building in master
[Pipeline] build (Building project_1)
Scheduling project: project_1 » master (link is here)
I believe when I set propagate: true, wait: true it would link to the job, but I cannot do this due to constraints in the project. Is there a way around this to get a link to the particular job without setting propagate or wait to true?

Related

Can I set up dependency builds in Jenkins similar to TeamCity?

I have not found information about that, I want to trigger build but before it executes, I want it to trigger some other pipelines and wait for a file created by these other pipelines to pass to a main Pipeline, can I do this in Jenkins?
You can, yes, in multiple ways depending on your actual use case.
The simplest way would be to create the job you want to call and then add a step for calling that job, copy artifacts from the job and then continue with the pipeline.
Using Jenkins Build step:
stage ('Child job') {
steps {
build(job: 'foo', wait: true, propagate: true, parameters: [parameters_list])
}
}
The parameter wait makes it so your pipeline waits for the child to complete run before continuing and propagate means that the result of the job is shown in the parent pipeline as well. The parameters section is needed only if your child job requires parameters. See the parameter types in the Build step documentation.
You can also use the Jenkins pipeline snippet generator to create the call properly in your own Jenkins instance.
To get any build artifacts from the child job, the easiest way to go is to use the Copy Artifact plugin. You'll need to archiveArtifacts in the first job and then
copyArtifacts fingerprintArtifacts: true, projectName: 'foo', selector: lastCompleted()
When the artifacts are needed.

Build a Jenkins pipeline (job) from another repo on another branch

What I am trying to do doesn't seem so complex but not so easy, but what I've read by now seems to make it look like I am launching rockets.
Basically let's say I have
my-deploys-repo with branches: master, develop
my-tools-repo with whatever branches
Inside my-tools-repo I have: Jenkinsfile-push-events ( jenkins pipeline )
Inside my-deploys-repo I have ON DEVELOP BRANCH: Jenkinsfile-deploy (which also gets some params, if it matters )
How can I trigger Jenkinsfile-deploy job from my-deploys-repo's develop branch FROM Jenkinsfile-push-events on my-tools-repo?
I understood that normally I'd do something like (inside Jenkinsfile-push-events)
stage('Deploy') {
steps {
script {
build job: '../my-deploys-repo/Jenkinsfile-deploy'
}
}
But it laying on another branch seems like a problem.
A Job in Jenkins has its definition in its properties and that already includes Jenkinsfile, so you cannot trigger a "Jenkinsfile" without defining a Job that uses that Jenkinsfile first.
If you have two branches, you need a Multibranch Pipeline job.
Let's say you created a new Multibranch Pipeline job — say MyJob is its name — that is configured to use your repo (my-deploys-repo.git) and your path to Jenkinsfile (Jenkinsfile-deploy.groovy). You can then trigger that job by:
build job: "MyJob/develop", wait: false, propagate: false,
parameters: [
string(name: 'PARAM_1', value: "1"),
string(name: 'PARAM_2', value: "maybe"), //etc.
]

Jenkins build job execute jobs in parallel how to create the programmatically without creating pipeline in jenkins GUI

i have 2 functions which I like to execute them in parallel, each job will run on slave or where there are enough resources. the problem is that I dont want to create pipelines in Jenkins GUI and then from that pipeline to execute my function. i like to be able to create the pipeline on the fly in code.
this is example what i have now :
//downstream job
build job: "my_job_pipeline_1",
parameters: [string(name: 'PROJECT_NAME', value: "${PROJECT_NAME}"),
propagate: false,
wait: true
//downstream job
build job: "my_job_pipeline_2",
parameters: [string(name: 'PROJECT_NAME', value: "${PROJECT_NAME}"),
propagate: false,
wait: true
This called from my main pipeline but for this to work I have to create 2 pipelines in Jenkins GUI
my_job_pipeline_2 and my_job_pipeline_1
can i create those pipelines programmatically ?
The Jenkins CLI allows you to create jobs from the terminal. Follow the documentation to set it up. Under Manage Jenkins > Jenkins CLI you can find the available commands (including "create job")

Github UI shows failing red X when Jenkins skips a build

I have a Jenkinsfile that contains a few logical checks for a commit to Github, and only after certain criteria are met, will it trigger a downstream build job. The relevant parts of the Jenkinsfile are below:
script {
if (... a bunch of conditions) {
echo 'Building because of on-demand job!'
build job: '/my/downstream/job', parameters: [gitParameter(name: 'BRANCH', value: env.BRANCH_NAME),
gitParameter(name: 'GIT_BRANCH', value: env.GIT_LOCAL_BRANCH)], wait: true, propagate: true
}
echo 'Skipping'
currentBuild.result = 'NOT_BUILT'
}
However, in my Github UI, anytime a job is skipped, it is rendered as a failure. For instance, when a commit is made that does not satisfy the condition, Jenkins correctly skips the build:
However, on the Github commit history, it shows as a failure:
I know this is somewhat trivial and literally only for appearances but it is quite aggravating to see so many red Xs. Is the best solution just to switch the currentBuild.result to SUCCESS? I am somewhat hesitant to do so since it's not technically a success (nothing was built) but I don't see another way of getting Github to not mark it as failed.

Jenkins multi-branch pipeline and specifying upstream projects

We currently generate a lot of Jenkins jobs on a per Git branch basis using Jenkins job DSL; the multi-branch pipeline plugin looks like an interesting way to potentially get first-class job generation support using Jenkinsfiles and reduce the amount of Job DSL we maintain.
For example we have libwidget-server and widget-server develop branch projects. When the libwidget-server build finishes then the widget-server job is triggered (for the develop branch). This applies to other branches too.
This makes use of the Build after other projects are built to trigger upon completion of an upstream build (e.g. libwidget-server causes widget-server to be built).
It seems that the multi-branch pipeline plugin lacks the Build after other projects are built setting - how would we accomplish the above in the multi-branch pipeline build?
You should add the branch name to your upstream job (assuming you are using a multi-branch pipeline for the upstream job too).
Suppose you have a folder with two jobs, both multi-branch pipeline jobs: jobA and jobB; jobB should trigger after jobA's master.
You can add this code snippet to jobB's Jenkinsfile:
properties([
pipelineTriggers([
upstream(
threshold: 'SUCCESS',
upstreamProjects: '../jobA/master'
)
])
])
(Mind that any branch of jobB here will trigger after jobA's master!)
I'm currently trying to get this to work for our deployment.
The closest I've got is adding the following to the downstream Jenkinsfile;
properties([
pipelineTriggers([
triggers: [
[
$class: 'jenkins.triggers.ReverseBuildTrigger',
upstreamProjects: "some_project", result: hudson.model.Result.SUCCESS
]
]
]),
])
That at least gets Jenkins to acknowledge that it should be triggering when
'some_project' get's built i.e it appears in the "View Configuration" page.
However so far builds of 'some_project' still don't trigger the downstream
project as expected.
That being said maybe you'll have more luck.
Let me know if it works for you.
(Someone else has asked a similar question here -> Jenkins: Trigger Multi-branch pipeline on upstream change )

Resources