[英]Git add and commit with Python script running on GitHub Actions
I want to add and commit a file with Python script to a repo running on the GitHub Actions server.我想将带有 Python 脚本的文件添加并提交到在 GitHub Actions 服务器上运行的存储库中。 I am committing a file by running the following in my
main.py
file:我通过在
main.py
文件中运行以下命令来提交文件:
import os
FILE = './file.txt'
os.system(f'git add {FILE}')
os.system("git commit -m 'update file'")
On GitHub actions, I am running Python script as:在 GitHub 动作上,我运行 Python 脚本为:
- name: Commit file
run: python commit_file.py
But there isn't any scripts commits on the repo master branch after GitHub actions run.但是在 GitHub 操作运行后,repo master 分支上没有任何脚本提交。
On my local machine, os.system("git commit -m 'update file'")
command is running normally, and I can verify that the commit exist with the git log
command.在我的本地机器上,
os.system("git commit -m 'update file'")
命令运行正常,我可以使用git log
命令验证提交是否存在。 On the GitHub actions, the following error occurs:在 GitHub 动作上,出现以下错误:
Run Commit file
python commit_file.py
Author identity unknown
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: empty ident name (for <runner@fv-az112-319.bzi55m0t1ufu323ye4cn5ktith.xx.internal.cloudapp.net>) not allowed
EDIT: I've added two more lines which will configure git credentials:编辑:我添加了另外两行将配置 git 凭据:
os.system('git config --global user.email "you@example.com"')
os.system('git config --global user.name "GitHub Actions"')
and now, the warnings are gone, but there is still no commit on the repo.现在,警告消失了,但回购协议上仍然没有提交。
It's as the error states, you need to configure the user email and name using git config
when you want to commit a new file to a repo using github action.这是错误状态,当您想使用 github 操作将新文件提交到存储库时,需要使用
git config
配置用户 email 和名称。
Note: Those values can be fake, hardcoded or depend on a variable.注意:这些值可能是假的、硬编码的或依赖于变量。
You just need to add those 2 command lines before the git add and the git commit commands in your python script.您只需在 python 脚本中的 git add 和 git 提交命令之前添加这两个命令行。
Moreover, you will need to perform a git push
at the end of the script for the file to appear on the repository branch at the end of the workflow run.此外,您需要在脚本末尾执行
git push
,以使文件在工作流运行结束时出现在存储库分支上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.