ad-m / github-push-action

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

Branch tip is behind remote #50

Open kevinkace opened 4 years ago

kevinkace commented 4 years ago

I have an action to build some frontend files (webpack/gulp/less), and commit them back to the repo.

The push is failing:

Run ad-m/github-push-action@master
  with:
    github_token: ***
    branch: origin HEAD
    directory: .
  env:
    CI: true
Started: bash /home/runner/work/_actions/ad-m/github-push-action/master/start.sh
Push to branch origin HEAD
To https://github.com/animeplanet/ap-main.git
   1e4fca86d..0ad8fbe14  HEAD -> dm-action
 ! [rejected]            HEAD -> origin (non-fast-forward)
error: failed to push some refs to '***github.com/animeplanet/ap-main.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and 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 status code: 1
    at ChildProcess.<anonymous> (/home/runner/work/_actions/ad-m/github-push-action/master/start.js:9: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/runner/work/_actions/ad-m/github-push-action/master/start.js:9:19)
    at ChildProcess.emit (events.js:210:5)

There is a git status command just prior to trying the push which shows the repo is up-to-date:

Run git status
  git status
  shell: /bin/bash -e {0}
On branch dm-action
Your branch is ahead of 'origin/dm-action' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

My Build yaml:

name: BuildCssJs.js CI

on:
  push:
    branches:
      - 'dm-action'
      - 'staging'
      - 'dark-mode-fixes'
      - 'manga-chapters'
      - 'carousel-fix'
      - 'characters-edit-actions'
      - 'responsive-carousels'
    paths:
      - '.github/workflows/*'
      - 'gulpfile.js'
      - 'webpack.*'
      - 'src/assets/**/*'
      - 'public_html/inc/**/*.js'
      - 'public_html/inc/**/*.less'
      - '!public_html/inc/dist/**/*'
      - '!public_html/inc/*'

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [12.x]

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
    - run: npm ci
    - run: npm run ci --if-present
    - run: git status
    - name: Commit files
      run: |
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        git add .
        git commit -m "Built CSS/JS - $(git rev-parse --short HEAD)"
    - run: git pull
    - run: git status
    - name: Push changes
      uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        branch: 'origin HEAD'
      env:
        CI: true
DarioRomano commented 4 years ago

I'm having the same issue, and would be interested in a fix as well!

vorcigernix commented 4 years ago

Same issue here

ad-m commented 4 years ago

Your branch is ahead of 'origin/dm-action' by 1 commit.

I believe It seems to me that the information presented is not current. You would have to do "git fetch" before "git status" to get current information.

vorcigernix commented 4 years ago

Git pull solved it to me.

William-Lake commented 4 years ago

I had the same issue today and in my case it was because I was missing the branch portion (E.g. branch: ${{ github.ref }}).

After adding it to my workflow file the issue was resolved:

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

I'm curious about whether changing branch: 'origin HEAD' to branch: ${{ github.ref }} will do the trick in your case. I imagine it should, since it looks like you're trying to push to the same branch that triggered the action and github.ref will resolve to the current branch reference.

mfouesneau commented 3 years ago

Same issue. Is this solved?