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
2.07k stars 231 forks source link

`create_branch` should be for remote only #295

Open mvorisek opened 1 year ago

mvorisek commented 1 year ago

git-auto-commit Version

4.16.0 / latest

Machine Type

Ubuntu (eg. ubuntu-latest)

Bug description

      uses: stefanzweifel/git-auto-commit-action@v4
      with:
        branch: ${{ github.head_ref || github.ref_name }}.changes
        # create_branch: true
        push_options: '--force'
        commit_message: Unit Changes
        commit_user_name: Bot
        commit_user_email: bot@example.com
        commit_author: Bot <bot@example.com>

cannot force push the remote branch even if it exists remotely

Expected behaviout

Notice, the create_branch option in the repro above is commented/disabled. When enabled, all works as expected.

create_branch should apply for remote branch creation only. In CI, an option for creating local branch has almost no sense. The important thing is if the branch is existing on the remote or not.

stefanzweifel commented 1 year ago

Thanks for your feedback/suggestion. Would you be able to share the exact error message you're receiving when running the action with create_branch: true and push_options: '--force'?

Thanks!

mvorisek commented 1 year ago

Thanks for your feedback/suggestion. Would you be able to share the exact error message you're receiving when running the action with create_branch: true and push_options: '--force'?

Thanks!

config:

create_branch: false
push_options: '--force'

output:

Started: bash /home/runner/work/_actions/stefanzweifel/git-auto-commit-action/v4/entrypoint.sh
INPUT_REPOSITORY value: .
INPUT_STATUS_OPTIONS: 
INPUT_FILE_PATTERN: .
INPUT_BRANCH value: fix_ns_resolving.changes
From https://github.com/mvorisek/sphinxcontrib-phpdomain
 * [new branch]      fix_ns_resolving.changes -> origin/fix_ns_resolving.changes
 * [new branch]      master     -> origin/master
error: Your local changes to the following files would be overwritten by checkout:
    test/unit/ns.html
Please commit your changes or stash them before you switch branches.
Aborting
Error: Invalid status code: 1
    at ChildProcess.<anonymous> (/home/runner/work/_actions/stefanzweifel/git-auto-commit-action/v4/index.js:17:19)
    at ChildProcess.emit (node:events:527:28)
    at maybeClose (node:internal/child_process:1092:16)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:302:5) {
  code: 1
}
Error: Invalid status code: 1
    at ChildProcess.<anonymous> (/home/runner/work/_actions/stefanzweifel/git-auto-commit-action/v4/index.js:17:19)
    at ChildProcess.emit (node:events:527:28)
    at maybeClose (node:internal/child_process:1092:16)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:302:5)
stefanzweifel commented 1 year ago

Thank you @mvorisek.

So the actual error you're getting is the following.

error: Your local changes to the following files would be overwritten by checkout: test/unit/ns.html Please commit your changes or stash them before you switch branches.

Which happens – as you correctly noticed – when switching to a new branch locally. I honestly can't remember why we actually switch to a different branch locally, instead of just pointing to the right branch when pushing. This is definitely something I would like to change in v6.

To fix your current issues, you would have to switch to the right branch before changing your files.

As you didn't provide the entire workflow file in your opening comment, I assume that this workflow is one causing you issues.

I can already see some general problems which git-auto-commit will not support (no matrices support), but I tried to provide a fix anyway:

name: CI

on:
  pull_request:
  push:
  schedule:
    - cron: '0 0/2 * * *'

jobs:
  build:
    if: endsWith(github.head_ref || github.ref_name, '.changes') == false
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python: ['3.7', '3.8', '3.9', '3.10', '3.11']
      fail-fast: false

    permissions:
      contents: write

    steps:
    - uses: actions/checkout@v3

+   - name: Checkout Branch
+     run: git checkout -b "${GITHUB_HEAD_REF:-$GITHUB_REF_NAME}.changes"

    - uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python }}

    - name: Install tools
      run: |
        sudo apt -y install libxml2-utils

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
        pip install -r test/requirements.txt
        pip install .

    - name: Build Unit Tests
      run: |
        cd test
        find . -name '*.html' -exec rm {} \;
        make html SPHINXOPTS='-W'
        (cd _build/html && rm genindex.html index.html search.html php-modindex.html)
        (cd _build/html && find . -name '*.html' -exec sh -c 'xmllint {} --xpath '"'"'//div[@role="main"]'"'"' | xmllint --format - > ../../{}' \;)

    - name: Diff Unit Tests Output
      run: |
        cd test
        rm -r _build
        git add . -N && git diff --exit-code

+   - run: git pull origin "${GITHUB_HEAD_REF:-$GITHUB_REF_NAME}.changes"

    - name: Push Unit Tests Output
      if: failure()
      uses: stefanzweifel/git-auto-commit-action@v4
      with:
        branch: ${{ github.head_ref || github.ref_name }}.changes
        # create_branch: true
        push_options: '--force'
        commit_message: Unit Tests Changes
        commit_user_name: Bot
        commit_user_email: bot@example.com
        commit_author: Bot <bot@example.com>

    - name: Build Unit Tests with toc show_parents=hide
      run: |
        cd test
        make html SPHINXOPTS='-W -D toc_object_entries_show_parents=hide'

    - name: Build Unit Tests with toc show_parents=domain
      run: |
        cd test
        make html SPHINXOPTS='-W -D toc_object_entries_show_parents=domain'

    - name: Build Unit Tests with toc show_parents=all
      run: |
        cd test
        make html SPHINXOPTS='-W -D toc_object_entries_show_parents=all'

(I haven't tested this code, but in theory something like this should work)

But I want to point out some problems you might run into with such a workflow.

stefanzweifel commented 1 year ago

I just also noticed, that you might run into issues with your workflow, if triggered by the scheduler: The actions/checkout-Action might checkout the repository in a detached state which could crash the git-auto-commit-action with the following message.

fatal: You are not currently on a branch.

It would probably be safer to checkout the main-branch if the triggered even was schedule

- uses: actions/checkout@v3
   with:
     ref: 'main'
mvorisek commented 1 year ago

Thank you for the detailed replies.

This PR is about create_branch: true to be for remote branch only. Thus the local branch should be always created if it does not exist. This is fairly safe as the local git is destroyed once a CI job ends anyway.