DEV Community

Michael Heap
Michael Heap

Posted on • Originally published at michaelheap.com on

Add and Commit action

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 git-auto-commit-action:

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 commit_option

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:

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.

The action also handles setting defaults in the code rather than using the default field in action.yml like git-auto-commit-action does.

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 code
on: push

jobs:
  run:
    name: Lint with ESLint
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v1
        with:
          node-version: 12.x

      - name: Install dependencies
        run: npm install

      - name: Update source code
        run: eslint "src/**" --fix

      - name: Commit changes
        uses: EndBug/add-and-commit@v7
        with:
          author_name: Your Name
          author_email: mail@example.com
          message: "Your commit message"
          add: "*.js"
Enter fullscreen mode Exit fullscreen mode

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 Compile
on: push

jobs:
  run:
    name: Compile
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v1
        with:
          node-version: 12.x

      - name: Install dependencies
        run: npm install

      - name: Compile
        run: npx ncc -o lib

      - name: Commit changes
        uses: EndBug/add-and-commit@v7
        with:
          author_name: Your Name
          author_email: mail@example.com
          message: "Your commit message"
          add: "lib"
Enter fullscreen mode Exit fullscreen mode

Useful links

Top comments (0)