diff --git a/Jenkinsfile b/Jenkinsfile index 5f99a4abd..ce4863175 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -23,8 +23,13 @@ pipeline { stage('Build') { steps { - echo 'Building project.' - sh './gradlew build publish --stacktrace --warn' + withCredentials([ + // build_secrets is parsed in SubprojectExtension#loadSecrets + file(credentialsId: 'build_secrets', variable: 'ORG_GRADLE_PROJECT_secretFile'), + ]) { + echo 'Building project.' + sh './gradlew build publish --stacktrace --warn' + } } } } @@ -33,7 +38,7 @@ pipeline { always { - archiveArtifacts artifacts: '**/build/libs/*.jar', fingerprint: true + archiveArtifacts artifacts: '**/build/libs/**/*.jar', fingerprint: true withCredentials([ string(credentialsId: 'discord_webhook_url', variable: 'DISCORD_URL') diff --git a/buildSrc/src/main/kotlin/dev/engine_room/gradle/subproject/SubprojectExtension.kt b/buildSrc/src/main/kotlin/dev/engine_room/gradle/subproject/SubprojectExtension.kt index 223538724..94a67712f 100644 --- a/buildSrc/src/main/kotlin/dev/engine_room/gradle/subproject/SubprojectExtension.kt +++ b/buildSrc/src/main/kotlin/dev/engine_room/gradle/subproject/SubprojectExtension.kt @@ -14,9 +14,14 @@ import org.gradle.jvm.tasks.Jar import org.gradle.jvm.toolchain.JavaLanguageVersion import org.gradle.kotlin.dsl.* import org.gradle.language.jvm.tasks.ProcessResources +import java.io.File +import java.net.URI +import java.util.* open class SubprojectExtension(val project: Project) { fun init(archiveBase: String, group: String, version: String) { + loadSecrets() + setBaseProperties(archiveBase, group, version) setupJava() addRepositories() @@ -154,6 +159,48 @@ open class SubprojectExtension(val project: Project) { if (project.hasProperty("mavendir")) { maven(project.rootProject.file(project.property("mavendir") as String)) } + + // Sets maven credentials if they are provided. This is generally + // only used for external/remote uploads. + if (project.hasProperty("mavenUsername") && project.hasProperty("mavenPassword") && project.hasProperty("mavenURL")) { + maven { + credentials { + username = project.property("mavenUsername") as String + password = project.property("mavenPassword") as String + } + url = URI.create(project.property("mavenURL") as String) + } + } + } + } + + private fun loadSecrets() { + // Detects the secrets file provided by CI and loads it into the project properties + if (project.rootProject.hasProperty("secretFile")) { + project.logger.lifecycle("Automatically loading properties from the secretFile") + val secretsFile = project.rootProject.file(project.rootProject.property("secretFile") as String) + + if (secretsFile.exists() && secretsFile.name.endsWith(".properties")) { + loadProperties(secretsFile) + } + } + } + + private fun loadProperties(propertyFile: File) { + if (propertyFile.exists()) { + val properties = Properties().apply { load(propertyFile.inputStream()) } + + var count = 0 + for (entry in properties.entries) { + if (entry.key is String) { + project.setProperty(entry.key as String, entry.value) + count++ + } + } + + project.logger.lifecycle("Successfully loaded $count properties") + } else { + project.logger.warn("The property file ${propertyFile.getName()} could not be loaded. It does not exist.") } } }