Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub Actions deployment error: "Git push failed" #4336

Closed
mrousavy opened this issue Mar 3, 2021 · 6 comments
Closed

GitHub Actions deployment error: "Git push failed" #4336

mrousavy opened this issue Mar 3, 2021 · 6 comments
Labels
closed: question This issue is a user error/misunderstanding.

Comments

@mrousavy
Copy link

mrousavy commented Mar 3, 2021

🐛 Bug Report

I am trying to use GitHub actions to automatically build my documentation from the main branch (docs/ folder) and deploy it to the gh-pages branch.

I've followed the official guide, but can't seem to get it working.

Success! Generated static files in build. Use `npm run serve` to test your build locally.

Site successfully built in locale=en
CMD: git clone https://git@github.com/cuvent/react-native-vision-camera.git react-native-vision-camera-gh-pages (code=0)
CMD: git checkout origin/gh-pages (code=0)
CMD: git checkout -b gh-pages (code=0)
CMD: git branch --set-upstream-to=origin/gh-pages (code=0)
CMD: git rm -rf . (code=0)
CMD: git add --all (code=0)
CMD: git commit -m "Deploy website - based on a97a3cf6ec9602e116bdd774c50c85c1f8d89097" (code=0)
CMD: git push --force origin gh-pages (code=128)
/home/runner/work/react-native-vision-camera/react-native-vision-camera/docs/node_modules/@docusaurus/core/lib/commands/deploy.js:145
                throw new Error('Error: Git push failed');
                ^

Error: Error: Git push failed
    at /home/runner/work/react-native-vision-camera/react-native-vision-camera/docs/node_modules/@docusaurus/core/lib/commands/deploy.js:145:23
    at copyDirItems (/home/runner/work/react-native-vision-camera/react-native-vision-camera/docs/node_modules/fs-extra/lib/copy/copy.js:171:21)
    at /home/runner/work/react-native-vision-camera/react-native-vision-camera/docs/node_modules/fs-extra/lib/copy/copy.js:183:14
    at /home/runner/work/react-native-vision-camera/react-native-vision-camera/docs/node_modules/graceful-fs/polyfills.js:247:20
    at FSReqCallback.oncomplete (fs.js:156:23)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Error: Process completed with exit code 1.

This is my GitHub Action:

name: Build Documentation

on:
  pull_request:
    branches: [main]
  push:
    branches: [main]

jobs:
  checks:
    if: github.event_name != 'push'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-node@v1
        with:
          node-version: '12.x'
      - name: Test Build
        run: |
          if [ -e yarn.lock ]; then
          yarn install --frozen-lockfile
          elif [ -e package-lock.json ]; then
          npm ci
          else
          npm i
          fi
          npm run build
  gh-release:
    if: github.event_name != 'pull_request'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-node@v1
        with:
          node-version: '12.x'
      - name: Add key to allow access to repository
        env:
          SSH_AUTH_SOCK: /tmp/ssh_agent.sock
        run: |
          mkdir -p ~/.ssh
          ssh-keyscan github.com >> ~/.ssh/known_hosts
          echo "${{ secrets.GH_PAGES_DEPLOY }}" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          cat <<EOT >> ~/.ssh/config
          Host github.com
          HostName github.com
          IdentityFile ~/.ssh/id_rsa
          EOT
      - name: Release to GitHub Pages
        env:
          USE_SSH: false
          GIT_USER: git
          CURRENT_BRANCH: main
          DEPLOYMENT_BRANCH: gh-pages
          github_token: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git config --global user.email "actions@github.com"
          git config --global user.name "gh-actions"
          cd docs
          yarn install --frozen-lockfile
          yarn deploy

Have you read the Contributing Guidelines on issues?

yea

Your Environment

Reproducible Demo

See my repo, especially the docusaurus config file and the github action.

And here's the log output from the GitHub action.

Note that it does work if I run it locally, so I'm guessing something in the action isn't correctly configured/authenticated...

@mrousavy mrousavy added bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers labels Mar 3, 2021
@mrousavy
Copy link
Author

mrousavy commented Mar 3, 2021

I think this happens because I set USE_SSH: false. But if I use USE_SSH: true, the git command fails at git clone....

  • Here's the GitHub action log output with USE_SSH: false (notice error: Error: Git push failed)
  • Here's the GitHub action log output with USE_SSH: true (notice error: Error: git clone failed)

I also noticed that the Deploy Key has "never been used":
Screenshot 2021-03-03 at 14 14 34

Here's the "Secrets" Page:

image

Do I have to set anything in "Environments"?

image

@mrousavy
Copy link
Author

mrousavy commented Mar 3, 2021

Why does it even try to git clone again? When this code runs it already is in the git repo...

@slorber
Copy link
Collaborator

slorber commented Mar 4, 2021

can you try the new workflow introduced by this PR?
#4263

I'm not too familiar with GH action, would have to inspect the code. more carefully to understand this problem

@mrousavy
Copy link
Author

mrousavy commented Mar 9, 2021

@armano2 I'm pretty sure that won't work as I have already tried that before. I will try again later in case I missed anything.

@slorber so that's just moving the ssh-agent step into a dedicated action right?

I have currently got it working like this:

  gh-release:
    if: github.event_name != 'pull_request'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1

      - name: Add key to allow access to repository
        env:
          SSH_AUTH_SOCK: /tmp/ssh_agent.sock
        run: |
          mkdir -p ~/.ssh
          ssh-keyscan github.com >> ~/.ssh/known_hosts
          echo "${{ secrets.GH_PAGES_DEPLOY }}" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          cat <<EOT >> ~/.ssh/config
          Host github.com
          HostName github.com
          IdentityFile ~/.ssh/id_rsa
          EOT
      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - name: Restore node_modules from cache
        uses: actions/cache@v2
        id: yarn-cache
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-
      - name: Release to GitHub Pages
        env:
          USE_SSH: true
          GIT_USER: git
          CURRENT_BRANCH: main
          DEPLOYMENT_BRANCH: gh-pages
          github_token: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git config --global user.email "actions@github.com"
          git config --global user.name "gh-actions"
          yarn install --frozen-lockfile
          cd docs
          yarn install --frozen-lockfile
          yarn deploy

I'm not sure if that's the best, fastest or safest way, but it works. I'll try to remove all of the SSH stuff so I don't need to worry about the private/public key+secret and just try to use checkout@v2 like @armano2 commented.

@mrousavy
Copy link
Author

mrousavy commented Mar 9, 2021

@armano2 okay so I just tried it like this:

  gh-release:
    if: github.event_name != 'pull_request'
    name: Build & Release Documentation
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - name: Restore node_modules from cache
        uses: actions/cache@v2
        id: yarn-cache
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - name: Release to GitHub Pages
        env:
          USE_SSH: true
          GIT_USER: github-actions
          CURRENT_BRANCH: main
          DEPLOYMENT_BRANCH: gh-pages
          github_token: ${{ secrets.GITHUB_TOKEN }}
        run: |
          date > generated.txt
          git config user.name github-actions
          git config user.email github-actions@github.com
          yarn install --frozen-lockfile
          cd docs
          yarn install --frozen-lockfile
          yarn deploy

and it did not work. It fails at git clone again:

Screenshot 2021-03-09 at 14 13 33

And if I set USE_SSH: false, git push fails:

Screenshot 2021-03-09 at 14 16 29

@polarathene
Copy link
Contributor

polarathene commented Mar 12, 2021

The checkout action default is a shallow clone of the commit. You may need to pull the full history to properly be in sync when doing a push.

- uses: actions/checkout@v2
  with:
    fetch-depth: 0

Alternatively, as I have noticed (with a different deployment approach), gh-pages branch may not share a common commit if it was never branched off. For me this causes some issues with my Git GUI, I wouldn't be surprised if it causes any issues with regular git CLI either, unless you clone the branch separately.

Anyway, I started out with the approach that Docusaurus originally documented and found it over-complicated for deploying Github Pages vs other platforms. You can use a Github Action for deployment like this (no SSH keys required):

- name: 'Your build steps'
  run: |
    date > generated.txt
    yarn install --frozen-lockfile
    cd docs
    yarn install --frozen-lockfile
    yarn build

- name: 'Deploy to Github Pages'
  uses: peaceiris/actions-gh-pages@v3
  with:
    github_token: ${{ secrets.GITHUB_TOKEN }}
    publish_dir: ./docs/build
    user_name: 'github-actions[bot]'
    user_email: '41898282+github-actions[bot]@users.noreply.github.com'

Note I changed yarn deploy to yarn build. This is assuming your docs output a build to ./docs/build. I'm not sure what the earlier date and yarn install commands are for prior to changing to docs dir, as those won't be part of the deployment?


UPDATE: Full workflow example is available via this PR, it presently has plenty of inline comments to clarify anything about the workflow.

@Josh-Cena Josh-Cena added closed: question This issue is a user error/misunderstanding. and removed bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers labels Mar 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed: question This issue is a user error/misunderstanding.
Projects
None yet
Development

No branches or pull requests

4 participants