Jenkins - link a java package to pipeline

114 views
Skip to first unread message

Marogo Ytcutc

unread,
Apr 12, 2019, 10:55:08 AM4/12/19
to Jenkins Users
I would like to use the TestLinkAPIClient class (java) in my pipeline Jenkins, so I need link these modules to the pipeline, and then run the code below in groovy (found on the net, so it is unknown if it can work at all):

#!/usr/bin/env groovy
 
import testlink.api.java.client.TestLinkAPIResults.*
 
import testlink.api.java.client.TestLinkAPIClient.*

 
def DEVKEY = "1f123453b123bd8dd811a7f824c194d0"
 
def URL = "http://PC4/lib/api/xmlrpc/v1/xmlrpc.php"
 
def api = new TestLinkAPIClient(DEVKEY, URL)
 
TestLinkAPIResults projects = api.getProjects()
 api
.createTestProject(...)

Ideally, it would be possible to link these modules to the pipeline after downloading them from the local SVN (where my project is built in the job) to the job workspace. My pipeline need some reference to the location of these modules in the %workspace%\testlink\api\java\client folder.

Marogo

Ivan Fernandez Calvo

unread,
Apr 12, 2019, 12:41:23 PM4/12/19
to Jenkins Users
Hi,

There are many reasons to not include those classes in your pipeline code I will give you a couple, one it is that you will have to allow to execute a bunch of packages related to that library this would make your Jenkins less secure see https://wiki.jenkins.io/display/JENKINS/Script+Security+Plugin, the second it is related to how we should put into pipeline, the pipeline script language it is designed to orchestrate your pipeline launching your build, deploy, test, ... scripts or whatever, it is not recommended to introduce business logic into your pipeline code, my mate Jesse Glick explains it much better than me in the following keynote "Jenkins World 2017: How to Use Jenkins Less" https://www.youtube.com/watch?v=Zeqc6--0eQw

so my recommendation is to put that code in a script and execute this groovy script from the pipeline, you do not need to include that code in the pipeline.

Marogo Ytcutc

unread,
Apr 12, 2019, 2:18:53 PM4/12/19
to Jenkins Users

The main problem is that Jenkins does not recognize the TestLinkAPIClient class, probably because he did not find it in the given location (testlink.api.java.client.TestLinkAPIClient.*).
How to solve this problem?

Ivan Fernandez Calvo

unread,
Apr 13, 2019, 3:09:40 PM4/13/19
to Jenkins Users
Put the jar file in a folder inside the classpath of the JDK used by your agent

Marogo Ytcutc

unread,
Apr 13, 2019, 5:38:38 PM4/13/19
to Jenkins Users
Thank You very much for Your help! I will check it on Monday.

Marogo Ytcutc

unread,
Apr 15, 2019, 5:39:44 AM4/15/19
to Jenkins Users
Still doesn't work. :-( I will describe what I have done in turn.
I downloaded the file "testlink-api-client-2.0.zip" from:


After extract archive I placed the file "testlink-api-client-2.0.jar" in directory "D:\Jenkins\testlink\"

In the configuration of my job in Jenkins I marked the option "Prepare an environment for the run"
and set "Properties Content" to:

CLASSPATH=D:\\Jenkins\\testlink

My pipeline code in Jenkins job:

#!/usr/bin/env groovy
import groovy.json.JsonSlurper
import testlink.api.java.client.TestLinkAPIResults.*
import testlink.api.java.client.TestLinkAPIClient.*

node('PC-2')
{
 ansiColor('xterm')
 {
   try
   {
     echo "CLASSPATH: " + env.CLASSPATH
     def DEVKEY = "1f344123b456bd9dd822a7f824c194c3"
     def api = new TestLinkAPIClient(DEVKEY, URL)
   }
   catch(e)
    {
      String error = "${e}";
      currentBuild.result = 'FAILURE'
      emailext body: "Log: ${env.BUILD_URL}console", recipientProviders: [[$class: 'FirstFailingBuildSuspectsRecipientProvider'], [$class: 'CulpritsRecipientProvider'], [$class: 'RequesterRecipientProvider'], [$class: 'UpstreamComitterRecipientProvider']], subject: "Build #${env.BUILD_NUMBER} of job ${env.JOB_NAME} finished with status: ${currentBuild.currentResult}"
   }
 }
}

In console log I received an error:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 22: unable to resolve class TestLinkAPIClient
 @ line 22, column 18.
def api = new TestLinkAPIClient(DEVKEY, URL)
               ^

 Where is the problem?

kuisathaverat

unread,
Apr 15, 2019, 6:42:29 AM4/15/19
to jenkins...@googlegroups.com
the problem is that you are not running the code as Groovy script, you include the code in your pipeline script, it is not the same. 

save this as a Groovy script in your repo, something like scripts/my-testlink.groovy
#!/usr/bin/env groovy
 
import testlink.api.java.client.TestLinkAPIResults.*
 
import testlink.api.java.client.TestLinkAPIClient.*

 
def DEVKEY = "1f123453b123bd8dd811a7f824c194d0"
 
def URL = "http://PC4/lib/api/xmlrpc/v1/xmlrpc.php"

 
def api = new TestLinkAPIClient(DEVKEY, URL)

 
TestLinkAPIResults projects = api.getProjects()
 api
.createTestProject(...)

then use it on your pipeline

```
node(){
  sh(label: 'my script', script:'./scripts/my-testlink.groovy')
}
```

--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/70qz8g46LpQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/9f8c7cc0-66e5-4411-89e2-c384158d0131%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Marogo Ytcutc

unread,
Apr 15, 2019, 8:41:31 AM4/15/19
to Jenkins Users
I used example from this page to load groovy script from file to pipeline:


My Script.groovy file (placed in %workspace% directory):

import testlink.api.java.client.TestLinkAPIClient;
import testlink.api.java.client.TestLinkAPIException;
import testlink.api.java.client.TestLinkAPIResults;
 
 def exampleMethod()
  {
     println("exampleMethod message");

     def DEVKEY = "1f344123b456bd9dd822a7f824c194c3";
     TestLinkAPIClient api = new TestLinkAPIClient(DEVKEY, URL);
  }

  def otherExampleMethod()
  {
     println("otherExampleMethod message");
  }
return this

The significant part of my pipeline:

  def rootDir = pwd()
 println("Current Directory: " + rootDir)
        
 def example = load "${rootDir}\\Script.groovy"

 example.exampleMethod()
 example.otherExampleMethod()
 

When in method "exampleMethod()" is only "println("exampleMethod")" all works well
("exampleMethod message" and "otherExampleMethod message" is printed in console log,
but when I add to "exampleMethod()":

 def DEVKEY = "1f344123b456bd9dd822a7f824c194c3";
  TestLinkAPIClient api = new TestLinkAPIClient(DEVKEY, URL);

my job finish with "FAILURE" result, without info about reason of error.
Maybe the main reason is that the TestLinkAPIClient class is created in Java and not Groovy?
To unsubscribe from this group and all its topics, send an email to jenkins...@googlegroups.com.

Ivan Fernandez Calvo

unread,
Apr 15, 2019, 12:28:44 PM4/15/19
to Jenkins Users
I've suggested you run a Groovy script on your agent using the Groovy CLI, not to include your groovy code in your pipeline, if you make a load it is the same that put the code in the Jenkinsfile (more or less),  
Reply all
Reply to author
Forward
0 new messages