Bootstrap FreeKB - GitHub Actions - Add and Commit file to repo
GitHub Actions - Add and Commit file to repo

Updated:   |  GitHub Actions articles

GitHub Actions can be used to do something whenever something happens in one of your GitHub repositories. If you are not familiar with GitHub Actions, check out my article Getting Started with GitHub Actions.

Let's say you create a file in your runner VM and you want to add and commit the file to your repo. Sometimes it make sense just to use run and to run the commands to add and commit the file.

name: my workflow
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  push:
    branches:
      - main
jobs:
  github-action-job:
    runs-on: ubuntu-latest
    steps:      
      - name: check out this repo
        uses: actions/checkout@v5

      - name: create foo.txt
        run: touch foo.txt
        
      - name: git add and commit
        run: |
          git config user.name "john.doe"
          git config user.email "john.doe@example.com" 
          git add foo.txt
          git commit -m 'initial commit' foo.txt
          git push origin HEAD

 

However, sometimes this can return some errors. For example, I once started getting errors like this.

Fatal: could not read Username for 'https://github.com': No such device or address

 

I was able to determine this had something to do with actions/checkout cloning the repository using the HTTPS url instead of the SSH git@github.com:my-organization/my-repo.git URL which I saw in the .git/config file of the cloned repo in the runner VM.

~]$ cat .git/config
[remote "origin"]
        url = https://github.com/my-organization/my-repo.git

 

In this scenario, using stefanzweifel/git-auto-commit-action was able to add and commit files to the repo even if the repo was cloned using the HTTPS URL.

name: my workflow
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  push:
    branches:
      - main
jobs:
  github-action-job:
    runs-on: ubuntu-latest
    steps:      
      - name: check out this repo
        uses: actions/checkout@v5

      - name: create foo.txt
        run: touch foo.txt
        
      - name: git add and commit
      	uses: stefanzweifel/git-auto-commit-action@v7
        with:
       		commit_message: "initial commit"
        	commit_user_name: "john.doe"
        	commit_user_email: "john.doe@example.com"

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter c7b858 in the box below so that we can be sure you are a human.