Gradle で Asciidoc 変換して Github Action で Github Pages で公開する


build.gradle.kts

org.asciidoctor.jvm.convert プラグインを導入して設定する。

plugins {
    id("org.asciidoctor.jvm.convert") version "3.3.2"
}

// asciidoctor config
val asciidoctorExtensions: Configuration by configurations.creating
tasks.asciidoctor {
    baseDirFollowsSourceFile()
    sources(delegateClosureOf<PatternSet> {
        include("index.adoc")
    })
    forkOptions {
        jvmArgs("--add-opens", "java.base/sun.nio.ch=ALL-UNNAMED")
        jvmArgs("--add-opens", "java.base/java.io=ALL-UNNAMED")
    }
}
  • app/build/docs/asciidoc/index.adoc が Asciidoc のソース
  • baseDirFollowsSourceFile() で同ディレクトリを起点に設定
  • Java モジュールシステム用に forkOptions を指定


index.adoc

app/build/docs/asciidoc/index.adoc に以下のような感じで作成。

include::attribute.adoc[]

= Document Title

toc::[]

include::01-contents.adoc[]

include::02-contents.adoc[]

app/build/docs/asciidoc/attribute.adoc は以下のようなAsciidocの属性を定義(直接 index.adoc に記載しても可)。

:encoding: utf-8
:backend: html5
:doctype: article
:toc: left
:toclevels: 3
:source-highlighter: rouge
:icons: font
:sectanchors:
:sectanchors:


変換は以下

$ ./gradlew asciidoctor

とすると、build/docs/asciidoc 以下に index.html が作成される。


Github Action

.github/workflows/gh-pages.yml を作成。

name: GitHub Pages

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

jobs:
  doc:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v3
      - name: Set up JDK 18
        uses: actions/setup-java@v3
        with:
          java-version: '18'
          distribution: 'temurin'
      - name: Asciidoc with Gradle
        uses: gradle/gradle-build-action@0d13054264b0bb894ded474f08ebb30921341cee
        with:
          arguments: asciidoctor
      - name: publish
        if: ${{ github.ref == 'refs/heads/main' }}
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./app/build/docs/asciidoc

Gradle で、asciidoctor して Asciidoc 変換し、actions-gh-pages で変換したhtmlファイルを公開。

上記例では、actions-gh-pages により、./app/build/docs/asciidoc ディレクトリの中身が、gh-pages ブランチ(自動的に作成してくれる)に配置される。


Github Pages

gh-pages ブランチを Github Pages で公開。

これでプッシュすれば、その内容で公開される。