Benjamin Lannon

Select a theme. Click on the overlay or the button again to exit

Using git commit in GitHub Actions

Posted on:

GitHub Actions provides full access to the runner at your disposal, and one thing you may want to do is make commits in a workflow run and push it back up to GitHub automatically. I'm going to show a simple example where we run the date unix command, save the contents to a file, and push it back to the master branch.

Example workflow

The following is a workflow which on push will do the following:

name: Commit date to master
on: push
jobs:
date:
runs-on: ubuntu-latest
steps:
# Checkout the branch
- name: checkout
uses: actions/checkout@v2

- name: save current date
run: |
# do some operation that changes a file in the git repo
date > time.txt


- name: setup git config
run: |
# setup the username and email. I tend to use 'GitHub Actions Bot' with no email by default
git config user.name "GitHub Actions Bot"
git config user.email "<>"


- name: commit
run: |
# Stage the file, commit and push
git add time.txt
git commit -m "new date commit"
git push origin master

There's no magic to what is being done. No complex git commands at play. Just plain old add, commit, and push.

On top of this, some additions or extractions for this workflow could be made:

With this, you can have your scripts make commits on your behalf. As the full git client is available, you can get extremely deep into doing things like reverts, rebases, etc, but for the usual tasks of doing commits, GitHub Actions provides the functionality to do as you would on your local machine.