Skip to content

Commit

Permalink
[JENKINS-41050] First work on checkout always being on node
Browse files Browse the repository at this point in the history
i.e., not in the container. Need to see how this works in practice and
figure out how to test it.
  • Loading branch information
abayer committed Jan 19, 2017
1 parent 3222d5a commit 2f7fb17
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 30 deletions.
Expand Up @@ -33,5 +33,13 @@
* @author Andrew Bayer
*/
public abstract class DeclarativeAgent<A extends DeclarativeAgent<A>> extends WithScriptDescribable<A> implements ExtensionPoint {
protected Object context;

public void setContext(Object context) {
this.context = context;
}

public Object getContext() {
return context;
}
}
Expand Up @@ -47,7 +47,7 @@ public class Agent extends MappedClosure<Object,Agent> implements Serializable {
*
* @return The instantiated declarative agent or null if not found.
*/
public DeclarativeAgent getDeclarativeAgent() {
public DeclarativeAgent getDeclarativeAgent(Object context) {
DeclarativeAgentDescriptor foundDescriptor = DeclarativeAgentDescriptor.all().find { d ->
getMap().containsKey(d.getName())
}
Expand All @@ -61,14 +61,16 @@ public class Agent extends MappedClosure<Object,Agent> implements Serializable {
argMap.put(UninstantiatedDescribable.ANONYMOUS_KEY, val)
}

return DeclarativeAgentDescriptor.instanceForDescriptor(foundDescriptor, argMap)
DeclarativeAgent a = DeclarativeAgentDescriptor.instanceForDescriptor(foundDescriptor, argMap)
a.setContext(context)
return a
} else {
return null
}
}

public boolean hasAgent() {
DeclarativeAgent a = getDeclarativeAgent()
DeclarativeAgent a = getDeclarativeAgent(null)
return a != null && !None.class.isInstance(a)
}

Expand Down
Expand Up @@ -74,27 +74,17 @@ public class ModelInterpreter implements Serializable {
// Stage execution and post-build actions run in try/catch blocks, so we still run post-build actions
// even if the build fails.
// We save the caught error, if any, for throwing at the end of the build.
inDeclarativeAgent(root.agent) {
inDeclarativeAgent(root, root.agent) {
withCredentialsBlock(root.getEnvCredentials()) {
toolsBlock(root.agent, root.tools) {
// If we have an agent and script.scm isn't null, run checkout scm
if (root.agent.hasAgent()
&& Utils.hasScmContext(script)
&& !((SkipDefaultCheckout)root.options?.options?.get("skipDefaultCheckout"))?.isSkipDefaultCheckout()) {
script.stage(SyntheticStageNames.checkout()) {
Utils.markSyntheticStage(SyntheticStageNames.checkout(), Utils.getSyntheticStageMetadata().pre)
script.checkout script.scm
}
}

for (int i = 0; i < root.stages.getStages().size(); i++) {
Stage thisStage = root.stages.getStages().get(i)
try {
script.stage(thisStage.name) {
if (firstError == null) {
withEnvBlock(thisStage.getEnvVars()) {
if (evaluateWhen(thisStage.when)) {
inDeclarativeAgent(thisStage.agent) {
inDeclarativeAgent(thisStage, thisStage.agent) {
withCredentialsBlock(thisStage.getEnvCredentials()) {
toolsBlock(thisStage.agent ?: root.agent, thisStage.tools) {
// Execute the actual stage and potential post-stage actions
Expand Down Expand Up @@ -295,17 +285,18 @@ public class ModelInterpreter implements Serializable {
/**
* Executes the given closure inside a declarative agent block, if appropriate.
*
* @param context Either a stage or root object, the context we're running in.
* @param agent The agent context we're running in
* @param body The closure to execute
* @return The return of the resulting executed closure
*/
def inDeclarativeAgent(Agent agent, Closure body) {
def inDeclarativeAgent(Object context, Agent agent, Closure body) {
if (agent == null) {
return {
body.call()
}.call()
} else {
return agent.getDeclarativeAgent().getScript(script).run {
return agent.getDeclarativeAgent(context).getScript(script).run {
body.call()
}.call()
}
Expand Down
Expand Up @@ -25,7 +25,6 @@

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

import hudson.model.Result
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript
import org.jenkinsci.plugins.workflow.cps.CpsScript

Expand All @@ -37,15 +36,11 @@ public class AnyScript extends DeclarativeAgentScript<Any> {

@Override
public Closure run(Closure body) {
return {
try {
script.node {
body.call()
}
} catch (Exception e) {
script.getProperty("currentBuild").result = Result.FAILURE
throw e
}
Label l = (Label) Label.DescriptorImpl.instanceForName("label", [label: null])
l.setContext(describable.context)
LabelScript labelScript = (LabelScript) l.getScript(script)
return labelScript.run {
body.call()
}
}
}
Expand Up @@ -75,9 +75,6 @@ public class DockerPipelineFromDockerfileScript extends AbstractDockerPipelineSc

private Closure buildImage() {
return {
if (!script.fileExists(describable.getDockerfileAsString())) {
script.checkout script.scm
}
try {
def hash = Utils.stringToSHA1(script.readFile(describable.getDockerfileAsString()))
def imgName = "${hash}"
Expand Down
Expand Up @@ -26,8 +26,12 @@
package org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl

import hudson.model.Result
import org.jenkinsci.plugins.pipeline.modeldefinition.SyntheticStageNames
import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgent
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript
import org.jenkinsci.plugins.pipeline.modeldefinition.model.Root
import org.jenkinsci.plugins.pipeline.modeldefinition.options.impl.SkipDefaultCheckout
import org.jenkinsci.plugins.workflow.cps.CpsScript

public class LabelScript extends DeclarativeAgentScript<Label> {
Expand All @@ -41,6 +45,16 @@ public class LabelScript extends DeclarativeAgentScript<Label> {
return {
try {
script.node(describable?.label) {
if (describable.context instanceof Root) {
Root root = (Root)describable.context
SkipDefaultCheckout skip = (SkipDefaultCheckout)root.options?.options?.get("skipDefaultCheckout")
if (skip?.isSkipDefaultCheckout() && Utils.hasScmContext(script)) {
script.stage(SyntheticStageNames.checkout()) {
Utils.markSyntheticStage(SyntheticStageNames.checkout(), Utils.getSyntheticStageMetadata().pre)
script.checkout script.scm
}
}
}
body.call()
}
} catch (Exception e) {
Expand Down

0 comments on commit 2f7fb17

Please sign in to comment.