add-and-commit

repository·main·Indexed 23 days ago

https://github.com/endbug/add-and-commit

A GitHub Action that allows users to automatically add, commit, and push changes made during a workflow run—such as linting fixes, documentation updates, or build artifacts—directly back to the repository. It supports configurable commit messages, custom author/committer identities, tagging, and specific path handling via the `add` and `remove` inputs.

Tokens
2.6K
Snippets
5
Records
14
Agent score
29%

What's inside add-and-commit

  1. How tokens affect commit permissions and CI triggers

    main

    The add-and-commit action uses the token configured in the local git repository (typically set during the actions/checkout step).

    Triggering CI on new commits

    By default, if you use the built-in GITHUB_TOKEN secret, GitHub will not trigger subsequent workflow runs from the commits generated by this action to prevent infinite loops.

    To ensure commits generated by this action do trigger CI, you must checkout the repository using a Personal Access Token (PAT) instead of the default GITHUB_TOKEN.

  2. How Git arguments are parsed

    main

    Arguments provided to inputs like add, commit, remove, pull, tag, and tag_push are not executed directly as CLI commands. Instead, they are parsed by the string-argv package and then passed to simple-git.

    Warning: Strings with complex nested quotes may be parsed incorrectly. If you encounter issues, verify your argument string using string-argv or enable debug logging in your GitHub Action run.

  3. Install and use Add & Commit GitHub Action

    main

    Use this GitHub Action to commit changes made in your workflow run directly to your repository (e.g., for linting, documentation updates, or build artifacts).

    To use it, add a step to your workflow file using the EndBug/add-and-commit@v10 action. You can specify various inputs to control how files are added, committed, and pushed.

    - uses: EndBug/add-and-commit@v10
      with:
        add: 'src'
        message: 'Your commit message'
  4. How to push commits and tags

    main

    Pushing commits

    The action defaults to running git push origin ${new_branch} --set-upstream. You can control this via the push input:

    • true (default): Behaves normally.
    • false: Prevents any git push command from running.
    • any other string: The action executes git push ${push_input}. You must include the remote and branch manually (e.g., origin my-branch --force).

    Tagging

    Use the tag input to provide arguments for git tag. Crucial: The tag name must be the first word in the string that is not preceded by a hyphen (e.g., -a tag-name -m "message" is valid). You can append extra arguments to the tag push command using the tag_push input.

  5. Configure checkout for Pull Requests

    main

    When using actions/checkout on a Pull Request, it defaults to a detached HEAD state. To make changes and commit them back to the source branch, you must explicitly checkout the head repository and ref.

    If the PR is from an internal repository (same repo), you can omit the repository input. If the PR is from a fork, ensure you have write access or use a Personal Access Token (PAT) as described in the About tokens section.

    - uses: actions/checkout@v4
      with:
        repository: ${{ github.event.pull_request.head.repo.full_name }}
        ref: ${{ github.event.pull_request.head.ref }}
  6. How to add and remove files

    main

    Adding files

    The add input accepts standard git add arguments. To add multiple specific paths or run multiple commands, you can provide a JSON or YAML array inside the string (e.g., '["file1", "file2"]').

    Removing files

    The remove input runs the git rm command. Like the add input, you can use JSON or YAML arrays to specify multiple files or commands. To auto-detect deleted files for committing, use the --no-ignore-removal or -A argument.

  7. Configure the commit author and committer

    main

    You can customize who appears as the author and committer of the changes.

    • Use default_author: github_actions to make the author appear as "GitHub Actions".
    • Use committer_name and committer_email to specify a custom committer identity.

    Note: The github_token input is used internally to access the GitHub API for user info and should not be modified.

    - uses: EndBug/add-and-commit@v10
      with:
        message: Show GitHub Actions logo
        committer_name: GitHub Actions
        committer_email: actions@github.com
  8. Run the action in a specific directory using `cwd`

    main

    If your repository is not located in the default $GITHUB_WORKSPACE (for example, if you used the path option in actions/checkout), use the cwd input to specify the target directory. The path should follow standard bash conventions.

    - uses: EndBug/add-and-commit@v10
      with:
        message: 'Add the very useful text file'
        add: '*.txt --force'
        cwd: './pathToRepo/'
  9. Configure Add & Commit GitHub Action inputs

    main

    The add-and-commit action accepts several inputs to control how files are staged, how commits are authored, and how changes are pushed.

    Core Inputs

    • add: Arguments for the git add command (e.g., . or specific file paths).
    • remove: Arguments for the git rm command.
    • message: The commit message. Defaults to Commit from GitHub Actions (${GITHUB_WORKFLOW}) if not provided.
    • push: Boolean or string arguments for the git push command.
    • pull: Controls whether to pull before committing. Note: NO-PULL is a legacy option; to skip pulling, simply omit this input.
    • github_token: Required for API interactions. A warning is issued if not detected.

    Author & Committer Configuration

    You can control the identity used for the commit using default_author and specific name/email inputs.

    default_author options:

    • github_actor: Uses the GITHUB_ACTOR as the name and ${GITHUB_ACTOR}@users.noreply.github.com as the email.
    • user_info: Attempts to fetch the user's real name and email via API. If unavailable, it falls back to github_actor values.
    • github_actions: Uses the standard github-actions bot identity.

    Identity Inputs:

    • author_name / author_email: The identity of the person who wrote the changes.
    • committer_name / committer_email: The identity of the person committing the changes. If not provided, they default to the author's identity.

    Git Behavior & Error Handling

    • fetch: Arguments for the git fetch command.
    • pathspec_error_handling: Determines how to handle errors in pathspecs. Valid values: ignore, exitImmediately, exitAtEnd.
    • cwd: The current working directory for the git commands.
    • new_branch: Optional branch name.
    • tag / tag_push: Options for tagging and pushing tags.
  10. Configure Add & Commit inputs

    main

    The action is highly configurable via the with block in your workflow. Below are the available inputs:

    InputDefaultDescription
    add.Arguments for the git add command. Supports JSON/YAML arrays for multiple commands (e.g., '["file1", "file2"]').
    author_namedepends on default_authorName of the commit author.
    author_emaildepends on default_authorEmail of the commit author.
    commit''Additional arguments for git commit (excluding --message).
    committer_namesame as authorName of the custom committer.
    committer_emailsame as authorEmail of the custom committer.
    cwd.Local path to the repository directory. Requires actions/checkout first.
    default_authorgithub_actorHow to fill missing author info: github_actor, user_info, or github_actions.
    fetch--tags --forceArguments for git fetch. Set to false to skip fetching.
    messageCommit from GitHub Actions...The commit message.
    new_branch''If set, the action pushes to this new branch.
    pathspec_error_handlingignoreHow to handle git add/rm errors: ignore, exitImmediately, or exitAtEnd.
    pull''Arguments for git pull.
    pushtrueWhether to push. Use false to skip, or a string like origin branch --force to customize the command.
    remove''Arguments for git rm. Supports JSON/YAML arrays.
    tag''Arguments for git tag. The tag name must be the first word not preceded by a hyphen.
    tag_push''Arguments appended to git push --tags.
  11. Use array inputs for `add` and `remove`

    main

    Due to GitHub Action API limitations, all inputs must be strings or booleans. To pass arrays to the add or remove inputs, you must encode them as a string using either a YAML flow sequence (with single quotes) or a YAML block sequence (using the pipe | character).

    # Using YAML flow sequence
    - uses: EndBug/add-and-commit@v10
      with:
        add: '["afile.txt", "anotherfile.txt"]'
    
    # Using YAML block sequence
    - uses: EndBug/add-and-commit@v10
      with:
        add: |
          - afile.txt
          - anotherfile.txt
  12. Optimize performance for large repositories

    main

    By default, the action performs a git fetch to ensure it can see existing refs. In repositories with a very large number of branches or tags, this can be slow.

    You can skip this step by setting the fetch input to false.

    Warning: Disabling fetch may impact the action's ability to create new branches or tags. Only disable it if necessary.