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

[FIXED JENKINS-52259] Add yamlFile option for Declarative agent #355

Merged
merged 10 commits into from
Jul 11, 2018
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ See the full list of issues at [JIRA](https://issues.jenkins-ci.org/issues/?filt

Preserve resources other than CPU and memory

1.10.0
-----

* Add `yamlFile` option for Declarative agent to read yaml definition from a different file [#355](https://github.com/jenkinsci/kubernetes-plugin/pull/355) [JENKINS-52259](https://issues.jenkins-ci.org/browse/JENKINS-52259)

1.9.3
-----

Expand Down
2 changes: 1 addition & 1 deletion Jenkinsfile-kubernetes
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def jvmOptions = '-Xmx300M'
timestamps {

podTemplate(serviceAccount: 'jenkins', label: label, containers: [
containerTemplate(name: 'maven', image: 'maven:3.5.0-jdk-8-alpine', ttyEnabled: true, command: 'cat',
containerTemplate(name: 'maven', image: 'maven:3.5.4-jdk-8', ttyEnabled: true, command: 'cat',
resourceRequestCpu: '100m',
resourceLimitMemory: '1200Mi')
], envVars: [
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,23 @@ spec:
}
```

or using `yamlFile` to keep the pod template in a separate `KubernetesPod.yaml` file

```
pipeline {
agent {
kubernetes {
label 'mypod'
defaultContainer 'jnlp'
yamlFile 'KubernetesPod.yaml'
}
}
stages {
...
}
}
```

Note that it was previously possible to define `containerTemplate` but that has been deprecated in favor of the yaml format.

```groovy
Expand Down
24 changes: 24 additions & 0 deletions examples/declarative_from_yaml_file/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pipeline {
agent {
kubernetes {
label 'mypod'
yamlFile 'examples/declarative_from_yaml_file/KubernetesPod.yaml'
}
}
stages {
stage('Run maven') {
steps {
sh 'set'
sh "echo OUTSIDE_CONTAINER_ENV_VAR = ${CONTAINER_ENV_VAR}"
container('maven') {
sh 'echo MAVEN_CONTAINER_ENV_VAR = ${CONTAINER_ENV_VAR}'
sh 'mvn -version'
}
container('busybox') {
sh 'echo BUSYBOX_CONTAINER_ENV_VAR = ${CONTAINER_ENV_VAR}'
sh '/bin/busybox'
}
}
}
}
}
25 changes: 25 additions & 0 deletions examples/declarative_from_yaml_file/KubernetesPod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
metadata:
labels:
some-label: some-label-value
spec:
containers:
- name: jnlp
env:
- name: CONTAINER_ENV_VAR
value: jnlp
- name: maven
image: maven:3.3.9-jdk-8-alpine
command:
- cat
tty: true
env:
- name: CONTAINER_ENV_VAR
value: maven
- name: busybox
image: busybox
command:
- cat
tty: true
env:
- name: CONTAINER_ENV_VAR
value: busybox
50 changes: 46 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<version>2.0.7</version>
<version>2.2.6</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -178,6 +178,33 @@
<version>2.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>3.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>3.7.0</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<version>2.2.6</version>
<scope>test</scope>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-scm-step</artifactId>
<version>2.4</version>
<scope>test</scope>
<classifier>tests</classifier>
</dependency>

<!-- for ContainerExecDecoratorPipelineTest -->
<dependency>
Expand Down Expand Up @@ -205,7 +232,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<version>2.1.11</version>
<version>2.1.14</version>
</dependency>

<!-- just to fix enforcer RequireUpperBoundDeps -->
Expand All @@ -217,18 +244,33 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.6</version>
<version>1.10</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ssh-credentials</artifactId>
<version>1.12</version>
<version>1.13</version>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci</groupId>
<artifactId>annotation-indexer</artifactId>
<version>1.12</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>mailer</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>junit</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class KubernetesDeclarativeAgent extends DeclarativeAgent<KubernetesDecla
private List<ContainerTemplate> containerTemplates;
private String defaultContainer;
private String yaml;
private String yamlFile;

@DataBoundConstructor
public KubernetesDeclarativeAgent() {
Expand Down Expand Up @@ -172,6 +173,15 @@ public void setActiveDeadlineSeconds(int activeDeadlineSeconds) {
this.activeDeadlineSeconds = activeDeadlineSeconds;
}

public String getYamlFile() {
return yamlFile;
}

@DataBoundSetter
public void setYamlFile(String yamlFile) {
this.yamlFile = yamlFile;
}

public Map<String, Object> getAsArgs() {
Map<String, Object> argMap = new TreeMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ import org.jenkinsci.plugins.workflow.cps.CpsScript
public class KubernetesDeclarativeAgentScript extends DeclarativeAgentScript<KubernetesDeclarativeAgent> {
public KubernetesDeclarativeAgentScript(CpsScript s, KubernetesDeclarativeAgent a) {
super(s, a)

}

@Override
public Closure run(Closure body) {
return {
try {
if (describable.getYamlFile() != null && describable.hasScmContext(script)) {
describable.setYaml(script.readTrusted(describable.getYamlFile()))
}
script.podTemplate(describable.asArgs) {
script.node(describable.label) {
if (describable.isDoCheckout() && describable.hasScmContext(script)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@

import static org.junit.Assert.*;

import jenkins.plugins.git.GitSampleRepoRule;
import jenkins.plugins.git.GitStep;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;

public class KubernetesDeclarativeAgentTest extends AbstractKubernetesPipelineTest {
@Rule
public GitSampleRepoRule repoRule = new GitSampleRepoRule();

@Issue("JENKINS-41758")
@Test
Expand Down Expand Up @@ -61,4 +67,24 @@ public void declarativeFromYaml() throws Exception {
r.assertLogContains("BUSYBOX_CONTAINER_ENV_VAR = busybox\n", b);
}

@Issue("JENKINS-52259")
@Test
public void declarativeFromYamlFile() throws Exception {
repoRule.init();
repoRule.write("Jenkinsfile", loadPipelineScript("declarativeFromYamlFile.groovy"));
repoRule.write("declarativeYamlFile.yml", loadPipelineScript("declarativeYamlFile.yml"));
repoRule.git("add", "Jenkinsfile");
repoRule.git("add", "declarativeYamlFile.yml");
repoRule.git("commit", "--message=files");

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "job with dir");
p.setDefinition(new CpsScmFlowDefinition(new GitStep(repoRule.toString()).createSCM(), "Jenkinsfile"));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("Apache Maven 3.3.9", b);
r.assertLogContains("OUTSIDE_CONTAINER_ENV_VAR = jnlp\n", b);
r.assertLogContains("MAVEN_CONTAINER_ENV_VAR = maven\n", b);
r.assertLogContains("BUSYBOX_CONTAINER_ENV_VAR = busybox\n", b);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pipeline {
agent {
kubernetes {
label 'mypod'
label 'declarative-pod'
containerTemplate {
name 'maven'
image 'maven:3.3.9-jdk-8-alpine'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
pipeline {
agent {
kubernetes {
label 'mypod'
label 'declarativefromyaml-pod'
yaml """
metadata:
labels:
some-label: some-label-value
class: KubernetesDeclarativeAgentTest
spec:
containers:
- name: jnlp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
pipeline {
agent {
kubernetes {
label 'declarativefromyamlfile-pod'
yamlFile 'declarativeYamlFile.yml'
}
}
options {
// Because there's no way for the container to actually get at the git repo on the disk of the box we're running on.
skipDefaultCheckout(true)
}
stages {
stage('Run maven') {
steps {
sh 'set'
sh "echo OUTSIDE_CONTAINER_ENV_VAR = ${CONTAINER_ENV_VAR}"
container('maven') {
sh 'echo MAVEN_CONTAINER_ENV_VAR = ${CONTAINER_ENV_VAR}'
sh 'mvn -version'
}
container('busybox') {
sh 'echo BUSYBOX_CONTAINER_ENV_VAR = ${CONTAINER_ENV_VAR}'
sh '/bin/busybox'
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
metadata:
labels:
some-label: some-label-value
class: KubernetesDeclarativeAgentTest
spec:
containers:
- name: jnlp
env:
- name: CONTAINER_ENV_VAR
value: jnlp
- name: maven
image: maven:3.3.9-jdk-8-alpine
command:
- cat
tty: true
env:
- name: CONTAINER_ENV_VAR
value: maven
- name: busybox
image: busybox
command:
- cat
tty: true
env:
- name: CONTAINER_ENV_VAR
value: busybox