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.
What does it do?
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
How does it work?
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:
- Add any files that match the pattern provided in
- Remove any files if
- If there are no staged files, the action stops execution
- Otherwise it populates
- As GitHub Actions does a shallow clone, the action then runs
git fetchto get all branches
- This isn’t documented in the README, but the action then switches to a branch with the name specified in the
branchinput. This defaults to the branch that triggered the workflow run
- Then it ensures that the branch is up to date. You can choose
--rebasedepending on which strategy you prefer
- As the working branch may have changed, it then re-adds any changed files
- Before creating a commit using the
author_nameprovided and the
--signoffflag if the
signoffinput was set
- If the
taginput was provided, the action also creates a tag named after the
- If the
pushinput was set to true, it runs
git push origin <branch> --set-upstream
- If the
pushinput provided is a string it runs
git push <input string>
- Finally, if a tag was created it runs
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.
Common use cases
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"