Skip to content

Commit 0288b37

Browse files
committed
There's always another secret
- Parse a build_secrets.properties file provided by CI - Expand archive glob to include api artifacts
1 parent d596345 commit 0288b37

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

Jenkinsfile

+8-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ pipeline {
2323
stage('Build') {
2424

2525
steps {
26-
echo 'Building project.'
27-
sh './gradlew build publish --stacktrace --warn'
26+
withCredentials([
27+
// build_secrets is parsed in SubprojectExtension#loadSecrets
28+
file(credentialsId: 'build_secrets', variable: 'ORG_GRADLE_PROJECT_secretFile'),
29+
]) {
30+
echo 'Building project.'
31+
sh './gradlew build publish --stacktrace --warn'
32+
}
2833
}
2934
}
3035
}
@@ -33,7 +38,7 @@ pipeline {
3338

3439
always {
3540

36-
archiveArtifacts artifacts: '**/build/libs/*.jar', fingerprint: true
41+
archiveArtifacts artifacts: '**/build/libs/**/*.jar', fingerprint: true
3742

3843
withCredentials([
3944
string(credentialsId: 'discord_webhook_url', variable: 'DISCORD_URL')

buildSrc/src/main/kotlin/dev/engine_room/gradle/subproject/SubprojectExtension.kt

+47
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ import org.gradle.jvm.tasks.Jar
1414
import org.gradle.jvm.toolchain.JavaLanguageVersion
1515
import org.gradle.kotlin.dsl.*
1616
import org.gradle.language.jvm.tasks.ProcessResources
17+
import java.io.File
18+
import java.net.URI
19+
import java.util.*
1720

1821
open class SubprojectExtension(val project: Project) {
1922
fun init(archiveBase: String, group: String, version: String) {
23+
loadSecrets()
24+
2025
setBaseProperties(archiveBase, group, version)
2126
setupJava()
2227
addRepositories()
@@ -154,6 +159,48 @@ open class SubprojectExtension(val project: Project) {
154159
if (project.hasProperty("mavendir")) {
155160
maven(project.rootProject.file(project.property("mavendir") as String))
156161
}
162+
163+
// Sets maven credentials if they are provided. This is generally
164+
// only used for external/remote uploads.
165+
if (project.hasProperty("mavenUsername") && project.hasProperty("mavenPassword") && project.hasProperty("mavenURL")) {
166+
maven {
167+
credentials {
168+
username = project.property("mavenUsername") as String
169+
password = project.property("mavenPassword") as String
170+
}
171+
url = URI.create(project.property("mavenURL") as String)
172+
}
173+
}
174+
}
175+
}
176+
177+
private fun loadSecrets() {
178+
// Detects the secrets file provided by CI and loads it into the project properties
179+
if (project.rootProject.hasProperty("secretFile")) {
180+
project.logger.lifecycle("Automatically loading properties from the secretFile")
181+
val secretsFile = project.rootProject.file(project.rootProject.property("secretFile") as String)
182+
183+
if (secretsFile.exists() && secretsFile.name.endsWith(".properties")) {
184+
loadProperties(secretsFile)
185+
}
186+
}
187+
}
188+
189+
private fun loadProperties(propertyFile: File) {
190+
if (propertyFile.exists()) {
191+
val properties = Properties().apply { load(propertyFile.inputStream()) }
192+
193+
var count = 0
194+
for (entry in properties.entries) {
195+
if (entry.key is String) {
196+
project.setProperty(entry.key as String, entry.value)
197+
count++
198+
}
199+
}
200+
201+
project.logger.lifecycle("Successfully loaded $count properties")
202+
} else {
203+
project.logger.warn("The property file ${propertyFile.getName()} could not be loaded. It does not exist.")
157204
}
158205
}
159206
}

0 commit comments

Comments
 (0)