Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use Jenkins libraryResource to read file from resources?

I'm unable to use libraryResource to load a resource using Jenkins. I've found plenty of example of people using libraryResource to load resources but no one that has the specific errors i'm getting so i assume that i'm missing something but i've no idea what that might be.

I'm trying to load a python script that will be reused by almost all jobs. I've placed a simple test script in resouces/org/foo/test.py which as far as i understand i should be able to read using:

libraryResource('org/foo/test.py')

But i get the error:

hudson.AbortException: No such library resource org/foo/test.py could be found.

I use a pipeline script that is loaded from SCM that is configured in the projects configuration in jenkins. This is the structure of the git repository from which the pipeline script is loaded.

-src
  -org
    -shared
      +Utilities.groovy
-jobs
  -nighly
    +nightly.groovy
-resources
  -org
   -foo
    +test.py

Loading the Utitilies.groovy using shared library at the root of nightly.groovy

@Library('shared-utilities')
import org.shared.Utilities

What I've tried:

  • Running libraryResource outside of stages, in stages and on nodes
  • Checking out scm before running libraryResource
  • Different paths foo/test.py, resources/org/foo/test.py

Based on the output from checkout scm the latest commit containing resources/org/foo/test.py is being fetched and it's there on the job root on the agent.

I'm out of ideas and I appreciate any help/suggestion that you have. I really would like to avoid having to commit this script to all projects that use it since its only used by Jenkins.

like image 859
T.Nielsen Avatar asked Nov 27 '18 12:11

T.Nielsen


People also ask

How does Jenkins shared library work?

A shared library in Jenkins is a collection of Groovy scripts shared between different Jenkins jobs. To run the scripts, they are pulled into a Jenkinsfile. Each shared library requires users to define a name and a method of retrieving source code.

How do you call a shared library in Jenkins?

Create a separate git repo for the Jenkins pipeline library & push the shared library code to that repo. Integrate the shared library repo in Jenkins under the Manage Jenkins section. Create Jenkinsfile in the project. In that Jenkinsfile, Import & use the shared library.

Where does Jenkins checkout shared library?

If the shared library is loaded from SCM and your workspace path is jenkins/workspaces/jobName , then a copy is checked out to jenkins/workspaces/jobName@libs or similar (might be suffixed with a number if that path is occupied by another concurrent build).

What is JSL in Jenkins?

JSL is usually a semantically versioned gradle groovy project kept in a git repository which is git cloned by Jenkins and put on a classpath when a pipeline job using a Jenkinsfile request for it.


1 Answers

What you're missing is the context/scenario under which usage of libraryResource API becomes applicabLe.

libraryResource: Load a resource file from a shared library Reads a resource from a shared library and returns its content as a plain string.

So if you had provisioned a shared library, then you may use libraryResource inside your shared library to read a resource in located in resource directory of the same shared library.

Hope that clears things up better.

like image 119
Igwe Kalu Avatar answered Nov 03 '22 01:11

Igwe Kalu