user11368879
user11368879

Reputation:

Execute a python script that is on my Git via Jenkins

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!

Upvotes: 2

Views: 15834

Answers (2)

Daniel Montes
Daniel Montes

Reputation: 1

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

Upvotes: 0

Underoos
Underoos

Reputation: 5190

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.

Upvotes: 1

Related Questions