如何利用GitHubAction自动化测试你的kotlin程序并部署文档到GithubPage

简单的实践操作

本文原发表于个人博客,转换为CSDN可能排版可能有些许差异,正确排版原文可以查看
地址:https://blog.fivezha.cn/2020/07/28/github-action-kotlin-test-action/

前言

这两天一直在写算法,但是一个一个测试太麻烦了,并且也没有文档看,简直累人,所以就写了个action让它自动生成并部署到GithubPage上面去,爽了。

测试

引入依赖

dependencies {
    ......
    testImplementation(
            'org.assertj:assertj-core:3.12.2',
            'org.junit.jupiter:junit-jupiter-api:5.4.2'
    )
    testRuntime('org.junit.jupiter:junit-jupiter-engine:5.4.2')
}

编写测试task

test {
    useJUnitPlatform()
}

完成!看看效果吧~

test

测试成功~

文档生成

本次使用的是kotlin官方文档生成器dokka,内含有多种生成方案,并且可以集成到Gradle中,爽的。

引入依赖

这里以插件方式引入:

plugins {
    ......
    id 'org.jetbrains.dokka' version '0.10.1'
}

编写task

dokka {
    // 样式,具体看官方文档
    outputFormat = 'javadoc'
    outputDirectory = "$buildDir/dokka"
    disableAutoconfiguration = false

    cacheRoot = 'default'

    configuration {
        moduleName = 'doc'

        // Use to include or exclude non public members.
        includeNonPublic = false

        // Do not output deprecated members. Applies globally, can be overridden by packageOptions
        skipDeprecated = false

        // Emit warnings about not documented members. Applies globally, also can be overridden by packageOptions
        reportUndocumented = true

        // Do not create index pages for empty packages
        skipEmptyPackages = true

        // This is a list of platform names that will be shown in the final result. See the "Platforms" section of this readme
        targets = ["JVM"]

        // Platform used for code analysis. See the "Platforms" section of this readme
        platform = "JVM"

        sourceRoot {
            // 源文件目录
            path = "src"
        }

        sourceLink {
            // Unix based directory relative path to the root of the project (where you execute gradle respectively).
            path = "src/main/kotlin" // or simply "./"

            // URL showing where the source code can be accessed through the web browser
            url = "https://github.com/xmmmmmovo/Algorithms4thEditionKotlinSolutions/blob/master/src/main/kotlin" //remove src/main/kotlin if you use "./" above

            // 指定行数前缀,github是#L
            lineSuffix = "#L"
        }

        // 用于链接java官方文档
        jdkVersion = 8

        // Disable linking to online kotlin-stdlib documentation
        noStdlibLink = false

        // Disable linking to online JDK documentation
        noJdkLink = false
    }
}

完成!看看效果~

成功~

自动化

每次都运行脚本太累了,干脆直接用CI自动跑了,这里用的是GithubAction,具体如何使用可以看:如何利用GitHubAction和GithubPage部署React应用 这篇,这里只写出yml文件:

# workflow name
name: Test Workflow

on:
  push:
    branches:
      - master

jobs:
  test:
    name: Kotlin Tests
    runs-on: ubuntu-latest
    env:
      TZ: Asia/Shanghai

    steps:
      # check it to your workflow can access it
      # from: https://github.com/actions/checkout
      - name: Checkout Repository master branch
        uses: actions/checkout@master

      # 验证wrapper
      - name: validate wrapper
        uses: gradle/wrapper-validation-action@v1

      # 安装jdk
      - name: Set up JDK 8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8

      # 缓存
      - name: Cache Gradle packages
        uses: actions/cache@v2
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
          restore-keys: ${{ runner.os }}-gradle

      # 给权限
      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      # 测试程序
      - name: Test with gradle
        run: ./gradlew test
# workflow name
name: Doc Workflow

on:
  release:
    types: [published]
    branches:
      - master

jobs:
  test:
    name: Kotlin Documentation Generation
    runs-on: ubuntu-latest
    env:
      TZ: Asia/Shanghai

    steps:
      # check it to your workflow can access it
      # from: https://github.com/actions/checkout
      - name: Checkout Repository master branch
        uses: actions/checkout@master

      # 验证wrapper
      - name: validate wrapper
        uses: gradle/wrapper-validation-action@v1

      # 安装jdk
      - name: Set up JDK 8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8

      # 缓存
      - name: Cache Gradle packages
        uses: actions/cache@v2
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
          restore-keys: ${{ runner.os }}-gradle

      # 给权限
      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      # 缓存
      # 暂时还未找到hash值的解决方案,找到了请pr
      # - name: Cache Dokka
      #   uses: actions/cache@v2
      #   with:
      #     path: ~/.cache/dokka
      #     key: ${{ runner.os }}-dokka-${{ hashFiles('**/*') }}
      #     restore-keys: ${{ runner.os }}-dokka

      # 生成文档
      - name: Generate documentation
        run: ./gradlew dokka

      # 部署到ghpage
      - name: Deploy to gh-pages
        uses: peaceiris/actions-gh-pages@v3
        with:
          deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
          publish_dir: ./build/dokka

完成!测试一下~

成功~

结语

爽到了爽到了~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值