简单的实践操作
本文原发表于个人博客,转换为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()
}
完成!看看效果吧~
测试成功~
文档生成
本次使用的是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
完成!测试一下~
成功~
结语
爽到了爽到了~