12

I have python script which does a REST command and processes the result. I want this script to be used by different Jenkins Pipelines, one way I found through Jenkins official documentation is to use 'Shared library' and that examples(also others example which I found online) they use the Groovy.

My question is, is it possible to create a shared lib in other language than Groovy? For ex. python?

Mohammed
  • 637
  • 13
  • 26

1 Answers1

26

Short answer is no. All Jenkins Pipeline execution (right now) is specialized Groovy that is executed with the Pipeline: Groovy plugin that uses the Groovy CPS library to perform the compilation and runtime transformations. The Jenkins Pipeline ecosystem is very heavily tied to Groovy. That may change in the future, but is not what worth the effort right now.

What you can do, if you really want to use Python code in a Shared Library, is to put it in the resources/ folder of the library and then load and execute it with pipeline steps. Your use case for why you want to use Python is not stated (or what problem you are trying to solve), so below is a contrived example:

  • In your Shared Library: resources/com/mkobit/sharedlib/my_file.py

      #!/usr/bin/env python
      print("Hello")
    
  • Shared Library Groovy global variable: vars/mkobitVar.groovy

      def runMyPython() {
        final pythonContent = libraryResource('com/mkobit/sharedlib/my_file.py')
        // There are definitely better ways to do this without having to write to the consumer's workspace
        writeFile(file: 'my_file.py', text: pythonContent)
        sh('python ./my_file.py')
      }
    
  • In a consumer

      @Library('mkobitLib') _
    
      node('python') {
         mkobitVar.runMyPython()
      }
    
mirekphd
  • 4,799
  • 3
  • 38
  • 59
mkobit
  • 43,979
  • 12
  • 156
  • 150
  • 2
    thanks for the wonderful reply, I am using Python because its easier to do a curl command using python script and I am not fluent in Groovy. I will try the way you mention, once again thanks. – Mohammed Nov 21 '17 at 16:51
  • 1
    @mkobit, great answer indeed. Would you suggest this approach for someone starting a new pipeline Shared Library project and who would rather use Python to make this library less coupled to Jenkins CI? (and also more easily unit-testable) – clipitar Apr 24 '20 at 19:02
  • Hey, I've been trying this exact answer, I also wrote some unit tests for this and I'm running into issues with this: `MissingMethod Exception: `groovy.lang .MissingMethodException: No signature of method: foobar.writeFile() is applicable for argument types: (LinkedHashMap) values: [[file:foobar.py, text:"python code"]]`. Any idea what could be the issue here @mkobit – Matti Kettu Jul 03 '20 at 07:27
  • @MattiKettu did you figureout the issue? – user2700022 Oct 20 '22 at 20:47
  • Yes, except that you can't execute Python files without the interpreter. So let's get rid of the `chmod` but add `**python** ./my_file.py` – mirekphd Aug 15 '23 at 09:52