gitgithubgithub-actionsgithub-pages

Why can I not override dubious ownership for a GitHub workflow?


I am trying to set up a GitHub workflow for build the docs and keep on getting this issue.

Run ad-m/github-push-action@v0.5.0
/usr/bin/docker run ...
Push to branch gh-pages
fatal: detected dubious ownership in repository at '/github/workspace/docs/build/html'
To add an exception for this directory, call:

    git config --global --add safe.directory /github/workspace/docs/build/html

I have modified my workflow accordingly:

name: docs_pages_workflow 

on: 
  push:
    branches: [ main ]   

jobs:
 build_docs_job:
    runs-on: ubuntu-latest
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2.3.4

      - name: Set up Python
        uses: actions/setup-python@v2.2.1
        with:
          python-version: 3.9

      - name: Install dependencies
        run: |
          python -m pip install -U sphinx
          python -m pip install sphinx-rtd-theme
      - name: make the sphinx docs
        run: |
          mkdir -p docs
          make -C docs clean
          make -C docs html
      - name: Mark repo as safe
        run: git config --global --add safe.directory /github/workspace/docs/build/html

      - name: Init new repo in dist folder and commit
        run: |
          mkdir -p docs/build/html/
          cd docs/build/html/
          git init
          touch .nojekyll
          git add -A
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git commit -m 'deploy'
  
      - name: Force push to destination branch
        uses: ad-m/github-push-action@v0.5.0
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: gh-pages
          force: true
          directory: ./docs/build/html

I have also tried:

git config --global --add safe.directory $GITHUB_WORKSPACE
git config --global --add safe.directory docs/build/html

As well as calling it various ways in the script.

Any advice?


Solution

  • The problem was that the workflow declared the safe directory outside of the docker containter.

    This can apparently be fixed by instead using, though I have not tested it:

    - name: Allow git in Docker action
      run: |
        GIT_GLOBAL="/home/runner/work/_temp/_github_home/.gitconfig"
        git config --file "$GIT_GLOBAL" --add safe.directory "$GITHUB_WORKSPACE"
        git config --file "$GIT_GLOBAL" --add safe.directory "$GITHUB_WORKSPACE/docs/build/html"
    

    I opted to avoid the workaround and use an alternative deployment, official gh-pages deployment I found about instead of deploying to a branch, this resulted in changing the script to this:

    name: docs_pages_workflow 
    
    on:
      push:
        branches: [ main ]
      workflow_dispatch:
    
    permissions:
      contents: read
      pages: write
      id-token: write
    
    concurrency:
      group: "pages"
      cancel-in-progress: false
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
    
          - uses: actions/setup-python@v5
            with:
              python-version: '3.9'
    
          - name: Install dependencies
            run: |
              python -m pip install -U pip
              python -m pip install sphinx sphinx-rtd-theme
    
          - name: Build Sphinx
            run: |
              mkdir -p docs
              make -C docs clean
              make -C docs html
    
          - name: Upload artifact
            uses: actions/upload-pages-artifact@v3
            with:
              path: docs/build/html
    
      deploy:
        needs: build
        runs-on: ubuntu-latest
        environment:
          name: github-pages
          url: ${{ steps.deployment.outputs.page_url }}
        steps:
          - name: Deploy to GitHub Pages
            id: deployment
            uses: actions/deploy-pages@v4
    

    Courtesy of UnoxyRich over on github: https://github.com/orgs/community/discussions/169665