I have Jenkins Pipeline jobs, where the only difference between the jobs is a parameter, a single "name" value, I could even use the multibranch job name (though not what it's passing as JOB_NAME which is the BRANCH name, sadly none of the envs look suitable without parsing). It would be great if I could set this outiside of the Jenkinsfile, since then I could reuse the same jenkinsfile for all the various jobs.
-
I am also interested in a solution for this. This is a major problem if we use normal pipeline or blueocean(declarative) pipeline. I have already this issue open - "https://stackoverflow.com/questions/46291750/jenkins-blueocean-pass-add-environment-variable-using-jenkins-job-ui-without-j" – vivekyad4v Feb 01 '18 at 09:14
-
Whats wrong with the `project parameters`? – SV Madhava Reddy Feb 01 '18 at 12:21
-
@SVMadhavaReddy what are you talking about? there's a `pipeline parameters` but I don't see a way to set those outside of the Jenkinsfile itself, there might be, but I can't find it. – xenoterracide Feb 01 '18 at 12:27
-
you can configure `job name` as a `project parameter` and access it in your pipeline script. `node {echo "Job Build Id : $BUILD_ID"}` this is an example. `BUILD_ID` is an env var. the same way your branch name can be parameter. if i understood your question correct. – SV Madhava Reddy Feb 01 '18 at 12:42
-
@SVMadhavaReddy my job configuration doesn't include anything called a "Project Parameter" also I'm using the declarative pipelines which don't use `node` – xenoterracide Feb 01 '18 at 12:48
-
Did you consider putting this pipeline code in global shared libraries and call that library and pass your changing parameter? – dot Feb 06 '18 at 21:49
3 Answers
Add this to your Jenkinsfile:
properties([
parameters([
string(name: 'myParam', defaultValue: '')
])
])
Then, once the build has run once, you will see the "build with parameters" button on the job UI.
There you can input the parameter value you want.
In the pipeline script you can reference it with params.myParam

- 2,342
- 17
- 34
-
is there a way to reference it outside of $1? can you reference it by name? – xenoterracide Feb 09 '18 at 21:20
-
-
`WorkflowScript: 14: The properties section has been renamed as of version 0.8. Use options instead. @ line 14, column 5.` where does this code go? I just added it inside the pipeline block – xenoterracide Feb 12 '18 at 19:27
-
so I figured out the actual syntax for the declarative pipeline. However using it, the configuration parameter only appears in the branch, I can't seem to configure it for the global job. That isn't really helpful since I wanted to configure it per job, not per branch. – xenoterracide Feb 12 '18 at 21:22
-
Multibranch pipelines will always configure the build per branch, as it references the Jenkinsfile in the root of that branch. If you want it to be in all branches, you need to promote the changes across all branches, or inherit them. – bp2010 Feb 13 '18 at 08:21
Basically you need to create a jenkins shared library example name myCoolLib
and have a full declarative pipeline in one file under vars, let say you call the file myFancyPipeline.groovy
.
Wanted to write my examples but actually I see the docs are quite nice, so I'll copy from there. First the myFancyPipeline.groovy
def call(int buildNumber) {
if (buildNumber % 2 == 0) {
pipeline {
agent any
stages {
stage('Even Stage') {
steps {
echo "The build number is even"
}
}
}
}
} else {
pipeline {
agent any
stages {
stage('Odd Stage') {
steps {
echo "The build number is odd"
}
}
}
}
}
}
and then aJenkinsfile that uses it (now has 2 lines)
@Library('myCoolLib') _
evenOrOdd(currentBuild.getNumber())
Obviously parameter here is of type int, but it can be any number of parameters of any type.
I use this approach and have one of the groovy scripts that has 3 parameters (2 Strings and an int) and have 15-20 Jenkinsfiles that use that script via shared library and it's perfect. Motivation is of course one of the most basic rules in any programming (not a quote but goes something like): If you have "same code" at 2 different places, something is not right.

- 9,691
- 5
- 31
- 42
-
if I have 2 stages that are the same except, lets say, the stage name, how could I reuse that code? is this shared library the right way? what would that code look like? – xenoterracide Feb 12 '18 at 17:37
-
-
And yes, shared library is the right way. So far I ran into some limitations, but nothing that would make me distribute the library in any other way... – cantSleepNow Feb 12 '18 at 17:41
-
well, I mean you can already pass the stage name, I guess I'm just not sure what the syntax looks like to store the ... stage in a variable, or something so it can be reused. e.g `def myStage = { steps { echo "${env.STAGE_NAME}" }` and `stage('Odd Stage') myStage` would print "Odd Stage" – xenoterracide Feb 12 '18 at 17:47
-
maybe clearer, lets say you called the stage `odd` and `even` and instead of copying the thing you wanted to write `echo "The build number is ${stage}"` how would you do that such that the code for the 2 stages was shared except for the name – xenoterracide Feb 12 '18 at 17:50
-
I'm also not sure about the syntax for this particular case and I can't verify right now. Maybe in 1-2hrs or so. I never had the need for a dynamic stage name.... I think I've once set build description dynamically, maybe with `currentBuild.description=something` – cantSleepNow Feb 12 '18 at 17:53
-
well you can definately access the stage name with `${env.STAGE_NAME}` in a `script`, I know I did it in some copy and paste, what I haven't figured out is how I can reuse the contents of the stage in another stage, so I'm not rewriting the exact same "docker, build, push" 3 times in a row. – xenoterracide Feb 12 '18 at 19:23
-
I see, that's a bit different. Take a look at my question here https://stackoverflow.com/questions/45306383/how-to-use-library-in-an-imported-groovy-script-in-jenkins-declarative-pipeline – cantSleepNow Feb 12 '18 at 19:31
-
So it would mean that you have a groovy script where you define your "stage function" and just call it 3 times – cantSleepNow Feb 12 '18 at 19:34
There is an option This project is parameterized in your pipeline job configuration. Write variable name and a default value if you wish. In pipeline access this variable with env.variable_name

- 230
- 2
- 18