I'm developing a Jenkins shared library.
Directory structure as below:
project
- src
--- Operations.groovy
- vars
--- entry.groovy
Now in entry.groovy my code is:
import Operations;
def call(body) {
def operation=new Operation();
podTemplate(xxxxxx) {
node(nodelabel){
operation.stage_checkout()
}
}
}
And in Operations.groovy:
class Operations {
def stage_checkout(){
stage('Checkout') {
checkout scm
}
}
}
When I tried and run it in Jenkins, I got error like below:
GitHub has been notified of this commit’s build result
groovy.lang.MissingPropertyException: No such property: scm for class: Operations
Possible solutions: ui
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:39)
at
I think "checkout" is a Jenkins plugin built-in method. Is there any correct way or a guide that can help me to use Jenkins built-in method correctly?
try/catch is scripted syntax. So any time you are using declarative syntax to use something from scripted in general you can do so by enclosing the scripted syntax in the scripts block in a declarative pipeline. So your try/catch should go inside stage >steps >script.
To ignore a failed step in declarative pipeline you basically have two options: Use script step and try-catch block (similar to previous proposition by R_K but in declarative style)
Here is a reference that briefly explains the syntax. Basically, you are providing the step() function a map of arguments in the form of name-value pairs. The first argument which is especially denoted by the name $class tells the function which class (plugin) to instantiate.
Using a shell script within Jenkins Pipeline can help simplify builds by combining multiple steps into a single stage. The shell script also allows users to add or update commands without having to modify each step or stage separately.
You can use built-in Jenkins pipeline steps through the reference to workflow script. You can pass a reference to Operations
class through the constructor by passing this
object. Consider following example:
vars/entry.groovy :
import Operations;
def call(body){
def operation=new Operation(this); // passing a reference to workflow script
podTemplate(xxxxxx){
node(nodelabel){
operation.stage_checkout()
}
}
}
src/Operations.groovy :
class Operations {
private final Script script
Operations(Script script) {
this.script = script
}
def stage_checkout(){
script.stage('Checkout') {
script.checkout script.scm
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With