Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid excessive Closures #1653

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Avoid excessive Closures
jglick committed Feb 25, 2025

Verified

This commit was signed with the committer’s verified signature.
jglick Jesse Glick
commit dc610c6ca9aca3fec21dce0973485829251d4cb0
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@
<jenkins.host.address />
<slaveAgentPort />
<jenkins.baseline>2.479</jenkins.baseline>
<jenkins.version>${jenkins.baseline}.1</jenkins.version>
<jenkins.version>${jenkins.baseline}.3</jenkins.version>
<no-test-jar>false</no-test-jar>
<useBeta>true</useBeta>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
@@ -58,7 +58,7 @@
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-${jenkins.baseline}.x</artifactId>
<version>4051.v78dce3ce8b_d6</version>
<version>4228.v0a_71308d905b_</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Original file line number Diff line number Diff line change
@@ -24,62 +24,58 @@
package org.csanchez.jenkins.plugins.kubernetes.pipeline

import org.jenkinsci.plugins.pipeline.modeldefinition.agent.CheckoutScript
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript2
import org.jenkinsci.plugins.workflow.cps.CpsScript

public class KubernetesDeclarativeAgentScript extends DeclarativeAgentScript<KubernetesDeclarativeAgent> {
public class KubernetesDeclarativeAgentScript extends DeclarativeAgentScript2<KubernetesDeclarativeAgent> {
public KubernetesDeclarativeAgentScript(CpsScript s, KubernetesDeclarativeAgent a) {
super(s, a)
}

@Override
public Closure run(Closure body) {
return {
if ((describable.getYamlFile() != null) && (describable.hasScmContext(script))) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(ignore WS)

describable.setYaml(script.readTrusted(describable.getYamlFile()))
}
if (describable.getInheritFrom() == null) {
// Do not implicitly inherit from parent template context for declarative Kubernetes agent declaration
describable.setInheritFrom("")
}
if (describable.labelExpression != null) {
script.echo '[WARNING] label option is deprecated. To use a static pod template, use the \'inheritFrom\' option.'
}
if (describable.containerTemplate != null) {
script.echo '[WARNING] containerTemplate option is deprecated, use yaml syntax to define containers.'
}
script.podTemplate(describable.asArgs) {
Closure run = {
script.node(describable.labelExpression ?: script.POD_LABEL) {
CheckoutScript.doCheckout(script, describable, describable.customWorkspace) {
// what container to use for the main body
def container = describable.defaultContainer ?: 'jnlp'
public void run(Closure body) {
if ((describable.getYamlFile() != null) && (describable.hasScmContext(script))) {
describable.setYaml(script.readTrusted(describable.getYamlFile()))
}
if (describable.getInheritFrom() == null) {
// Do not implicitly inherit from parent template context for declarative Kubernetes agent declaration
describable.setInheritFrom("")
}
if (describable.labelExpression != null) {
script.echo '[WARNING] label option is deprecated. To use a static pod template, use the \'inheritFrom\' option.'
}
if (describable.containerTemplate != null) {
script.echo '[WARNING] containerTemplate option is deprecated, use yaml syntax to define containers.'
}
script.podTemplate(describable.asArgs) {
Closure run = {
script.node(describable.labelExpression ?: script.POD_LABEL) {
CheckoutScript.doCheckout2(script, describable, describable.customWorkspace) {
// what container to use for the main body
def container = describable.defaultContainer ?: 'jnlp'

if (describable.containerTemplate != null) {
// run inside the container declared for backwards compatibility
container = describable.containerTemplate.asArgs
}
if (describable.containerTemplate != null) {
// run inside the container declared for backwards compatibility
container = describable.containerTemplate.asArgs
}

// call the main body
if (container == 'jnlp') {
// If default container is not changed by the pipeline user,
// do not enclose the body with a `container` statement.
body.call()
} else {
script.container(container) {
body.call()
}
}
}.call()
// call the main body
if (container == 'jnlp') {
// If default container is not changed by the pipeline user,
// do not enclose the body with a `container` statement.
body.call()
} else {
script.container(container, body)
}
}
}
if (describable.retries > 1) {
script.retry(count: describable.retries, conditions: [script.kubernetesAgent(), script.nonresumable()]) {
run.call()
}
} else {
}
if (describable.retries > 1) {
script.retry(count: describable.retries, conditions: [script.kubernetesAgent(), script.nonresumable()]) {
run.call()
}
} else {
run.call()
}
}
}