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

Ensure that scm env variables are set for declarative pipeline. #286

Closed
Closed
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/.idea/
*.iml
nbactions.xml
/.vscode/
32 changes: 32 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,38 @@
</dependency>

<!-- for testing -->
<dependency> <!-- KubernetesDeclarativeAgentTest -->
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>2.6.0</version>
<classifier>tests</classifier>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency> <!-- KubernetesDeclarativeAgentTest -->
Copy link
Contributor

Choose a reason for hiding this comment

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

duplicated

<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>2.6.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency> <!-- KubernetesDeclarativeAgentTest -->
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-scm-step</artifactId>
<version>${jenkins-workflow-scm-step.version}</version>
<scope>test</scope>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,27 @@ public class KubernetesDeclarativeAgentScript extends DeclarativeAgentScript<Kub
try {
script.podTemplate(describable.asArgs) {
script.node(describable.label) {
def checkoutMap = [:]
if (describable.isDoCheckout() && describable.hasScmContext(script)) {
if (!describable.inStage) {
script.stage(SyntheticStageNames.checkout()) {
script.checkout script.scm
String subDir = describable.subdirectory
if (subDir != null && subDir != "") {
script.dir(subDir) {
checkoutMap.putAll(performCheckout(script, describable))
}
} else {
// No stage when we're in a nested stage already
script.checkout script.scm
checkoutMap.putAll(performCheckout(script, describable))
}
}
script.container(describable.containerTemplate.asArgs) {
body.call()
if (checkoutMap) {
script.withEnv(checkoutMap.collect { k, v -> "${k}=${v}" }) {
script.container(describable.containerTemplate.name) {
body.call()
}
}
} else {
script.container(describable.containerTemplate.asArgs) {
body.call()
}
}
}
}
Expand All @@ -61,4 +70,18 @@ public class KubernetesDeclarativeAgentScript extends DeclarativeAgentScript<Kub
}
}
}

private static Map performCheckout(CpsScript script, KubernetesDeclarativeAgent agent) {
def checkoutMap = [:]
if (!agent.inStage) {
script.stage(SyntheticStageNames.checkout()) {
checkoutMap.putAll(script.checkout(script.scm) ?: [:])
}
} else {
// No stage when we're in a nested stage already
checkoutMap.putAll(script.checkout(script.scm) ?: [:])
}

return checkoutMap
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;


import org.apache.commons.compress.utils.IOUtils;
import org.csanchez.jenkins.plugins.kubernetes.ContainerEnvVar;
import org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate;
Expand All @@ -47,6 +48,7 @@
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.JenkinsRuleNonLocalhost;
import org.jvnet.hudson.test.LoggerRule;
import jenkins.plugins.git.GitSampleRepoRule;

import com.google.common.collect.ImmutableMap;

Expand Down Expand Up @@ -77,6 +79,15 @@ public class AbstractKubernetesPipelineTest {
@Rule
public LoggerRule logs = new LoggerRule().record(Logger.getLogger(KubernetesCloud.class.getPackage().getName()),
Level.ALL);
@Rule
public GitSampleRepoRule sampleRepo = new GitSampleRepoRule();

protected void prepRepoWithJenkinsfile(String pipelineName) throws Exception {
sampleRepo.init();
sampleRepo.write("Jenkinsfile", loadPipelineScript(pipelineName));
sampleRepo.git("add", "Jenkinsfile");
sampleRepo.git("commit", "--message=files");
}

@BeforeClass
public static void isKubernetesConfigured() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import static org.junit.Assert.*;

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition;
import jenkins.plugins.git.GitStep;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Test;
Expand All @@ -47,4 +49,16 @@ public void declarative() throws Exception {
r.assertLogContains("OUTSIDE_CONTAINER_ENV_VAR = " + CONTAINER_ENV_VAR_VALUE + "\n", b);
}

@Test
public void declarativeWithSCMEnvVars() throws Exception {
prepRepoWithJenkinsfile("declarativeWithSCMEnvVars.groovy");
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "job with dir");
p.setDefinition(new CpsScmFlowDefinition(new GitStep(sampleRepo.toString()).createSCM(), "Jenkinsfile"));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatusSuccess((r.waitForCompletion(b)));
r.assertLogNotContains("GIT_COMMIT is null", b);
Copy link
Contributor

Choose a reason for hiding this comment

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

you should also check that log contains GIT_COMMIT is

Copy link
Author

Choose a reason for hiding this comment

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

Will do.

r.assertLogContains("GIT_COMMIT is ", b);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pipeline {
agent any
stages {
stage("foo") {
steps {
echo "GIT_COMMIT is ${env.GIT_COMMIT}"
}
}
}
}