Using git commit in GitHub Actions
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.
With the new release of a beta for the V2 of the actions/checkout action, it now will checkout the branch and setup the upstream alongside it. Previously it checked out to the exact commit and would cause a detached head state, but that is no longer the case.
Now, with such, pushing back to a branch is as simple as
git commit origin <branch>
The following is a workflow which on push will do the following:
- checkout the repo
dateand save it to
- setup git config
- commit the changed file and push it back to master
name: Commit date to masteron: pushjobs:date:runs-on: ubuntu-lateststeps:# Checkout the branch- name: checkoutuses: actions/checkout@v2-beta # use either @v2-beta or @master. Eventually there will be a @v2 tag.- name: save current daterun: |# do some operation that changes a file in the git repodate > time.txt- name: setup git configrun: |# setup the username and email. I tend to use 'GitHub Actions Bot' with no email by defaultgit config user.name "GitHub Actions Bot"git config user.email "<>"- name: commitrun: |# Stage the file, commit and pushgit add time.txtgit 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
On top of this, some additions or extractions for this workflow could be made:
- Instead of committing to
master, could commit to the specific branch that triggered the
on: pushin the workflow.
- You could conditionally check if any files were changed and skip the commit if there were no changes.
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.