jimporter / mike

Manage multiple versions of your MkDocs-powered documentation via Git
BSD 3-Clause "New" or "Revised" License
534 stars 47 forks source link

GitHub Actions Refs Error #60

Closed sasial-dev closed 3 years ago

sasial-dev commented 3 years ago

I have a GH Action

name: Site Deployment

on:
  push:
    branches:
      - docs

jobs:
  build:
    name: Deploy
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Setup Python
        uses: actions/setup-python@v2
        with:
          python-version: 3.9.x

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

      - name: Set up git author
        run: |
          remote_repo="https://${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
          git config --global user.name "${GITHUB_ACTOR}"
          git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
          git remote rm origin
          git remote add origin "${remote_repo}"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Deploy
        run: mike deploy --push --rebase master latest

      - name: List
        run: mike list

and I am getting the error

error: failed to push branch gh-pages to origin: "To https://github.com/Lundstrong/LundstrongOrders.git
 ! [rejected]        gh-pages -> gh-pages (fetch first)
error: failed to push some refs to 'https://github.com/Lundstrong/LundstrongOrders.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."

How do I fix this, I've looked through the other issues and found no soulution.

jimporter commented 3 years ago

The problem you're seeing is because Github makes a shallow checkout when you use actions/checkout@v2, which means that when mike tries to do its thing on the gh-pages branch, it thinks it's a brand-new, empty branch. That works fine until pushing upstream, when it fails for the reason you see.

The easiest solution I know of is to add the following to your checkout action:

uses: actions/checkout@v2
with:
  fetch-depth: 0 # fetch all commits/branches

There are some more details in this issue as well.

There might be a better way to do this (which is why I haven't officially-recommended the above in the README), but it should work for you. If not, just let me know. I'll also try and figure out a proper solution to put in the README (or maybe even do something like this automatically?) since it's a subtle issue that tends to trip people up.

sasial-dev commented 3 years ago

Thanks so much!

sasial-dev commented 3 years ago

Hmm, I implimented it but I've still got errors

sasial-dev commented 3 years ago

https://github.com/Lundstrong/LundstrongOrders/blob/docs/.github/workflows/push-deploy.yml - here is the link so I don't spam the thread with the yml file I'm getting the same error.

jimporter commented 3 years ago

You probably want to get rid of --rebase in the mike command. I don't think that's necessary in your case and it might be causing problems. If nothing else, mike should provide more informative messages about the remote branch status without --rebase.

If that doesn't work, then you can try seeing whether your CI's version of gh-pages is right. Just add git log gh-pages -n1 somewhere and make sure the commit ID looks right and that it's on both your local gh-pages as well as origin/gh-pages. It should look something like:

commit <id> (origin/gh-pages, gh-pages)
jimporter commented 3 years ago

Oh, I'm also a little concerned about this bit:

          git remote rm origin
          git remote add origin "${remote_repo}"

I'm not totally sure what you're trying to do here, but I think it would mean that the fetch-depth solution I posted above won't work for you. If you run git remote rm origin and add a new origin, I don't think you'll have origin/gh-pages anymore. Thus, you're (probably) running into the same issue again: mike thinks gh-pages is a brand-new branch with no commits and then things fail when you try to push because your local gh-pages is unrelated to origin/gh-pages.

If you need the rm origin/add origin stuff, you could probably solve the problem by adding git fetch origin/gh-pages after it. Then you can remote the fetch-depth stuff from your checkout action.

sasial-dev commented 3 years ago

Ah okay. Was trying to fix the git.name and whatnot and "stole" that from #49 I'll remove rebase and experiment with the origin. Thanks again, I'll get back to you.

sasial-dev commented 3 years ago

Thanks! Works now Removed the origin and rebase.