ad-m / github-push-action

GitHub actions to push back to repository eg. updated code
MIT License
1.21k stars 230 forks source link

Pushing changes fail #79

Open Kraymer opened 3 years ago

Kraymer commented 3 years ago

My action :

name: foobar

on: [pull_request]

jobs:
  hello_world_job:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Get changed files
        id: files
        uses: jitterbit/get-changed-files@v1
        with:
          format: 'space-delimited'

      - name: Action that add a new file in repo
            ...

      - name: Commit files
        run: |
          git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git add .
          git commit -m "Add changes" -a

      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.head_ref }}

Error log :

Run ad-m/github-push-action@master
  with:
    github_token: ***
    branch: gh-action-generate
    directory: .
Push to branch gh-action-generate
To https://github.com/Kraymer/Simple-Repertoire.git
 ! [rejected]        HEAD -> gh-action-generate (fetch first)
error: failed to push some refs to 'https://github.com/Kraymer/Simple-Repertoire.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Error: Invalid exit code: 1
    at ChildProcess.<anonymous> (/home/runner/work/_actions/ad-m/github-push-action/master/start.js:29:21)
    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 exit code: 1
    at ChildProcess.<anonymous> (/home/runner/work/_actions/ad-m/github-push-action/master/start.js:29:21)
    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)

Note: I did not commit in the remote branch after the commit that triggered the action

navaneeth-spotnana commented 3 years ago

Clue:

hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.

I had the same issue, but my case was a different clue, and so a different fix. 😝

navaneeth-spotnana commented 3 years ago

Yo. Now I got your same error. Add force: true to fix this:

      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.head_ref }}
          force: true
navaneeth-spotnana commented 3 years ago

Not sure if force pushing is going to be safe, I'm wondering if doing a git pull before you push would do the job.

fs-c commented 3 years ago

Quick note in response to the force push suggestion: Don't force push unless you know what you are doing. Many, many people have shot themselves in the foot with git push -f.

Also, as another data point, I get the same error as the OP on ad-m/github-push-action@master but ad-m/github-push-action@v0.5.0 works fine.

And on an unrelated note, it's a really bad practice to recommend people to use ad-m/github-push-action@master as is being done in the README. It might just introduce unexpected errors in people's workflows even though seemingly nothing changed...

jg75 commented 1 year ago

It looks like you would get this error if you have no new commits to push. For example, let's say you have a workflow that triggers on a pull_request that does:

If there are no changes in the format code step, you'll get that error in the push step. So, question then. Should you commit with --allow-empty and just always create another commit or is it possible to fail gracefully if there is nothing to push?