Add and Commit is the second (of three!) actions that can be used to push your changes to a repo during a GitHub Actions workflow.
Much like the last spotlight post, today’s action is focused on adding and committing files to a repo. The README does a good job explaining how it’s different to
This is heavily inspired by git-auto-commit-action (by Stefan Zweifel): that action automatically detects changed files and commits them. While this is useful for most situations, this doesn't commit untracked files and can sometimes commit unintended changes (such as package-lock.json or similar, that may have happened during previous steps).
It’s also evolved from
git-auto-commit and supports pulling changes if required before a push, removing files with
git rm and makes features such as
signoff an input rather than making you specify
--signoff as a
add-and-commit is implemented in TypeScript rather than
bash and is a single 388 line file named main.ts. It uses simple-git to interact with the underlying
git repo and provides a compiled version of the action that includes all dependencies.
main.ts itself is reasonable easy to follow:
git fetchto get all branches
branchinput. This defaults to the branch that triggered the workflow run
--rebasedepending on which strategy you prefer
author_nameprovided and the
--signoffflag if the
signoffinput was set
taginput was provided, the action also creates a tag named after the
pushinput was set to true, it runs
git push origin <branch> --set-upstream
pushinput provided is a string it runs
git push <input string>
git push --tags. This part is interesting as it deletes a remote tag and re-pushes when an error is encountered
There are a couple of other interesting things in the action to dig into.
One of the big questions in the Actions world is how to parse structured input.
add-and-commit attempts to parse inputs that can be arrays as both JSON and YAML before falling back to treating it as a simple string.
Automatically committing changed files can be useful in a couple of situations. The example shown in the docs is to run
eslint --fix to fix code styling before pushing the changes back to the repo:
name: Lint source codeon: pushjobs:run:name: Lint with ESLintruns-on: ubuntu-lateststeps:- name: Checkout repo- name: Set up Node.jswith:node-version: 12.x- name: Install dependenciesrun: npm install- name: Update source coderun: eslint "src/**" --fix- name: Commit changesuses: EndBug/[email protected]with:author_name: Your Nameauthor_email: [email protected]message: "Your commit message"add: "*.js"
Another idea is to make sure that your
lib folder for your action (like the one used in
add-and-commit) is always compiled as expected on push:
name: Automatic Compileon: pushjobs:run:name: Compileruns-on: ubuntu-lateststeps:- name: Checkout repo- name: Set up Node.jswith:node-version: 12.x- name: Install dependenciesrun: npm install- name: Compilerun: npx ncc -o lib- name: Commit changesuses: EndBug/[email protected]with:author_name: Your Nameauthor_email: [email protected]message: "Your commit message"add: "lib"