'Jenkins pipelines set defaultValue on parameter dynamically
I have jenkinsfile and I want to set defaultValue on the imageTag dynamically, which I'm fetching from the pom file.
Here is the file:
def gr
pipeline {
agent any
image = readMavenPom().getParent().getVersion()
parameters {
string(name: 'imageTag', defaultValue: image, description: 'Docker image tag')
}
stages {
stage('Environment') {
steps {
script {
gr = load 'src/ci/script.groovy';
echo("Using imageTag: ${params.imageTag}")
}
}
}
}
I'm getting an error:
startup failed: WorkflowScript: 10: Not a valid section definition: "image = readMavenPom().getParent().getVersion()". Some extra configuration is required. @ line 10, column 5. image = readMavenPom().getParent().getVersion() ^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310) at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085) at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603) at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581) at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558) at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298) at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268) at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688) at groovy.lang.GroovyShell.parse(GroovyShell.java:700) at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142) at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127) at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:571) at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:523) at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:337) at hudson.model.ResourceController.execute(ResourceController.java:97) at hudson.model.Executor.run(Executor.java:429) Finished: FAILURE
I found this solution and it works, but the main idea was setting defaultValue dynamically directly on parameter
stage('Environment') {
steps {
script {
gr = load 'src/ci/script.groovy';
version = readMavenPom().getParent().getVersion()
if (!params.imageTag) {
imageTag = version
}
echo("Using imageTag: ${imageTag}")
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
