python - 通过 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 文件。
任何帮助表示赞赏。谢谢社区!
解决方案
据我了解,您有一个要在 Jenkins 上运行的 python 脚本。
当他们说PY文件时,他们指的是.py
文件,它只不过是你的 python 脚本。
您需要做的是通过谷歌搜索每个步骤来执行以下步骤:
- 创建一个 Jenkins 作业并在其中配置您的 git 存储库。
- 在构建步骤中,选择
Execute shell script
选项。 - 将命令作为
python3 your_script_name.py
或使用python2 your_script_name.py
取决于您的 python 版本。 - 保存作业并单击构建。
- 检查正在运行的作业的控制台输出。
让我知道这是否是您所期望的。
推荐阅读
- excel - 如何将列总计添加到当前时间
- java - Java在索引处拆分字符串而不剪切单词
- python - 值得信赖的客户
- ios - Swift 4.2 中自定义类 UIButton 的错误。UIButton 有什么新功能?
- matlab - .mat 文件的“功能工作区”字段中包含什么?
- php - SQLState 42000 语法错误。可能的表问题?
- javascript - 使用 Jest 进行测试时,如何正确设置当地货币?
- c# - 从我的 C# windows 应用程序运行批处理文件时遇到问题
- javascript - 我如何修改此代码以让机器人读取完整的不和谐消息以响应短语和标记特定角色?
- vue.js - Nuxt.js + Socket.io