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

孤人 提交于 2020-01-04 00:39:06

问题


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?


回答1:


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()
    }
    


来源:https://stackoverflow.com/questions/47402263/can-i-create-shared-library-in-jenkins-pipeline-in-other-language-than-groovy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!