Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,19 @@
package org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl

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

class AnyScript extends DeclarativeAgentScript<Any> {
class AnyScript extends DeclarativeAgentScript2<Any> {

AnyScript(CpsScript s, Any a) {
super(s, a)
}

@Override
Closure run(Closure body) {
return {
script.node {
CheckoutScript.doCheckout(script, describable, null, body).call()
}
void run(Closure body) {
script.node {
CheckoutScript.doCheckout2(script, describable, null, body)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,27 @@ package org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl


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

class LabelScript extends DeclarativeAgentScript<Label> {
class LabelScript extends DeclarativeAgentScript2<Label> {

LabelScript(CpsScript s, Label a) {
super(s, a)
}

@Override
Closure run(Closure body) {
Closure run = {
script.node(describable?.label) {
CheckoutScript.doCheckout(script, describable, describable.customWorkspace, body).call()
}
}
void run(Closure body) {
if (describable.retries > 1) {
return {
script.retry(count: describable.retries, conditions: [script.agent(), script.nonresumable()]) {
run.call()
script.retry(count: describable.retries, conditions: [script.agent(), script.nonresumable()]) {
script.node(describable?.label) {
CheckoutScript.doCheckout2(script, describable, describable.customWorkspace, body)
}
}
} else {
run
script.node(describable?.label) {
CheckoutScript.doCheckout2(script, describable, describable.customWorkspace, body)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@

package org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl

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

class NoneScript extends DeclarativeAgentScript<None> {
class NoneScript extends DeclarativeAgentScript2<None> {

NoneScript(CpsScript s, None a) {
super(s, a)
}

@Override
Closure run(Closure body) {
return body
void run(Closure body) {
body.call()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static void setUpAgent() throws Exception {

@Issue("JENKINS-47363")
// Give this a longer timeout
@Test(timeout=5 * 60 * 1000)
@Test(timeout=10 * 60 * 1000)
Copy link
Member Author

Choose a reason for hiding this comment

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

And now it is passing in 98s. 🤷

public void stages300() throws Exception {
assumeFalse("can exceed even 5m timeout", Functions.isWindows());
RuntimeASTTransformer.SCRIPT_SPLITTING_TRANSFORMATION = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,23 @@

package org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl

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

class LabelAndOtherFieldAgentScript extends DeclarativeAgentScript<LabelAndOtherFieldAgent> {
class LabelAndOtherFieldAgentScript extends DeclarativeAgentScript2<LabelAndOtherFieldAgent> {

LabelAndOtherFieldAgentScript(CpsScript s, LabelAndOtherFieldAgent a) {
super(s, a)
}

@Override
Closure run(Closure body) {
void run(Closure body) {
script.echo "Running in labelAndOtherField with otherField = ${describable.getOtherField()}"
script.echo "And nested: ${describable.getNested()}"
Label l = (Label) Label.DescriptorImpl.instanceForName("label", [label: describable.label])
l.inStage = describable.inStage
l.doCheckout = describable.doCheckout
LabelScript labelScript = (LabelScript) l.getScript(script)
return labelScript.run {
body.call()
}
labelScript.run(body)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Map;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import java.util.logging.Logger;
import org.jenkinsci.plugins.pipeline.modeldefinition.options.DeclarativeOption;
import org.jenkinsci.plugins.pipeline.modeldefinition.withscript.WithScriptDescribable;
import org.jenkinsci.plugins.pipeline.modeldefinition.withscript.WithScriptScript;
Expand All @@ -41,6 +42,9 @@
* @author Andrew Bayer
*/
public abstract class DeclarativeAgent<A extends DeclarativeAgent<A>> extends WithScriptDescribable<A> implements ExtensionPoint {

private static final Logger LOGGER = Logger.getLogger(DeclarativeAgent.class.getName());

protected boolean inStage;
protected boolean doCheckout;
protected String subdirectory;
Expand All @@ -53,7 +57,11 @@ public WithScriptScript getScript(CpsScript cpsScript) throws Exception {

cpsScript.getClass().getClassLoader().loadClass("org.jenkinsci.plugins.pipeline.modeldefinition.agent.CheckoutScript");

return super.getScript(cpsScript);
var script = super.getScript(cpsScript);
if (script instanceof DeclarativeAgentScript) {
LOGGER.warning(() -> script.getClass().getName() + " should implement " + DeclarativeAgentScript2.class.getName());
}
return script;
}

public void setInStage(boolean inStage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.jenkinsci.plugins.pipeline.modeldefinition.withscript.WithScriptScript;
import org.jenkinsci.plugins.workflow.cps.CpsScript;


@Deprecated
Copy link
Member Author

Choose a reason for hiding this comment

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

Implemented in a few other plugins:

  • kubernetes
  • docker
  • docker-workflow
  • amazon-ecs

public abstract class DeclarativeAgentScript<A extends DeclarativeAgent<A>> extends WithScriptScript<A> {

public DeclarativeAgentScript(CpsScript s, A a) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.pipeline.modeldefinition.agent;

import groovy.lang.Closure;
import org.jenkinsci.plugins.pipeline.modeldefinition.withscript.WithScriptScript;
import org.jenkinsci.plugins.workflow.cps.CpsScript;

public abstract class DeclarativeAgentScript2<A extends DeclarativeAgent<A>> extends WithScriptScript<A> {

public DeclarativeAgentScript2(CpsScript s, A a) {
super(s, a);
}

public abstract void run(Closure body);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,18 @@

/**
* A type of {@code agent} option that supports automatic retries.
* Usage from your {@link DeclarativeAgentScript#run} would look something like:
* Usage from your {@link DeclarativeAgentScript2#run} would look something like:
* <pre>{@code
* Closure run = {
* script.node {
* CheckoutScript.doCheckout(script, describable, null, body).call()
* }
* }
* if (describable.retries > 1) {
* return {
* script.retry(count: describable.retries, conditions: [script.agent(), script.nonresumable()]) {
* run.call()
* script.retry(count: describable.retries, conditions: [script.agent(), script.nonresumable()]) {
* script.node {
* CheckoutScript.doCheckout2(script, describable, null, body)
* }
* }
* } else {
* run
* script.node {
* CheckoutScript.doCheckout2(script, describable, null, body)
* }
* }}</pre>
*/
public abstract class RetryableDeclarativeAgent<A extends RetryableDeclarativeAgent<A>> extends DeclarativeAgent<A> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,41 @@ import org.jenkinsci.plugins.workflow.cps.CpsScript

class CheckoutScript implements Serializable {

@Deprecated
static Closure doCheckout(CpsScript script, DeclarativeAgent agent, String customWorkspace = null, Closure body) {
return {
if (customWorkspace) {
script.ws(customWorkspace) {
checkoutAndRun(script, agent, body).call()
}
} else {
checkoutAndRun(script, agent, body).call()
doCheckout2(script, agent, customWorkspace, body)
}
}

static void doCheckout2(CpsScript script, DeclarativeAgent agent, String customWorkspace = null, Closure body) {
if (customWorkspace) {
script.ws(customWorkspace) {
checkoutAndRun(script, agent, body)
}
} else {
checkoutAndRun(script, agent, body)
}
}

private static Closure checkoutAndRun(CpsScript script, DeclarativeAgent agent, Closure body) {
return {
def checkoutMap = [:]

if (agent.isDoCheckout() && agent.hasScmContext(script)) {
String subDir = agent.subdirectory
if (subDir != null && subDir != "") {
script.dir(subDir) {
checkoutMap.putAll(performCheckout(script, agent))
}
} else {
private static void checkoutAndRun(CpsScript script, DeclarativeAgent agent, Closure body) {
def checkoutMap = [:]

if (agent.isDoCheckout() && agent.hasScmContext(script)) {
String subDir = agent.subdirectory
if (subDir != null && subDir != "") {
script.dir(subDir) {
checkoutMap.putAll(performCheckout(script, agent))
}
}
if (checkoutMap) {
script.withEnv(checkoutMap.collect { k, v -> "${k}=${v}" }) {
body.call()
}
} else {
body.call()
checkoutMap.putAll(performCheckout(script, agent))
}
}
if (checkoutMap) {
script.withEnv(checkoutMap.collect { k, v -> "${k}=${v}" }, body)
} else {
body.call()
}
}

private static Map performCheckout(CpsScript script, DeclarativeAgent agent) {
Expand Down
Loading