Skip to content

Commit

Permalink
javalibs now on scripted pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
Joaquimmnetto committed Jun 16, 2022
1 parent 8303ad7 commit 2934f82
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 163 deletions.
61 changes: 35 additions & 26 deletions src/net/wooga/jenkins/pipeline/check/NodeCreator.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ class NodeCreator {
Closure mainCls, Closure catchCls, Closure finallyCls) {
return {
jenkins.node(nodeLabels) {
jenkins.withEnv(environment) {
runSteps(mainCls, catchCls, finallyCls)
}
runSteps(environment, [], mainCls, catchCls, finallyCls)
}
}
}
Expand All @@ -27,44 +25,55 @@ class NodeCreator {
return node(paramsMap)
}

Closure node(Map params = [
label : null,
when : { -> true },
steps : {},
onError: {},
after : { maybeException -> }
]) {
node(params.label as String,
params.when as Closure<Boolean>,
params.steps as Closure,
params.onError as Closure,
params.after as Closure)
Closure node(Map params) {
def fullParams = [
label : null,
when : { -> true },
environment: [:],
credentials: [],
steps : {},
onError : {ex -> throw ex},
after : { maybeException -> }
]
fullParams.putAll(params)
node(fullParams.label as String,
fullParams.environment as Map<String, String>,
fullParams.credentials as List,
fullParams.when as Closure<Boolean>,
fullParams.steps as Closure,
fullParams.onError as Closure,
fullParams.after as Closure)
}

Closure node(String label, Closure<Boolean> when = { -> true },
Closure node(String label, Map<String, String> environment, List<Object> credentials, Closure<Boolean> when = { -> true },
Closure steps, Closure onError, Closure after) {
return {
if (when()) {
def envList = environment.collect {"${it.key}=${it.value}".toString() }
if (label) {
jenkins.node(label) {
runSteps(steps, onError, after)
runSteps(envList, credentials, steps, onError, after)
}
} else {
runSteps(steps, onError, after)
runSteps(envList, credentials, steps, onError, after)
}
}
}
}

private static def runSteps(Closure steps, Closure onError, Closure after) {
private def runSteps(List<String> environment, List credentials, Closure steps, Closure onError, Closure after) {
def maybeException = null as Exception
try {
steps.call()
} catch (Exception e) {
maybeException = e
onError?.call(e)
} finally {
after?.call(maybeException)
jenkins.withEnv(environment) {
jenkins.withCredentials(credentials) {
try {
steps.call()
} catch (Exception e) {
maybeException = e
onError?.call(e)
} finally {
after?.call(maybeException)
}
}
}
}

Expand Down
30 changes: 0 additions & 30 deletions src/net/wooga/jenkins/pipeline/config/JavaConfig.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -62,34 +62,4 @@ class JavaConfig implements PipelineConfig {
PipelineTools getPipelineTools() {
return PipelineTools.fromConfig(baseConfig.jenkins, this)
}

boolean equals(o) {
if (this.is(o)) return true
if (getClass() != o.class) return false

JavaConfig that = (JavaConfig) o

if (checkArgs != that.checkArgs) return false
if (conventions != that.conventions) return false
if (dockerArgs != that.dockerArgs) return false
if (gradleArgs != that.gradleArgs) return false
if (jenkins != that.jenkins) return false
if (metadata != that.metadata) return false
if (!Arrays.equals(platforms, that.platforms)) return false

return true
}

int hashCode() {
int result
result = (jenkins != null ? jenkins.hashCode() : 0)
result = 31 * result + (conventions != null ? conventions.hashCode() : 0)
result = 31 * result + (platforms != null ? Arrays.hashCode(platforms) : 0)
result = 31 * result + (metadata != null ? metadata.hashCode() : 0)
result = 31 * result + (gradleArgs != null ? gradleArgs.hashCode() : 0)
result = 31 * result + (dockerArgs != null ? dockerArgs.hashCode() : 0)
result = 31 * result + (checkArgs != null ? checkArgs.hashCode() : 0)
return result
}

}
9 changes: 3 additions & 6 deletions test/groovy/scripts/BuildGradlePluginSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,15 @@ class BuildGradlePluginSpec extends DeclarativeJenkinsSpec {
def buildGradlePlugin = loadSandboxedScript(SCRIPT_PATH) {
params.RELEASE_TYPE = "not-snapshot"
params.RELEASE_SCOPE = "any"
env.GRGIT_USR = "usr"
env.GRGIT_PSW = "pwd"
}

when: "running buildGradlePlugin pipeline"
inSandbox { buildGradlePlugin() }

then: "sets up GRGIT environment"
def env = buildGradlePlugin.binding.env
env["GRGIT"] == credentials['github_access']
env["GRGIT_USER"] == "usr" //"${GRGIT_USR}"
env["GRGIT_PASS"] == "pwd" //"${GRGIT_PSW}"
def env = usedEnvironments.last() as Map<String, String>
env["GRGIT_USER"] == "usr"
env["GRGIT_PASS"] == "pwd"
and: "sets up github environment"
env["GITHUB_LOGIN"] == "usr" //"${GRGIT_USR}"
env["GITHUB_PASSWORD"] == "pwd" //"${GRGIT_PSW}"
Expand Down
16 changes: 6 additions & 10 deletions test/groovy/scripts/BuildJavaLibraryOSSRHSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,23 @@ class BuildJavaLibraryOSSRHSpec extends DeclarativeJenkinsSpec {
currentBuild["result"] = null
params.RELEASE_TYPE = "not-snapshot"
params.RELEASE_SCOPE = "any"
env.GRGIT_USR = "usr"
env.GRGIT_PSW = "pwd"
}

when: "running buildJavaLibrary pipeline"
inSandbox { buildJavaLibrary() }

then: "sets up GRGIT environment"
def env = buildJavaLibrary.binding.env
env["GRGIT"] == credentials['github_access']
def env = usedEnvironments.first()
env["GRGIT_USER"] == "usr" //"${GRGIT_USR}"
env["GRGIT_PASS"] == "pwd" //"${GRGIT_PSW}"
and: "sets up github environment"
env["GITHUB_LOGIN"] == "usr" //"${GRGIT_USR}"
env["GITHUB_PASSWORD"] == "pwd" //"${GRGIT_PSW}"
and: "sets up ossrh keys"
def publishEnv = usedEnvironments.last()
publishEnv["OSSRH_USERNAME"] == "user"
publishEnv["OSSRH_PASSWORD"] == "key"
publishEnv["OSSRH_SIGNING_KEY"] == "signing_key"
publishEnv["OSSRH_SIGNING_KEY_ID"] == "signing_key_id"
publishEnv["OSSRH_SIGNING_PASSPHRASE"] == "signing_passwd"
env["OSSRH_USERNAME"] == "user"
env["OSSRH_PASSWORD"] == "key"
env["OSSRH_SIGNING_KEY"] == "signing_key"
env["OSSRH_SIGNING_KEY_ID"] == "signing_key_id"
env["OSSRH_SIGNING_PASSPHRASE"] == "signing_passwd"
}
}
5 changes: 1 addition & 4 deletions test/groovy/scripts/BuildJavaLibrarySpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,13 @@ class BuildJavaLibrarySpec extends DeclarativeJenkinsSpec {
currentBuild["result"] = null
params.RELEASE_TYPE = "not-snapshot"
params.RELEASE_SCOPE = "any"
env.GRGIT_USR = "usr"
env.GRGIT_PSW = "pwd"
}

when: "running buildJavaLibrary pipeline"
inSandbox { buildJavaLibrary() }

then: "sets up GRGIT environment"
def env = buildJavaLibrary.binding.env
env["GRGIT"] == credentials['github_access']
def env = usedEnvironments.last()
env["GRGIT_USER"] == "usr" //"${GRGIT_USR}"
env["GRGIT_PASS"] == "pwd" //"${GRGIT_PSW}"
and: "sets up github environment"
Expand Down
16 changes: 6 additions & 10 deletions test/groovy/scripts/BuildPrivateJavaLibrarySpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,23 @@ class BuildPrivateJavaLibrarySpec extends DeclarativeJenkinsSpec {
and: "build plugin with publish parameters"
def buildJavaLibrary = loadSandboxedScript(SCRIPT_PATH) {
currentBuild["result"] = null
env.GRGIT_USR = "usr"
env.GRGIT_PSW = "pwd"
}

when: "running buildJavaLibrary pipeline"
inSandbox { buildJavaLibrary() }

then: "sets up GRGIT environment"
def env = buildJavaLibrary.binding.env
env["GRGIT"] == credentials['github_access'] //credentials("github_access")
def env = usedEnvironments.get(usedEnvironments.size()-2)
env["GRGIT_USER"] == "usr" //"${GRGIT_USR}"
env["GRGIT_PASS"] == "pwd" //"${GRGIT_PSW}"
and: "sets up github environment"
env["GITHUB_LOGIN"] == "usr" //"${GRGIT_USR}"
env["GITHUB_PASSWORD"] == "pwd" //"${GRGIT_PSW}"
and: "sets up ossrh keys"
def publishEnv = usedEnvironments.last()
publishEnv["ARTIFACTORY_USER"] == "user"
publishEnv["ARTIFACTORY_PASS"] == "key"
publishEnv["OSSRH_SIGNING_KEY"] == "signing_key"
publishEnv["OSSRH_SIGNING_KEY_ID"] == "signing_key_id"
publishEnv["OSSRH_SIGNING_PASSPHRASE"] == "signing_passwd"
env["ARTIFACTORY_USER"] == "user"
env["ARTIFACTORY_PASS"] == "key"
env["OSSRH_SIGNING_KEY"] == "signing_key"
env["OSSRH_SIGNING_KEY_ID"] == "signing_key_id"
env["OSSRH_SIGNING_PASSPHRASE"] == "signing_passwd"
}
}
1 change: 1 addition & 0 deletions test/groovy/tools/DeclarativeJenkinsSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ abstract class DeclarativeJenkinsSpec extends Specification {
varBindingOps(binding.variables)
if(reloadSideScripts) {
registerSideScript("vars/javaLibs.groovy", binding)
registerSideScript("vars/declarativePipelineTemplate.groovy", binding)
}
return helper.loadSandboxedScript(path, binding)
}
Expand Down
4 changes: 3 additions & 1 deletion test/groovy/tools/FakeEnvironment.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class FakeEnvironment {
try {
cls()
} finally {
usedEnvironments.add(deepCopy(binding.env as Map))
if((binding.env as Map).size() > 0) {
usedEnvironments.add(deepCopy(binding.env as Map))
}
env.each {
binding.env.remove(it.key)
binding.variables.remove(it.key)
Expand Down
1 change: 1 addition & 0 deletions vars/buildGradlePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ def call(Map configMap = [:], Closure stagesConfigCls = {it -> }) {
publisher.gradlePlugin('gradle.publish.key', 'gradle.publish.secret')
}
}
stagesConfigCls(stages)
}
}
58 changes: 58 additions & 0 deletions vars/declarativePipelineTemplate.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env groovy
import net.wooga.jenkins.pipeline.config.PipelineConfig

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// Step buildGradlePlugin //
// //
// //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

def call(PipelineConfig config, List<Closure> stageFactoriesList) {
pipeline {
agent none

options {
buildDiscarder(logRotator(artifactNumToKeepStr: '40'))
}

parameters {
choice(choices: ["snapshot", "rc", "final"], description: 'Choose the distribution type', name: 'RELEASE_TYPE')
choice(choices: ["", "patch", "minor", "major"], description: 'Choose the change scope', name: 'RELEASE_SCOPE')
choice(choices: ["", "quiet", "info", "warn", "debug"], description: 'Choose the log level', name: 'LOG_LEVEL')
booleanParam(defaultValue: false, description: 'Whether to log truncated stacktraces', name: 'STACK_TRACE')
booleanParam(defaultValue: false, description: 'Whether to refresh dependencies', name: 'REFRESH_DEPENDENCIES')
booleanParam(defaultValue: false, description: 'Whether to clear workspaces after build', name: 'CLEAR_WS')
}

stages {
stage('Preparation') {
agent any
steps {
sendSlackNotification "STARTED", true
script {
for(Closure stageFactory: stageFactoriesList) {
def stage = stageFactory() as Closure
stage.call()
}
}
}
post {
cleanup {
script {
if (config.mainPlatform.clearWs) {
cleanWs()
}
}
}
}
}
}

post {
always {
sendSlackNotification currentBuild.result, true
}
}
}
}
Loading

0 comments on commit 2934f82

Please sign in to comment.