ad-m / github-push-action

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

Failed to sync with upstream repository with the help of this project. #193

Closed hongyi-zhao closed 7 months ago

hongyi-zhao commented 10 months ago

I tried to sync with the upstream repository with the help of this project via the workflow script here, but the error occurred, see here for more details, as shown below:

image

How do you think I could fix this error?

Regards, Zhao

ZPascal commented 8 months ago

I tried to sync with the upstream repository with the help of this project via the workflow script here, but the error occurred, see here for more details, as shown below:

image

How do you think I could fix this error?

Regards, Zhao

Hi @hongyi-zhao, is this case still up to date? The problem is not related to the push action in general. Is the fetched repository publicly accessible? If the repository is not available, it is necessary to inject the credentials for the repository.

hongyi-zhao commented 7 months ago

Is the fetched repository publicly accessible?

See below:

werner@X10DAi:~$ /usr/bin/git clone git://git.lyx.org/lyx
Cloning into 'lyx'...
remote: Enumerating objects: 504295, done.
remote: Counting objects: 100% (504295/504295), done.
^Cmote: Compressing objects:  76% (68942/90574)
ZPascal commented 7 months ago

Is the fetched repository publicly accessible?

See below:

werner@X10DAi:~$ /usr/bin/git clone git://git.lyx.org/lyx
Cloning into 'lyx'...
remote: Enumerating objects: 504295, done.
remote: Counting objects: 100% (504295/504295), done.
^Cmote: Compressing objects:  76% (68942/90574)

Hello @hongyi-zhao,

I have a feeling that the problem is a side effect of the missing git config --local --add safe.directory function in your workflow file. The setting is not included by default in the checkout action version v2. I recommend upgrading to v3 or v4. It would also be helpful if you could add the --verbose option to this line.

hongyi-zhao commented 7 months ago

I've recreated the workfolws based on your comments. Let's see if it can solve the problem.

ZPascal commented 7 months ago

I've recreated the workfolws based on your comments. Let's see if it can solve the problem.

Okay. You can also modify the workflow to trigger it manually.

hongyi-zhao commented 7 months ago

I have a feeling that the problem is a side effect of the missing git config --local --add safe.directory function in your workflow file. The setting is not included by default in the checkout action version v2. I recommend upgrading to v3 or v4.

Based on your above comments, it seems that if I use v3 or v4, the following line is unnecessary:

https://github.com/hongyi-zhao/lyx/blob/0b28df8809f6ad7d67d1785def4acd693f4dc4ec/workflows/sync-with-upstream-repository.yml#L20

But I'm not sure if I understand you correctly.

ZPascal commented 7 months ago

I have a feeling that the problem is a side effect of the missing git config --local --add safe.directory function in your workflow file. The setting is not included by default in the checkout action version v2. I recommend upgrading to v3 or v4.

Based on your above comments, it seems that if I use v3 or v4, the following line is unnecessary:

https://github.com/hongyi-zhao/lyx/blob/0b28df8809f6ad7d67d1785def4acd693f4dc4ec/workflows/sync-with-upstream-repository.yml#L20

But I'm not sure if I understand you correctly.

Yes, that is right.

hongyi-zhao commented 7 months ago

Ok, thank you for your confirmation, and I've updated the corresponding settings to the following:

https://github.com/hongyi-zhao/lyx/blob/fa31f7552f625ad75c6055edb09f3a4547ab53c8/workflows/sync-with-upstream-repository.yml#L17-L19

hongyi-zhao commented 7 months ago

Strangely, it has exceeded the preset running cycle of my workflow, but it did not run as expected, as shown here.

ZPascal commented 7 months ago

Strangely, it has exceeded the preset running cycle of my workflow, but it did not run as expected, as shown here.

@hongyi-zhao Is the workflow possibly paused? In general, you can also trigger the workflow manually for test purposes by adding this adaptation to it.

hongyi-zhao commented 7 months ago

Thank you very much for reminding me about this. It turned out that the workflows had been put into REPO_ROOT/workflows instead of REPO_ROOT/.github/workflows under the root of GitHub repository. I have corrected the problem and let's see if it can work as expected.

hongyi-zhao commented 7 months ago

It runs but still fails, as shown below, and you see here for more details:

image

ZPascal commented 7 months ago

Hi @hongyi-zhao, I think you can remove the duplicate checkout branch step and change the corresponding command by adding the --allow-unrelated-histories parameter to the execution section. I think this should solve the problem.

hongyi-zhao commented 7 months ago

I have repaired the workflows according to your suggestion: https://github.com/hongyi-zhao/lyx/blob/master/.github/workflows/sync-with-upstream-repository.yml

hongyi-zhao commented 7 months ago

Still failed, as shown below:

image

hongyi-zhao commented 7 months ago

I manually resolved the conflicts on my local machine, merged with the upstream, and then pushed it to my GitHub repo. Let's see if the workflow works in subsequent update runs.

ZPascal commented 7 months ago

Hi @hongyi-zhao,

You have some Git merge conflicts and the problem has nothing to do with the GitHub push action. I will try to support you to solve the case and set up a test environment on my side.

hongyi-zhao commented 7 months ago

Now, I use the following logic to implement automatic merging:

https://github.com/hongyi-zhao/lyx/blob/558e098789b19a00eebf2c1d406cb78280b17cca/.github/workflows/sync-with-upstream-repository.yml#L30-L35

ZPascal commented 7 months ago

I adjusted the GH action and was able to carry out a successful test. I also checked the runs here and found that you are missing authorizations for the token. You can solve this problem as follows.

hongyi-zhao commented 7 months ago

Thanks again for your patient help and guidance, I finally succeeded, as shown here.

hongyi-zhao commented 7 months ago

Although the workflow discussed here has been successful, I still have some additional questions, as shown here:

image

As shown in the figure above, this method produces many additional commits. Is there a way to avoid this problem by guaranteeing that pull only if upstream does have new commits, and then no additional commits are added in the updated GitHub mirror repo?

ZPascal commented 7 months ago

Yes, you can use the following code snipped to handle only valid commits.

      - name: Merge changes from upstream
        run: git merge upstream/master --allow-unrelated-histories -Xtheirs --no-edit # Ensure the upstream branch is correct

      - name: Check if push is necessary
        id: verify-changed-files
        run: |
          if [ -n "$(git status --untracked-files=no --porcelain)" ]; then
            echo "files_changed=true" >> "$GITHUB_OUTPUT";
          else
            echo "files_changed=false" >> "$GITHUB_OUTPUT";
          fi
hongyi-zhao commented 7 months ago

Then, how about the following implementation without using your github-push-action package and without using the temporary variable steps.verify-changed-files.outputs.files_changed?

name: Sync with upstream repository

on:
  workflow_dispatch:
  schedule:
    - cron: '0 */6 * * *' # Every 6 hours, can be adjusted as needed

jobs:
  sync:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          persist-credentials: false

      - name: Setup git
        run: |
          git config user.name "Hongyi Zhao"
          git config user.email "hongyi.zhao@gmail.com"

      - name: Add remote upstream
        run: git remote add upstream git://git.lyx.org/lyx

      - name: Fetch changes from upstream
        run: git fetch upstream

      - name: Merge changes from upstream
        run: git merge upstream/master --allow-unrelated-histories -Xtheirs --no-edit

      - name: Push changes if necessary
        run: |
          if git diff --quiet; then
            echo "No changes to push."
          else
            git push origin master
          fi
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ZPascal commented 7 months ago

Then, how about the following implementation without using your github-push-action package and without using the temporary variable steps.verify-changed-files.outputs.files_changed?

name: Sync with upstream repository

on:
  workflow_dispatch:
  schedule:
    - cron: '0 */6 * * *' # Every 6 hours, can be adjusted as needed

jobs:
  sync:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          persist-credentials: false

      - name: Setup git
        run: |
          git config user.name "Hongyi Zhao"
          git config user.email "hongyi.zhao@gmail.com"

      - name: Add remote upstream
        run: git remote add upstream git://git.lyx.org/lyx

      - name: Fetch changes from upstream
        run: git fetch upstream

      - name: Merge changes from upstream
        run: git merge upstream/master --allow-unrelated-histories -Xtheirs --no-edit

      - name: Push changes if necessary
        run: |
          if git diff --quiet; then
            echo "No changes to push."
          else
            git push origin master
          fi
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

@hongyi-zhao I don't think this is possible because then you will have problems using the credentials to push to the repository. To better explain the result, I've tested it here

hongyi-zhao commented 7 months ago

Hi @ZPascal,

Although your latest recommended workflow can avoid frequent updates, I ultimately found that it seems unable to fetch the latest updates from upstream. The latest update in my mirror repo is that I manually updated from the remote upstream repo to local, and then pushed it to the GitHub repo.

For more details, you can check the commit history here.

ZPascal commented 7 months ago

Hi @ZPascal,

Although your latest recommended workflow can avoid frequent updates, I ultimately found that it seems unable to fetch the latest updates from upstream. The latest update in my mirror repo is that I manually updated from the remote upstream repo to local, and then pushed it to the GitHub repo.

For more details, you can check the commit history here.

Hello @hongyi-zhao,

I rechecked it and found a suitable solution for your case.

Functionality: I forward the output of the fetch command to a file and check the line length in the file to see if a push is necessary.

Test cases: I reverted your commit in my fork and created a state to test the new functionality. After that, I tested the following cases:

  1. Identified diff and working push
  2. Non-diff and skipped push

Implementation:

    - name: Merge changes from upstream
      run: git merge upstream/master --allow-unrelated-histories -Xtheirs --no-edit > log.txt # Ensure the upstream branch is correct

    - name: Check if push is necessary
      id: verify-changed-files
      run: |
        if [ $(wc -l log.txt | sed 's/ log.txt//g') -gt 2 ]; then
          echo "files_changed=true" >> "$GITHUB_OUTPUT";
        else
          echo "files_changed=false" >> "$GITHUB_OUTPUT";
        fi
        rm -rf log.txt

I hope this helps you. I will leave the fork for a few more days to observe the orderly effect of the functionality.

hongyi-zhao commented 7 months ago

After a period of observation, it seems that your above modification has achieved the expected functionality.