1

I have a Jenkinsfile and it has two parallel stages. In one stage I want to call a function from python script.

Jenkinsfile

pipeline {
  agent any
    stages {
      stage('Main') {
        parallel {
          stage('Build') {
            steps {
              script {
                  ********I want to call python function here ********
              }
            }
          }
          stage('Test'){
              steps{
                  echo "It is a test stage."
              }
          }
        }
      }
    }
}

Python script:

class Build:

    def A():
        try:
            print("Build stage")
            return True
        except Exception as e:
            print(str(e))

I want to call this A() function there in my Jenkinsfile and want to get return value there. Is there any mechanism to do this.

sam
  • 203
  • 1
  • 3
  • 15
  • use `sh` or `bat` to start a python: https://stackoverflow.com/questions/3987041/run-function-from-the-command-line – daggett Aug 18 '20 at 19:08
  • I want to call the function in Jenkinsfile. sh or bat will only run the python script. – sam Aug 18 '20 at 19:29
  • Then you need to convert the logic from Python into something Jenkins Pipeline can natively interpret, such as Groovy. – Matthew Schuchard Aug 18 '20 at 20:25

1 Answers1

0
  1. change the logic in python script create object of class and call the method and return the value from method
script {
    sh"""
    variable = python python_script.py
    """
    }
Ankush Sahu
  • 578
  • 7
  • 13