【问题标题】:Automate doc build on Github pages when new version is released新版本发布时自动在 Github 页面上构建文档
【发布时间】:2021-12-30 16:43:37
【问题描述】:

在 github 存储库 my_repo 上,我可以正确设置 github 操作来触发构建、测试和文档:

name: CMake

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

env:
  # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
  BUILD_TYPE: Release

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Install dependencies
      run: sudo apt-get install -y --no-install-recommends libboost-all-dev libgdal-dev doxygen graphviz

    - name: Configure CMake
      run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}

    - name: Build
      run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

    - name: Test
      working-directory: ${{github.workspace}}/build
      run: ctest -C ${{env.BUILD_TYPE}}

    - name: Docs
      working-directory: ${{github.workspace}}/build
      run: make doc

我还实施了 Release Drafter 来自动处理版本更新:

name: Release Drafter

on:
  push:
    branches:
      - master
  pull_request:

    types: [opened, reopened, synchronize]

jobs:
  update_release_draft:
    runs-on: ubuntu-latest
    steps:

      - uses: release-drafter/release-drafter@v5
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

现在,我想自动执行以下操作:

  • 主要版本已在 repo my_repo 中发布
  • 这会在我的 Github Pages repo 中触发一个事件
  • 文档建在我的 Github Pages repo 文件夹 softs/my_repo/docs
  • 网站发布(相当于commit变更,推送master分支)

我不完全知道如何实现它。我应该在我的 Github 页面中编写一个 github 工作流来“听”my_repo 项目中发生的事情吗?另外,我可以将 my_repo 中的版本转发到 Doxygen 吗?

【问题讨论】:

    标签: github-actions versioning doxygen


    【解决方案1】:

    我最终能够实现我的目标。我将发布此示例代码,以防下一个初学者使用 Github Action 自动构建文档:

        name: CMake
        
        on:
          push:
            branches: [ master ]
          pull_request:
            branches: [ master ]
        
        env:
          # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
          BUILD_TYPE: Release
        
        jobs:
          build:
            # The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac.
            # You can convert this to a matrix build if you need cross-platform coverage.
            # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
            runs-on: ubuntu-latest
        
            steps:
            - uses: actions/checkout@v2
              with:
                # we want to find git tags to pass version to doxygen
                fetch-depth: 0
        
            - name: Install quetzal and Doxygen dependencies
              run: sudo apt-get install -y --no-install-recommends libboost-all-dev libgdal-dev doxygen graphviz
        
            - name: Configure CMake
              # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
              # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
              run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
        
            - name: Build
              # Build your program with the given configuration
              run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
        
            - name: Test
              working-directory: ${{github.workspace}}/build
              # Execute tests defined by the CMake configuration.
              # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
              run: ctest -C ${{env.BUILD_TYPE}}
        
            - name: Generate documentation
              working-directory: ${{github.workspace}}/build
              # this is defined in the repo docs/CMakeLists.txt file
              run: make docs
        
            - name: Moving Files
              run: |
                mv ${{github.workspace}}/build/docs/html ./docs/api
        
              # Deploy to GitHub Pages
            - name: Deploy
              uses: peaceiris/actions-gh-pages@v3
              with:
                github_token: ${{ secrets.GITHUB_TOKEN }}
                publish_dir: ./
    
    

    project/docs/CMakeLists.txt

    # look for Doxygen package
    # Require dot, treat the other components as optional
    find_package(Doxygen
                 REQUIRED dot
                 OPTIONAL_COMPONENTS mscgen dia)
    
    if(DOXYGEN_FOUND)
      # exclude sqlite code
      set(DOXYGEN_EXCLUDE_PATTERNS
            */sqlite3/*
      )
      # doxygen settings can be set here, prefixed with "DOXYGEN_"
      set(DOXYGEN_PROJECT_NAME "my-project")
      set(DOXYGEN_INPUT "mainpage.md")
      set(DOXYGEN_USE_MDFILE_AS_MAINPAGE "mainpage.md")
      set(DOXYGEN_EXCLUDE_PATTERNS "README.md")
      set(DOXYGEN_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/docs")
      # this target will only be built if specifically asked to.
      # run "make docs" to create the doxygen documentation
      doxygen_add_docs(
        docs
        ${PROJECT_SOURCE_DIR}
        COMMENT "Generate API-documents for NoteSearch."
      )
    endif(DOXYGEN_FOUND)
    

    要自动检索版本号并将其传递给 Doxygen(以及 C++ 代码),我可以调整 Brian Milco 在此处给出的解决方案:https://ipenguin.ws/2012/11/cmake-automatically-use-git-tags-as.html。 他们在 2012 年发布了解决方案,因此在 2022 年可能会有更简单的方法来做同样的事情。但是,就我而言,它对我有用!

    在根 CMakeLists.txt 中:

    cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
    
    #
    # VERSIONING
    #
    
    # Appends the cmake/modules path to MAKE_MODULE_PATH variable.
    set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
    
    include(GetGitRevisionDescription)
    git_describe(VERSION --tags --dirty=-d)
    
    #parse the version information into pieces.
    string(REGEX REPLACE "^v([0-9]+)\\..*" "\\1" VERSION_MAJOR "${VERSION}")
    string(REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${VERSION}")
    string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${VERSION}")
    string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.[0-9]+(.*)" "\\1" VERSION_SHA1 "${VERSION}")
    set(VERSION_SHORT "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
    
    configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/version.cpp.in
                    ${CMAKE_CURRENT_BINARY_DIR}/version.cpp)
    
    set(version_file "${CMAKE_CURRENT_BINARY_DIR}/version.cpp")
    
    #Add the version_file to the executables being built or it won't compile.
    #add_executable(${PROJECT_NAME} ${source_files} ${ui_files} ${version_file})
    
    #
    # PROJECT DESCRIPTION
    #
    project(
      "project_name"
      LANGUAGES CXX
      VERSION ${VERSION_SHORT}
    

    这会将 CMake 项目版本设置为自动检索到的 git 版本标记,并默认在 set(DOXYGEN_PROJECT_NUMBER $(PROJECT_VERSION) 上传递给 Doxygen 模块。

    可以在https://github.com/Becheler/quetzal-CoalTL/commit/2ef5851cc6a34391d7a9ea64fb7c7122feb23b0a我的项目中找到完整的工作解决方案

    【讨论】:

      猜你喜欢
      • 2021-10-23
      • 2013-03-05
      • 2019-06-20
      • 2019-10-31
      • 2020-04-19
      • 2020-11-28
      • 2016-10-20
      • 2022-08-08
      • 1970-01-01
      相关资源
      最近更新 更多