2

So I am new to Jenkins/Git. My question may be basic but please take time to answer.

So I am trying a simple thing. I have a simple Python Script that I have pushed on git. This is it:


def about_me(your_name):
    print("The wise {} loves Python.".format(your_name))            

def HW():
    print("Hello World!")

def Both():
    HW()
    about_me("Ab")

Both()
F  = open(r"C:\Users\AMRABET\Documents\VSC\HW\a.txt", "a")
F.write("ok\n")

No big deal. Just a console/file print. I pushed it on master branch on my git.

Next, I tried to execute it via Jenkins. After reading multiple topics on internet, I understood that actually Jenkins does not run the code. It only build it.

So I did build it. I configured a connection between Jenkins and Git and I succeeeded. This is the output of the build:


Running as SYSTEM
Building in workspace C:\Program Files (x86)\Jenkins\workspace\GitJob
using credential 1f413199-4637-40b4-96b9-a06e1d5aab4c
 > C:\Program Files\Git\bin\git.exe rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > C:\Program Files\Git\bin\git.exe config remote.origin.url https://github.com/MbtAbd/HelloWorld # timeout=10
Fetching upstream changes from https://github.com/MbtAbd/HelloWorld
 > C:\Program Files\Git\bin\git.exe --version # timeout=10
using GIT_ASKPASS to set credentials 
 > C:\Program Files\Git\bin\git.exe fetch --tags --force --progress https://github.com/MbtAbd/HelloWorld +refs/heads/*:refs/remotes/origin/*
 > C:\Program Files\Git\bin\git.exe rev-parse "refs/remotes/origin/master^{commit}" # timeout=10
 > C:\Program Files\Git\bin\git.exe rev-parse "refs/remotes/origin/origin/master^{commit}" # timeout=10
Checking out Revision 34500678072eb8536821606367d6ecf329d344d9 (refs/remotes/origin/master)
 > C:\Program Files\Git\bin\git.exe config core.sparsecheckout # timeout=10
 > C:\Program Files\Git\bin\git.exe checkout -f 34500678072eb8536821606367d6ecf329d344d9
Commit message: "Added the file writing stuff"
 > C:\Program Files\Git\bin\git.exe rev-list --no-walk 34500678072eb8536821606367d6ecf329d344d9 # timeout=10
Finished: SUCCESS

Can anyone please tell me how to run the result of the build via Jenkins? All other threads on StackOverflow suggest to execute a shell action ( sh 'python script.py' ) but in my case I don't really have the PY file.

Any help is appreciated. Thank you community!

6
  • Why you don't have the .py file? It should be check out from the git repository. Commented May 24, 2019 at 11:16
  • Well, first of all, your python script does not really have to be "built" - and it doesn't seem like the build script is doing much more than checking out the commit. Moreover, what do you mean you "don't really have the py file" - that's exactly what you put in the git repository, isn't it? Also, why do you want to run this script with Jenkins to begin with? Commented May 24, 2019 at 11:17
  • Yes. I meant I don't want to compile the py script that is on my machine. I want it to execute the script that he finds on git. Thats what i meant Commented May 24, 2019 at 11:18
  • doing sh 'python script.py' on Jenkins execute the script.py file that is on the machine, no ? Commented May 24, 2019 at 11:20
  • Jenkins seems like the wrong tool for this, but more to the point, doing sh 'python script.py' executes the script on the build machine, ie. the one that just checked out your git commit. It should indeed have the script file available to it. Commented May 24, 2019 at 11:27

2 Answers 2

1

From what I understood, you have a python script which you want to run on Jenkins.

When they say PY file, they're referring to .py file which is nothing but your python script.

What you need to do is follow these steps by googling each one of them:

  1. Create a Jenkins job and configure your git repo in it.
  2. In Build-steps, select Execute shell script option.
  3. Give command as python3 your_script_name.py or use python2 your_script_name.py depends on your python version.
  4. Save the job and click on Build.
  5. Check the console output of the job that is running.

Let me know if that's what you were expecting to happen.

Sign up to request clarification or add additional context in comments.

2 Comments

What should I put in Script field when I choose Execute Python script please? I want it to execute it whtever he gets from git
A shell script on a windows machine?
0

From what I understand of the initial OP, is that he wants to run the .py script after the git checkout.

So it's pretty easy, you do the git checkout in the Jenkins project.

After that you create a BUILD step, I am using windows so I use, EXECUTE WINDOWS BATCH COMMAND, and run it like this:

echo %WORKSPACE% # should print the directory jenkins downloaded your files from Git.

python -u "%WORKSPACE%\pythonScript.py" #calls the script located in the directory if is in the root folder

Comments

Your Answer

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