stefanzweifel / git-auto-commit-action

Automatically commit and push changed files back to GitHub with this GitHub Action for the 80% use case.
MIT License
1.98k stars 227 forks source link

Using multiple commits #124

Closed marcofranssen closed 3 years ago

marcofranssen commented 3 years ago

I'm trying to commit multiple commits in my workflow to split the larger amount of changes in smaller commits.

Currently my build is having the following error.

INPUT_REPOSITORY value: .
INPUT_BRANCH value: 
Your branch is up to date with 'origin/master'.
INPUT_FILE_PATTERN: modules/moduleA
INPUT_COMMIT_OPTIONS: --no-verify --signoff
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
    modules/moduleB

nothing added to commit but untracked files present
Error: Invalid status code: 1
    at ChildProcess.<anonymous> (/home/ec2-user/actions-runner/_work/_actions/stefanzweifel/git-auto-commit-action/v4.4.0/index.js:17:19)
    at ChildProcess.emit (events.js:210:5)
    at maybeClose (internal/child_process.js:1021:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5) {
  code: 1
}
Error: Invalid status code: 1
    at ChildProcess.<anonymous> (/home/ec2-user/actions-runner/_work/_actions/stefanzweifel/git-auto-commit-action/v4.4.0/index.js:17:19)
    at ChildProcess.emit (events.js:210:5)
    at maybeClose (internal/child_process.js:1021:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5)

I'm using the following workflow.

jobs:
  name: Generate code
  steps:      
      - run: .github/generate.sh

      - uses: stefanzweifel/git-auto-commit-action@v4.4.0
        if: github.ref == 'refs/heads/master'
        with:
          commit_message: Main changes
          commit_options: '--no-verify --signoff'
          skip_dirty_check: true
          file_pattern: README.md main/
          commit_user_name: My Bot
          commit_user_email: mybot@users.noreply.github.com

      - uses: stefanzweifel/git-auto-commit-action@v4.4.0
        if: github.ref == 'refs/heads/master'
        with:
          commit_message: Update moduleA
          commit_options: '--no-verify --signoff'
          skip_dirty_check: true
          file_pattern: modules/moduleA
          commit_user_name: My Bot
          commit_user_email: mybot@users.noreply.github.com

      - uses: stefanzweifel/git-auto-commit-action@v4.4.0
        if: github.ref == 'refs/heads/master'
        with:
          commit_message: Update moduleB
          commit_options: '--no-verify --signoff'
          skip_dirty_check: true
          file_pattern: modules/moduleB
          commit_user_name: My Bot
          commit_user_email: mybot@users.noreply.github.com

As not every module does always have changes, sometimes there is no need to make that commit. Currently the workflow fails because moduleB has changes and moduleA doesn't while trying to commit moduleA.

Am I doing something wrong? Or could this be a feature which would only try to commit if the pattern staged files.

stefanzweifel commented 3 years ago

This should definitely be possible with the current state of the action. 🤔

From you're example, I could find the following issues:

  1. You're setting skip_dirty_check: true in all your steps. This will bypass the git status check and will try to create a commit and push to the remote repository.

  2. You're using version v4.4.0 which isn't the latest version. You don't have to use the latest version, but in v4.4.1 the git status check has been updated to use the given file pattern.

So updating your workflow to use v4.4.1 – or v4 for the latest version – and removing the skip_dirty_check: true line in your steps should do the trick. The Action will then only attempt to create a commit and push to the remote repository, if files for the given file pattern actually changed.

 jobs:
   name: Generate code
   steps:      
       - run: .github/generate.sh

-       - uses: stefanzweifel/git-auto-commit-action@v4.4.0
+       - uses: stefanzweifel/git-auto-commit-action@v4.4.1
         if: github.ref == 'refs/heads/master'
         with:
           commit_message: Main changes
           commit_options: '--no-verify --signoff'
-          skip_dirty_check: true
           file_pattern: README.md main/
           commit_user_name: My Bot
           commit_user_email: mybot@users.noreply.github.com

-       - uses: stefanzweifel/git-auto-commit-action@v4.4.0
+       - uses: stefanzweifel/git-auto-commit-action@v4.4.1
         if: github.ref == 'refs/heads/master'
         with:
           commit_message: Update moduleA
           commit_options: '--no-verify --signoff'
-          skip_dirty_check: true
           file_pattern: modules/moduleA
           commit_user_name: My Bot
           commit_user_email: mybot@users.noreply.github.com

-       - uses: stefanzweifel/git-auto-commit-action@v4.4.0
+       - uses: stefanzweifel/git-auto-commit-action@v4.4.1
         if: github.ref == 'refs/heads/master'
         with:
           commit_message: Update moduleB
           commit_options: '--no-verify --signoff'
-          skip_dirty_check: true
           file_pattern: modules/moduleB
           commit_user_name: My Bot
           commit_user_email: mybot@users.noreply.github.com

However, keep in mind that each step will create a commit and push it to the remote. Between each step you would have to add a git pull step which updates the local state of the repository in your workflow.

marcofranssen commented 3 years ago

Thanks that did the trick. I have also configured dependabot on my repo for github actions now.