Home > Enterprise >  Git add and commit with Python script running on GitHub Actions
Git add and commit with Python script running on GitHub Actions

Time:12-24

I want to add and commit a file with Python script to a repo running on the GitHub Actions server. I am committing a file by running the following in my main.py file:

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:

- name: Commit file
    run: python commit_file.py

But there isn't any scripts commits on the repo master branch after GitHub actions run.

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. On the GitHub actions, the following error occurs:

Run Commit file
python commit_file.py
Author identity unknown

*** Please tell me who you are.

Run

  git config --global user.email "[email protected]"
  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:

os.system('git config --global user.email "[email protected]"')
os.system('git config --global user.name "GitHub Actions"')

and now, the warnings are gone, but there is still no commit on the repo.

CodePudding user response:

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.

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.

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.

  • Related