0

I would like to run a python file from jenkinsfile.build pipeline file my jenkinsfile.build file has many stages and from one stage i would like to run a python script the format of my jenkinsfile.build is

stages{
        stage('Build stage') {
            steps {
                echo "${username}"
                dir("py_scripts"){
                    python -u say_hello.py --name '${username}'
                }
            }
        }
        stage('Deploy stage') {
         --------some code-------
        }
    }
}

and i have my python code which accepts an argument --name. That is running completely fine. when i Run the jenkins job, i am seeing i get the error in console output of jenkins which denotes the line where i am calling the python script. the error is

18:48:52  WorkflowScript: 26: expecting '}', found 'say_hello' @ line 26, column 31.
18:48:52                         python -u say_hello.py --name '${username}'
18:48:52                                   ^
18:48:52  
18:48:52  1 error

i need to change the command i am using to call the python script in jenkinsfile. please help me in this. I am new to this Jenkinsfile.

1 Answer 1

0

You have to run the python script as a shell script inside a sh step:

sh "python -u say_hello.py --name '${username}'"
Sign up to request clarification or add additional context in comments.

3 Comments

Or as a bat step if running on a Windows agent
Please help me on this. I have executed your code. it gives me usage: say_hello.py [-h] --name NAME 12:14:38 say_hello.py: error: the following arguments are required: --name
First of all, the Python script obviously gets executed now and the error is coming from the script execution itself. Is the variable username not an empty string? What do you see in the console output when the echo "${username}" statement is executed? You may also want to try to replace the ${username} in the Python call with a literal value in order to rule out errors in the Python script itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.