Home > Software design >  GitHub git hook on remote site to update wiki pages
GitHub git hook on remote site to update wiki pages

Time:12-10

The LTP project on GitHub stores wiki sources in doc/ directory (but there are other files which aren't wiki sources).

From time to time I update the GitHub wiki with a local script, which

  1. pulls ltp.wiki.git

  2. copies files from ltp.git doc/ directory into ltp.wiki.git

  3. git commit . in ltp.wiki.git

  4. git push in ltp.wiki.git

I'd like to have a git hook, which would do it after push on remote repository (post-update?). Is that possible?

CodePudding user response:

GitHub doesn't support Git hooks (on GitHub.com at least, they are supported on GitHub Enterprise). However, you can use GitHub Actions to run arbitrary code on a push, albeit in an isolated VM and not on the actual Git server.

In .github/workflows/wiki-mirror.yml (or whatever filename you want in .github/actions), you can add an Action to do the syncing. Since GitHub wikis expect markdown files, this changes the extensions of the .txt files to match:

name: "Mirror doc/ to wiki"

# Run on changes to the `doc` directory on the `master` branch
on:
  push:
    branches:
      - master
    paths:
      - 'doc/**'

jobs:
  mirror:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout main
        uses: actions/checkout@v2
        with:
          path: [repo name]
      - name: Checkout wiki
        uses: actions/checkout@v2
        with:
          repository: "[owner]/[repo name].wiki"
          path: [repo name].wiki
      - name: Copy files
        run: |
          git config --global user.email "[email protected]"
          git config --global user.name "Wiki mirror"
          cp $GITHUB_WORKSPACE/[repo name]/doc/*.txt $GITHUB_WORKSPACE/[repo name].wiki
          cd $GITHUB_WORKSPACE/[repo name].wiki
          rename.ul -v .txt .md *.txt
          git add .
          # only commit if there are changes
          git diff-index --quiet HEAD -- || git commit -m "sync from doc/"
          git push
  • Related