RAJ
RAJ

Reputation: 17

How to pass the Groovy variable to python program in Jenkinsfile

I need to pass the groovy variable to python module.

I have tried below code but it is not working. In echo it is printing the results but when passing it to python it is taking as empty value.

stage('Run python code') {
steps {
    script {
        ACTIVE_CONNECTORS = sh(script: """
              test="a,b,c"
              echo "**********test value*********"
              echo \$test
              TEMP=\$(python -c \
              'import json; print(${test}); SPLIT_PUB=${data}; JSON_SPLIT_PUB=json.dumps(SPLIT_PUB); \
               JSON_SPLIT_PUB=JSON_SPLIT_PUB.replace("rename_value",$"{test}"); \
               JSON_SPLIT_PUB=JSON_SPLIT_PUB.replace("name","${params.app}");
               """.stripIndent(), returnStdout: true).trim()

                }

            }
  }

Upvotes: 1

Views: 1955

Answers (1)

Brahim Ben Amira
Brahim Ben Amira

Reputation: 261

You can use an environment variable and then call it inside the python program using os.environ. An example:

    stage('Test') {
        steps {
            script{
                 env.TEST = 'a,b,c'
                 ACTIVE_CONNECTORS = sh(script: """
                                                python -c "import os;\
                                                           print(os.environ['TEST'])"
                                                """.stripIndent(), returnStdout: true).trim()
                 println ACTIVE_CONNECTORS
            }
        }
    }
                                               
                     


  

Upvotes: 2

Related Questions