Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create 'shared library' in Jenkins Pipeline in other language than Groovy?

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?

like image 213
Mohammed Avatar asked Nov 20 '17 22:11

Mohammed


People also ask

Can we use Python in Jenkins pipeline?

Create your initial Pipeline as a Jenkinsfile. You're now ready to create your Pipeline that will automate building your Python application with PyInstaller in Jenkins. Your Pipeline will be created as a Jenkinsfile , which will be committed to your locally cloned Git repository ( simple-python-pyinstaller-app ).

Which language is used in Jenkins pipeline?

Pipelines are Jenkins jobs enabled by the Pipeline (formerly called “workflow”) plugin and built with simple text scripts that use a Pipeline DSL (domain-specific language) based on the Groovy programming language.

Where do you configure a shared global library in Jenkins?

In Jenkins, go to Manage Jenkins → Configure System. Under Global Pipeline Libraries, add a library with the following settings: Name: pipeline-library-demo. Default version: Specify a Git reference (branch or commit SHA), e.g. master.


1 Answers

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('chmod +x my_file.py && ./my_file.py')
    }
    
  • In a consumer

    @Library('mkobitLib') _
    
    node('python') {
       mkobitVar.runMyPython()
    }
    
like image 86
mkobit Avatar answered Oct 11 '22 20:10

mkobit