Skip to content

Instantly share code, notes, and snippets.

@shundhammer
Last active January 24, 2024 21:52
Show Gist options
  • Save shundhammer/ed359db0d9329d4db551528256060d2a to your computer and use it in GitHub Desktop.
Save shundhammer/ed359db0d9329d4db551528256060d2a to your computer and use it in GitHub Desktop.
Doxygen-Autodocs on GitHub Pages

Doxygen-Generated Autodocs on GitHub Pages

General Idea

GitHub offers hosting documentation on GitHub Pages. This can be done once for each GitHub user, for each GitHub organization, for each GitHub project.

If the process for building the autodocs is already in place (as is the case for libyui), it is just a matter of generating them, moving them to the right place, and deploying them to the project's GitHub Pages.

The Magic "gh-pages" Branch

GitHub pages use a special branch named gh-pages. Put an index.html or an index.md file to a directory, commit it to that branch, and you'll get a page on the project's GitHub page. The URL is something like https://username.github.io/projectname/... plus of course the directory hierarchy where the index.html or index.md file is.

Manual Process

To generate and publish the C++ class documentation for the libyui base library in my fork, I did:

  • Clone the repo and go to the checkout:
git clone -o mine git@github.com:shundhammer/libyui.git
cd libyui
  • Create a fresh empty branch named gh-pages (the exact branch name is important!):
git branch | grep -q "gh-pages" && git branch -D gh-pages
git branch gh-pages
git checkout gh-pages
  • Generate the autodocs:
make -C libyui -f Makefile.repo doc

The autodocs are now in ./libyui/build/doc/html.

  • Move the freshly generated autodocs to the desired directory and add them to the gh-pages branch:
rm -rf api-doc
mv libyui/build/doc/html api-doc
git add api-doc/**
git commit -am "Generated API doc"
git push -f mine gh-pages:gh-pages

Automation

This was implemented as a GitHub action with libyui/libyui#26: We already had a docker image from which to build libyui, so it was just a matter to add a new GitHub action that executes the above steps:

Some GitHub authentication token might be required to have push permissions in the actions.

Amazingly enough, no additional credentials were required to push to the gh-pages branch; but the Git committer needs to be identified with git config.

@shundhammer
Copy link
Author

shundhammer commented Apr 26, 2021

@shundhammer
Copy link
Author

The exact YML file for the GitHub action definition:

https://github.com/libyui/libyui/blob/master/.github/workflows/publish-api-doc.yml

# This generates Doxygen autodocs for the C++ API (for the libyui base lib)
# and pushes them to the "gh-pages" branch which publishes them
# to the project's GitHub pages at https://libyui.github.io/libyui/api-doc/
#
# See also
#
#   https://gist.github.com/shundhammer/ed359db0d9329d4db551528256060d2a
#
# GitHub actions syntax:
#   https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions

name: publish-api-doc

# Trigger this when a pull request is merged (which implies pushing to master).
on:
  push:
    branches:
      - master

jobs:
  api-doc:
    runs-on: ubuntu-latest
    container: registry.opensuse.org/devel/libraries/libyui/containers/libyui-devel:latest

    steps:

    - name: Git Checkout
      uses: actions/checkout@v2

    - name: Create clean gh-pages branch
      run:  git checkout -b gh-pages

    - name: Generate autodocs for libyui base lib
      run:  make -C libyui -f Makefile.repo doc

    - name: Move generated autodocs to target directory
      run:  mv libyui/build/doc/html api-doc

    - name: Add generated autodocs to Git repo in the gh-pages branch
      run:  |
        git config --global user.email "libyui-github-bot@suse.com"
        git config --global user.name  "$GITHUB_WORKFLOW GitHub action"
        git add api-doc
        git commit -am "Generated API doc"

    - name: Publish autodocs as GitHub pages
      run:  git push -f origin gh-pages:gh-pages

    - name: Result URLs
      run:  |
        REPO_OWNER=$(echo $GITHUB_REPOSITORY | cut -d '/' -f 1)
        REPO_NAME=$(echo $GITHUB_REPOSITORY | cut -d '/' -f 2)
        echo "Formatted API docs:  https://$REPO_OWNER.github.io/$REPO_NAME/api-doc"
        echo ""
        echo "GitHub pages branch: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/tree/gh-pages" 

@Siddhant-K-code
Copy link

This will also work:

https://gist.github.com/Siddhant-K-code/05f06eaa478d11627c6b9253bc0d2996

name: Doxygen

on:
  repository_dispatch:
  push:
    branches:
      - master
      - gh-pages

# In that case do the job 'make_and_deploy_doxygen'
jobs:
  make_and_deploy_doxygen:
    runs-on: ubuntu-latest
    # which needs the following steps to be executed:
    steps:
      # 1. Checkout current branch of GitHub repository.
      - name: Checkout current branch
        uses: actions/checkout@v2
      # 2. Install doxygen and its auxiliary components.
      - name: Install doxygen and latex components
        run: sudo apt-get update; sudo apt-get install -y doxygen graphviz texlive-full
      # 3. Create the doxygen pages.
      - name: Create the doxygen
        run: |
          git clone https://github.com/metacall/core.git
          mkdir core/build && cd core/build
          cmake -DOPTION_BUILD_DOCS=ON ..
          make api-docs
      - name: Moving Files
        run: |
          mv ./core/build/docs/api-docs ./api

      # Deploy to GitHub Pages
      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment