From ca70af724e80b23f0d973979e895184bcffb7212 Mon Sep 17 00:00:00 2001 From: mattcc Date: Tue, 15 Feb 2022 16:35:33 -0600 Subject: [PATCH] Add CI+CD and update gradle build process --- .github/workflows/continuous_integration.yml | 42 +++++++++++ .gitignore | 5 -- build.gradle | 73 ++++++++++++-------- docker-compose.ci.yml | 15 ++++ docker-compose.yml | 11 --- gradle.properties | 10 --- gradle/wrapper/gradle-wrapper.properties | 2 +- libs.versions.toml | 27 ++++++++ scripts/cibuild | 33 +++++++++ settings.gradle | 21 ++++++ 10 files changed, 185 insertions(+), 54 deletions(-) create mode 100644 .github/workflows/continuous_integration.yml create mode 100644 docker-compose.ci.yml delete mode 100644 docker-compose.yml create mode 100644 libs.versions.toml create mode 100755 scripts/cibuild diff --git a/.github/workflows/continuous_integration.yml b/.github/workflows/continuous_integration.yml new file mode 100644 index 0000000..ab24ef9 --- /dev/null +++ b/.github/workflows/continuous_integration.yml @@ -0,0 +1,42 @@ +name: CI + +on: + push: + branches: + - 'master' + tags: + - 'v*' + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Caches Gradle dependencies and wrappers + uses: actions/cache@v2 + with: + path: | + ~/.gradle/caches/modules-2 + ~/.gradle/wrapper/dists + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle', '**/libs.versions.toml', '**/gradle/wrapper/gradle-wrapper.properties') }} + restore-keys: | + ${{ runner.os }}-gradle- + + - name: Run cibuild script + id: exec + run: ./scripts/cibuild + + - name: Cleanup Gradle Cache + run: | + sudo rm -f ~/.gradle/caches/modules-2/modules-2.lock + sudo rm -f ~/.gradle/caches/modules-2/gc.properties + + - name: Create release if this is a tagged workflow + uses: ncipollo/release-action@v1 + if: startsWith(github.ref, 'refs/tags/') + with: + artifacts: build/libs/${{ steps.exec.outputs.slimjar }},build/libs/${{ steps.exec.outputs.libszip }},build/libs/${{ steps.exec.outputs.fatjar }} + name: NiSCore ${{ steps.exec.outputs.version }} + token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 916b6a2..573e847 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,3 @@ .idea/ .gradle/ -jars/ build/ -*/build/ -mcserver/ - -EnhancedChat/*.sqlite \ No newline at end of file diff --git a/build.gradle b/build.gradle index ce22ab5..261652d 100644 --- a/build.gradle +++ b/build.gradle @@ -1,14 +1,22 @@ plugins { id 'java' - id 'nu.studer.jooq' version '5.2.1' - id 'com.palantir.git-version' version '0.12.3' + alias libs.plugins.jooqgradle + alias libs.plugins.gitgradle } +final git = versionDetails() + group 'space.nerdsin.plugins' -version gitVersion() +version "${git.lastTag.substring(1)}.r${git.commitDistance}" archivesBaseName = "$pluginName" -sourceCompatibility = targetCompatibility = JavaVersion.VERSION_16 +final isCiBuilding = System.getenv().get('CI') == 'true' + +java { + toolchain { + languageVersion = JavaLanguageVersion.of(17) + } +} configurations { compileJar {} @@ -18,7 +26,7 @@ configurations { } jooq { - version = jooqVersion + version = libs.versions.jooq configurations { main { @@ -62,29 +70,21 @@ jooq { } } -repositories { - mavenCentral() - maven { url = 'https://papermc.io/repo/repository/maven-public/' } -} - dependencies { - implementation group: 'com.destroystokyo.paper', name: 'paper-api', version: paperApiVersion + implementation libs.paperapi - compileOnly group: 'org.projectlombok', name: 'lombok', version: lombokVersion - annotationProcessor group: 'org.projectlombok', name: 'lombok', version: lombokVersion + compileOnly libs.lombok + annotationProcessor libs.lombok - compileJar group: 'com.h2database', name: 'h2', version: h2DatabaseVersion - jooqGenerator group: 'com.h2database', name: 'h2', version: h2DatabaseVersion + compileJar libs.h2 - compileJar group: 'org.jooq', name: 'jooq' - jooqGenerator group: 'org.jooq', name: 'jooq-meta' - jooqGenerator group: 'org.jooq', name: 'jooq-meta-extensions' - jooqGenerator group: 'org.jooq', name: 'jooq-codegen' + compileJar libs.jooq + jooqGenerator libs.bundles.jooqgen - compileJar group: 'org.flywaydb', name: 'flyway-core', version: flywayCoreVersion + compileJar libs.flyway - compileJar group: 'com.google.inject', name: 'guice', version: guiceVersion - compileJar group: 'org.reflections', name: 'reflections', version: reflectionsVersion + compileJar libs.guice + compileJar libs.reflections } jar { @@ -95,9 +95,16 @@ jar { attributes("Class-Path": configurations.compileJar.collect { "libs/${it.getName()}" }.join(' ')) } - finalizedBy task("copyLibs", type: Copy) { +// finalizedBy task("copyLibs", type: Copy) { +// from configurations.compileJar +// into tasks.jar.destinationDirectory.dir("libs") +// } + + finalizedBy task("zipLibs", type: Zip) { + archiveBaseName = "$archivesBaseName-libs" + destinationDirectory = tasks.jar.destinationDirectory + from configurations.compileJar - into tasks.jar.destinationDirectory.dir("libs") } } @@ -122,9 +129,7 @@ processResources { filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: [ 'name' : archivesBaseName, 'class' : pluginClass, - 'version' : (project.version =~ /v([\d]+\.[\d]+\.[\d]+).*/) - .with { it[0] } - .with { m, ver -> ver }, + 'version' : git.lastTag.substring(1), 'apiversion': pluginApiVersion ]); } @@ -136,3 +141,17 @@ processResources { ]); } } + +task createGitHubActionsVars() { + onlyIf { isCiBuilding } + doFirst { + println "::set-output name=slimjar::${jar.archiveFileName.get()}" + println "::set-output name=libszip::${tasks.zipLibs.archiveFileName.get()}" + println "::set-output name=fatjar::${fatJar.archiveFileName.get()}" + println "::set-output name=version::${project.version}" + } +} + +build { + finalizedBy createGitHubActionsVars +} diff --git a/docker-compose.ci.yml b/docker-compose.ci.yml new file mode 100644 index 0000000..87b0a45 --- /dev/null +++ b/docker-compose.ci.yml @@ -0,0 +1,15 @@ +version: '2.4' + +services: + gradle: + image: eclipse-temurin:17.0.2_8-jdk-focal + environment: + - CI=${CI:-} + volumes: + # gradle caches + - ~/.gradle/caches/modules-2:/root/.gradle/caches/modules-2:z + - ~/.gradle/wrapper/dists:/root/.gradle/wrapper/dists:z + # mount working directory + - ./:/opt/build:Z + working_dir: /opt/build + command: bash -c 'set -x && ./gradlew --no-daemon clean build --stacktrace' \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 07f52c4..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,11 +0,0 @@ -version: '2.4' -services: - niscore-build: - container_name: niscore-build - image: adoptopenjdk/openjdk16 - working_dir: /opt/build - entrypoint: bash -c "chmod +x ./gradlew && ./gradlew clean build" - environment: - GRADLE_USER_HOME: /opt/build/.gradle/caches - volumes: - - .:/opt/build:Z \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 86ded50..9451c54 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,13 +1,3 @@ -# libraries -paperApiVersion=1.16.5-R0.1-SNAPSHOT -jooqVersion=3.14.11 -h2DatabaseVersion=1.4.200 -flywayCoreVersion=7.10.0 -lombokVersion=1.18.20 -guiceVersion=5.0.1 -reflectionsVersion=0.9.12 - -# plugin pluginApiVersion=1.13 pluginClass=space.nerdsin.plugins.PluginCore pluginName=NiSCore diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index d58c14b..573b621 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ #Sat Oct 19 18:12:56 CDT 2019 -distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStorePath=wrapper/dists diff --git a/libs.versions.toml b/libs.versions.toml new file mode 100644 index 0000000..81bc4e1 --- /dev/null +++ b/libs.versions.toml @@ -0,0 +1,27 @@ +[versions] +paper-api = "1.18.1-R0.1-SNAPSHOT" +jooq = "3.16.4" +h2 = "2.1.210" +flyway = "8.5.0" +lombok = "1.18.22" +guice = "5.1.0" +reflections = "0.10.2" + +[libraries] +paperapi = { group = "io.papermc.paper", name = "paper-api", version.ref = "paper-api" } +lombok = { group = "org.projectlombok", name = "lombok", version.ref = "lombok" } +h2 = { group = "com.h2database", name = "h2", version.ref = "h2" } +jooq = { group = "org.jooq", name = "jooq", version.ref = "jooq" } +jooqmeta = { group = "org.jooq", name = "jooq-meta", version.ref = "jooq" } +jooqmetaext = { group = "org.jooq", name = "jooq-meta-extensions", version.ref = "jooq" } +jooqcodegen = { group = "org.jooq", name = "jooq-codegen", version.ref = "jooq" } +flyway = { group = "org.flywaydb", name = "flyway-core", version.ref = "flyway" } +guice = { group = "com.google.inject", name = "guice", version.ref = "guice" } +reflections = { group = "org.reflections", name = "reflections", version.ref = "reflections" } + +[bundles] +jooqgen = ["jooqmeta", "jooqmetaext", "jooqcodegen", "h2"] + +[plugins] +jooqgradle = { id = "nu.studer.jooq", version = "5.2.1" } +gitgradle = { id = "com.palantir.git-version", version = "0.12.3" } \ No newline at end of file diff --git a/scripts/cibuild b/scripts/cibuild new file mode 100755 index 0000000..16f3913 --- /dev/null +++ b/scripts/cibuild @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +set -e + +if [[ -n "${DEBUG_MODE}" ]]; then + set -x +fi + +function usage() { + echo -n \ + "Usage: $(basename "$0") +Build application for staging or a release. +" +} + +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + if [[ "${1:-}" == "--help" ]]; then + usage + else + # if mounting the hosts cache folder, gradle inside the container will try and + # acquire a file lock that the host machine may already be holding. this will + # result in the container hanging indefinitely. stopping any existing daemons + # on the host is the simplest way to 'solve' this issue + if command -v java &>/dev/null; then + ./gradlew --stop + fi + + chmod +x ./gradlew + + CI="${CI:-}" docker-compose -f docker-compose.ci.yml \ + run --rm gradle + fi +fi diff --git a/settings.gradle b/settings.gradle index cc7b4d7..ce80f45 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1,22 @@ +pluginManagement { + repositories { + gradlePluginPortal() + } +} + +enableFeaturePreview 'VERSION_CATALOGS' + +dependencyResolutionManagement { + repositories { + mavenCentral() + maven { url = 'https://papermc.io/repo/repository/maven-public/' } + } + + versionCatalogs { + libs { + from files('libs.versions.toml') + } + } +} + rootProject.name = 'plugins'