ad-m / github-push-action

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

Push fails with remote rejected deny updating a hidden ref #143

Closed lukasdenk closed 2 years ago

lukasdenk commented 2 years ago

When I try the first example in the README, I always get this error message: ! [remote rejected] HEAD -> refs/pull/15/merge (deny updating a hidden ref)

ZPascal commented 2 years ago

Hi @lukasdenk, could you please explain your setup and scenario?

lukasdenk commented 2 years ago

Thanks for the fast reply, @ZPascal. The workflow is triggered when I push a new commit to a branch with an open PR to development.

This is my ci.yml:

name: CI

on:
  push:
    branches:
      - main
      - development
  pull_request:
    branches:
      - main
      - development
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal access tokenn.
          fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository.
          ref: ${{ github.ref }}
      - name: Create local changes
        run: echo he > t.txt
      - name: Commit files
        run: |
          git add . 
          git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git commit -m "Add changes" -a
      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}

This is the error message:

Run ad-m/github-push-action@master
  with:
    github_token: ***
    branch: refs/pull/15/merge
    github_url: https://github.com
    directory: .
Push to branch refs/pull/15/merge
Pushing to https://github.com/FEUP-MEIC-DS-[2](https://github.com/FEUP-MEIC-DS-2022-1MEIC05/dynamic-prices-backend/actions/runs/3347182291/jobs/5544886512#step:5:2)022-1MEIC05/dynamic-prices-backend.git
POST git-receive-pack (1066 bytes)
To https://github.com/FEUP-MEIC-DS-2022-1MEIC05/dynamic-prices-backend.git
 ! [remote rejected] HEAD -> refs/pull/15/merge (deny updating a hidden ref)
error: failed to push some refs to 'https://github.com/FEUP-MEIC-DS-2022-1MEIC05/dynamic-prices-backend.git'
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 (node:events:[3](https://github.com/FEUP-MEIC-DS-2022-1MEIC05/dynamic-prices-backend/actions/runs/3347182291/jobs/5544886512#step:5:3)90:28)
    at maybeClose (node:internal/child_process:106[4](https://github.com/FEUP-MEIC-DS-2022-1MEIC05/dynamic-prices-backend/actions/runs/3347182291/jobs/5544886512#step:5:4):16)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:301:[5](https://github.com/FEUP-MEIC-DS-2022-1MEIC05/dynamic-prices-backend/actions/runs/3347182291/jobs/5544886512#step:5: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 (node:events:390:28)
    at maybeClose (node:internal/child_process:10[6](https://github.com/FEUP-MEIC-DS-2022-1MEIC05/dynamic-prices-backend/actions/runs/3347182291/jobs/5544886512#step:5:6)4:[16](https://github.com/FEUP-MEIC-DS-2022-1MEIC05/dynamic-prices-backend/actions/runs/3347182291/jobs/5544886512#step:5:17))
    at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5)

Thank you!

lukasdenk commented 2 years ago

Ok, I have solved it.

Since my workflow was triggered by a PR, github.ref contained the reference to the PR, not the source branch. So pushing a commit would set the PR reference to this commit. Hence, the PR reference and the source branch of the PR would refer to different commits.

The solution was to replace github.ref by github.head_ref.

ZPascal commented 2 years ago

Hi @lukasdenk, good to hear that everything works now as expected. I'll follow the topic up and document it more clear inside the documentation.

lukasdenk commented 2 years ago

Cool, thanks :)