name: Git Auto Commit
on: push
jobs:
format-code:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- uses: stefanzweifel/git-auto-commit-action@v5
git-auto-commit-action
Automatically commit and push changed files back to GitHub with this GitHub Action for the 80% use case.
git-auto-commit
is pretty useful for automatically managing the changes in our repositories. This action is intended to cover the most common case: files are modified during the execution of the workflow, and those changes should be committed back to the repository.
It detects file changes, commits them, and pushes back to the repo under the "GitHub Actions" account by default, co-authored by whoever made the last commit.
So, here's a basic rundown of how it's used:
- First, the
GITHUB_TOKEN
permission is set up to allow for commits.
You would then put the Action into your workflow after any steps that modify files. Pretty self-explanatory.
You could even humanly modify the commit messages, pick specifically which files to commit, and do much more advanced stuff like tagging and using different git options for adding or pushing changes. How much simple could it be? For instance, consider a workflow that executes a PHP code formatter and commits the changes. To integrate git-auto-commit
, you will just need to add these lines to your workflow file.
What's super helpful with this Action is that it automates the management of changes without requiring any human intervention. The implication of this is that, for teams that would want the development workflows automated, especially for minor updates and documentation repeatedly, time after time, this could be the game-changer.
It keeps the main branch updated without human intervention, while it keeps the full history of the up-to-date changes living as new. It does come with some of the limitations; for example, this does not fetch the latest changes from the remote and does not handle the merge conflict. Further, the commits made by this action do not trigger new workflow runs, which can adversely affect workflows that rely on such triggers.
This is made very easy with the help of GitHub Actions—easy creation and use of multiline commit messages.
How to Create and Use Multiline Commit Messages in GitHub Actions?
In some cases, your commitment messages might be so detailed that it will span more than a single line. GitHub Actions can indeed work well with multiline commit messages but should be configured properly so that they would fit in perfectly into your workflow.
# Building a multiline commit message
# Adjust to your liking
- run: echo "Commit Message 1" >> commitmessage.txt
- run: echo "Commit Message 2" >> commitmessage.txt
- run: echo "Commit Message 3" >> commitmessage.txt
# Create a multiline string to be used by the git-auto-commit Action
- name: Set commit message
id: commit_message_step
run: |
echo 'commit_message<<EOF' >> $GITHUB_OUTPUT
cat commitmessage.txt >> $GITHUB_OUTPUT
echo 'EOF' >> $GITHUB_OUTPUT
# Quick and dirty step to get rid of the temporary file holding the commit message
- run: rm -rf commitmessage.txt
- uses: stefanzweifel/git-auto-commit-action@v5
id: commit
with:
commit_message: ${{ steps.commit_message_step.outputs.commit_message }}
Firstly, it creates a temporary text file (commitmessage.txt) where each line added indicates a line of the final commit message. - The Set commit message
step sets the commit message to a multiline string by taking all lines from commitmessage.txt
and putting them in such a way that multiple environment variables can be parsed by GitHub Actions.
This formatted message will then be the commit message for the git-auto-commit-action
, so the whole text will appear formatted in the commit log. - Clean up the temporary text file at the end to not make a mess of the repository. This allows one to have an organized way for formatted, detailed descriptions in your commits: this may be quite useful in documenting changes introduced per commit, especially when dealing with complex projects.
This warrants clarity and a chance for good access to information for any person having a look at the history of commits.
How to Use GitHub Actions in Forks from Private Repositories?
By default, workflows do not run on repositories forked from your private one because of security and privacy reasons. This shows that the main reason for not running workflows in the forked repository is because of insecurity on the contributor's.
To enable actions for private repositories; Go to your repository setting on GitHub.
Navigate to the "Actions" tab.". - "Run workflows from pull requests" will allow Actions to run on pull requests from forks.
Take a look at to GitHub Post about Enabling GitHub Actions for Forked Repositories.
How to Sign Commits and Configure Git Command Line Options in GitHub Actions?
For example, one may want to sign commits with command-line options to secure changes in your repository. However, this is going to have some setup requirements that must be fulfilled on forking or private repositories to work.
Check out the Import GPG Signature action if you want to automate these tasks regularly.