简体   繁体   English

Git 添加和提交 Python 脚本在 GitHub 上运行

[英]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.

相关问题 在 GitHub Actions CI 中运行 testcontainers-python - Running testcontainers-python in GitHub Actions CI Python 在 github 操作中运行时不会发生自动版本控制 - Python automatic versioning not happening while running in github actions 有没有办法使用 Github Actions 停止和重新启动自托管的 Python 脚本? - Is there a way to stop and restart a self hosted Python script using Github Actions? 如何将 Github 操作用户输入传递到 python 脚本 - How to pass Github Actions user input into a python script Github 执行创建文件的 Python 脚本,然后提交并推送该文件 - Github action to execute a Python script that create a file, then commit and push this file 如何在 GitHub Actions 上的 matrix.python-version 中添加 pyqt5? - How to add pyqt5 in matrix.python-version on GitHub Actions? 在冻结的 Python 脚本 (Cx_Freeze) 中显示 git commit hash - Display git commit hash in frozen Python script (Cx_Freeze) Python 本地存储库脚本和特定提交的 Cherry-Pick GIT - Python Script for Local Repository and Cherry-Pick for specific commit GIT python 脚本打印来自特定 git 分支的最后提交消息 - python script to print the last commit message from specific git branch python 脚本:git 运行前检查 - python script: git checkout prior to running
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM