首页 > 解决方案 > 通过 Jenkins 执行我的 Git 上的 python 脚本

问题描述

所以我是 Jenkins/Git 的新手。我的问题可能很基本,但请花时间回答。

所以我正在尝试一个简单的事情。我有一个在 git 上推送的简单 Python 脚本。就是这个:


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")

没什么大不了。只是一个控制台/文件打印。我把它推到了我的 git上的master分支上。

接下来,我尝试通过 Jenkins 执行它。在互联网上阅读了多个主题后,我了解到实际上 Jenkins 并没有运行代码。它只会构建它。

所以我确实建造了它。我在 Jenkins 和 Git 之间配置了一个连接,我成功了。这是构建的输出:


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

谁能告诉我如何通过 Jenkins运行构建结果?StackOverflow 上的所有其他线程都建议执行 shell 操作 ( sh 'python script.py'),但在我的情况下,我并没有真正的 PY 文件。

任何帮助表示赞赏。谢谢社区!

标签: pythongitjenkins

解决方案


据我了解,您有一个要在 Jenkins 上运行的 python 脚本。

当他们说PY文件时,他们指的是.py文件,它只不过是你的 python 脚本。

您需要做的是通过谷歌搜索每个步骤来执行以下步骤:

  1. 创建一个 Jenkins 作业并在其中配置您的 git 存储库。
  2. 在构建步骤中,选择Execute shell script选项。
  3. 将命令作为python3 your_script_name.py或使用python2 your_script_name.py取决于您的 python 版本。
  4. 保存作业并单击构建。
  5. 检查正在运行的作业的控制台输出。

让我知道这是否是您所期望的。


推荐阅读