Jenkins parameterized pipeline fail - jenkins

How should pipeline/jenkinsfile syntax look to be triggered by parameterized trigger via curl for example?
I'm having the pipeline that starts with:
pipeline{
parameters {
string(name: 'mycommitid', defaultValue: 'nocommit', description: 'my parameterized build')  
}
properties([
parameters([
string(defaultValue: 'nocommit', description: 'fas', name: 'mycommitid')
])
])
node{...}
}
By setting this in the code my pipeline won't be triggered, only if I set it manually in build triggers section in jenkins. But the goal is to use it in the multibranch pipelines and jenkinsfiles.
The output I'm getting is (the hash here is some random number i typed as example):
git rev-parse 4364576456fgds467734ss344c^{commit} # timeout=10
ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.
Finished: FAILURE
And having the commit passed, how do you advise should I build only that single revision?

When defining parameters in groovy pipeline script, you should define them before the groovy script, in the job configuration.
Once you created your parameters in the job configuration you can access them in the groovy, with the syntax ${env.PARAM}.

Related

How to get build number as a parameter for downstream job from upstream job with declarative pipeline code

I have Two jobs like one is CI jod & another one is CD. I want CI build number should use on CD number.. Can you please help me with declarative pipeline script to get build number as a parameter. here CI job is calling CD job.
Jenkins already provides a simple means to access the number of the current build using env.BUILD_NUMBER. So if you wanted to pass the build number of CI to the downstream job CD, you could do
build([
job : 'CD',
parameters: [
string(name: 'MAIN_BUILD_NUMBER', value: "${env.BUILD_NUMBER}")
]
])
Then in the CD job, declare a parameter like this:
parameters {
string(defaultValue: null, description: 'Build No', name: 'MAIN_BUILD_NUMBER')
}
You should then be able to use ${env.MAIN_BUILD_NUMBER} anywhere in your CD jobs' Jenkinsfile.

How can I select a Jenkins pipeline file from a branch specified in a build parameter

I have a parameterized Jenkins pipeline build. One of the parameters (branch) is the branch to build. The pipeline file is stored in the branch.
In the Pipeline definition, when I use */${branch} as the branch to build in place of */main the ${branch} does not get replaced but shows up as a literal. If I hard code the branch it works as expected.
The ${branch} does get replaced as expected in the pipeline file. So the branch parameter is being set.
Is there a way to get the parameter value into the "Pipeline script from SCM" retrieval from git?
You can try another approach as following:
In pipeline configuration page, change pipeline from SCM to pipeline script
Put following pipeline script in input box
node('<Jenkins node label>') {
properties([
parameters([
// parameter for branch
])
])
git url: '', credentailId:'', branch: "${branch}"
load '<relative path to your Jenkinsfile>'
}
You can use the branch parameter, when you disable the "Lightweight Checkout".
See JENKINS-28447

How to access pipeline name in the freestyle job

I have a pipeline which triggers a jenkins freestyle job.
How to access the pipeline name from the freestyle job.
I tried using ${BUILD_CAUSE} but it shows the value as UPSTREAMTRIGGER.
How to get the pipeline name?
JOB_NAME may contain the name of the job that you're after, unless it was triggered by an upstream job.
See a list of generic Jenkins environment variables that are available in most builds. The best way to find out what environment variables are available is to run sh 'printenv' in the steps.
If you need the name of the upstream job in a downstream job, I'd suggest setting a parameter on the downstream job and then referencing it when you trigger the job.
e.g. Upstream Pipeline job:
...
step {
build job: 'freestyle-downstream-job', parameters: [
string(name: 'upstream_job_name', value: "${JOB_NAME}")
]
}
...

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 /buildWithParameters POST request fails on multi-branch project because "This build is not parameterized"

Using Jenkins latest, I have a multi-branch project with a declarative Jenkinsfile. The jenkins file has parameters defined:
Jenkins snippet
pipeline {
agent any
parameters {
string(name: 'P_A', defaultValue: 'a', description: 'a')
string(name: 'P_B', defaultValue: 'b#b.com', description: 'b')
}
stages {
In my multi-branch project configuration, I have disabled SCM triggering when a new branch is found. I want to run the pipeline on demand via the following call to /buildWithParameters.
Request
POST http://127.0.0.1:9000/job/multibranchprojectname/job/jobname/buildWithParameters
Content-type: application/x-www-form-urlencoded
token=&P_A=test&P_B=c#c.com
Reponse
HTTP 500
java.lang.IllegalStateException: This build is not parameterized!
For parameters defined in the pipeline Jenkins does not recognize them if the pipeline has not been executed at least once.
Under the "General" tab of the Jenkins job in the jenkins web app, you must select the checkbox labeled "This project is parameterized" and then create String Parameters (one of the available types of parameters once this check box is selected) which you are trying to pass values to.

Resources