Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jenkins's built in method in class?

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?

like image 472
Gabriel Wu Avatar asked Nov 07 '18 11:11

Gabriel Wu


People also ask

How do you use try catch in Jenkins pipeline?

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.

How do you skip failed stage in Jenkins pipeline?

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)

What is $class in Jenkins pipeline?

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.

Which pipeline approach is used in Jenkins as a best practice?

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.


1 Answers

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
        }
    }
}
like image 73
Szymon Stepniak Avatar answered Nov 05 '22 03:11

Szymon Stepniak