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

Added a generic method to create then run a pipeline based on the current method name #527

Merged
merged 4 commits into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@
import static java.util.Arrays.*;
import static org.csanchez.jenkins.plugins.kubernetes.KubernetesTestUtil.*;

import java.io.IOException;
import java.net.InetAddress;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;

import hudson.Util;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.csanchez.jenkins.plugins.kubernetes.ContainerEnvVar;
Expand All @@ -40,6 +45,9 @@
import org.csanchez.jenkins.plugins.kubernetes.model.KeyValueEnvVar;
import org.csanchez.jenkins.plugins.kubernetes.model.SecretEnvVar;
import org.csanchez.jenkins.plugins.kubernetes.model.TemplateEnvVar;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
Expand Down Expand Up @@ -80,6 +88,10 @@ public static void isKubernetesConfigured() throws Exception {

private String projectName;

protected WorkflowJob p;

protected WorkflowRun b;

@Before
public void defineProjectName() {
// Add spaces before uppercases
Expand All @@ -90,6 +102,28 @@ protected String getProjectName() {
return projectName;
}

/**
* Creates a pipeline job using <methodName>.groovy as pipeline definition,
* then schedule it and wait for it to start.
*
* Resolves $NAME to the method name in order to avoid any hard-coded reference
* to the method name within the pipeline definition.
*
* @return The scheduled pipeline run
* @throws IOException If something gets wrong when creating the pipeline job
* @throws ExecutionException If something went wrong while retrieving the run object
* @throws InterruptedException If the thread gets interrupted while waiting for the run to start
*/
protected final WorkflowRun createJobThenScheduleRun() throws IOException, ExecutionException, InterruptedException {
p = r.jenkins.createProject(WorkflowJob.class, getProjectName());
Map<String, String> env = new HashMap<>();
env.put("NAME", name.getMethodName());
String script = Util.replaceMacro(loadPipelineScript(name.getMethodName() + ".groovy"), env);
p.setDefinition(new CpsFlowDefinition(script, true));
b = p.scheduleBuild2(0).waitForStart();
return b;
}

@Before
public void configureCloud() throws Exception {
cloud = setupCloud(this, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
import io.fabric8.kubernetes.api.model.PodList;
import org.csanchez.jenkins.plugins.kubernetes.PodAnnotation;
import org.csanchez.jenkins.plugins.kubernetes.PodTemplate;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import org.junit.Before;
Expand All @@ -64,18 +62,11 @@ public class KubernetesPipelineTest extends AbstractKubernetesPipelineTest {
@Rule
public TemporaryFolder tmp = new TemporaryFolder();

WorkflowJob p;

WorkflowRun b;

@Before
public void setUp() throws Exception {
deletePods(cloud.connect(), getLabels(cloud, this, name), false);
logs.capture(1000);
p = r.jenkins.createProject(WorkflowJob.class, getProjectName());
p.setDefinition(new CpsFlowDefinition(loadPipelineScript(name.getMethodName() + ".groovy"), true));
b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
assertNotNull(createJobThenScheduleRun());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
podTemplate(label: 'badcontainername', containers: [
podTemplate(label: '$NAME', containers: [
containerTemplate(name: 'badcontainerName_!', image: 'busybox', ttyEnabled: true, command: '/bin/cat'),
]) {

node ('badcontainername') {
node ('$NAME') {
stage('Run') {
container('busybox') {
sh """
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
podTemplate(label: 'badcontainernameyaml', yaml: """
podTemplate(label: '$NAME', yaml: """
apiVersion: v1
kind: Pod
metadata:
Expand All @@ -19,7 +19,7 @@ spec:
"""
) {

node ('badcontainernameyaml') {
node ('$NAME') {
stage('Run') {
container('busybox') {
sh """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ spec:
"""
) {

node ('badlabel') {
node ('$NAME') {
stage('Run') {
container('busybox') {
sh """
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pipeline {
agent {
kubernetes {
label 'declarative'
label '$NAME'
containerTemplate {
name 'maven'
image 'maven:3.3.9-jdk-8-alpine'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pipeline {
agent {
kubernetes {
label 'declarative-custom-workspace-pod'
label '$NAME'
customWorkspace 'some/other/path'
defaultContainer 'maven'
yaml """
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pipeline {
agent {
kubernetes {
label 'declarativeFromYaml'
label '$NAME'
yaml """
metadata:
labels:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pipeline {
agent {
kubernetes {
label 'declarativeFromYamlFile'
label '$NAME'
yamlFile 'declarativeYamlFile.yml'
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pipeline {
agent {
kubernetes {
label 'declarativefromyaml-pod'
label '$NAME'
yaml """
metadata:
namespace: kubernetes-plugin-test-overridden-namespace
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pipeline {
agent {
kubernetes {
label 'parent-pod'
label '$NAME-parent'
yaml """
spec:
containers:
Expand All @@ -17,7 +17,7 @@ spec:
stage('Run maven') {
agent {
kubernetes {
label 'nested-pod'
label '$NAME-nested'
yaml """
spec:
containers:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pipeline {
agent {
kubernetes {
label 'docker'
label '$NAME'
containerTemplate {
name 'docker'
image 'docker:1.11'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//noinspection GrPackage
podTemplate(label: 'getContainerLog') {
node ('getContainerLog') {
podTemplate(label: '$NAME') {
node ('$NAME') {
stage('container log') {
containerLog 'jnlp'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//noinspection GrPackage
podTemplate(label: 'getContainerLogWithRestart')
podTemplate(label: '$NAME')
{
node ('getContainerLogWithRestart') {
node ('$NAME') {
stage('container log') {
sh 'for i in `seq 1 5`; do echo $i; sleep 5; done'
containerLog 'jnlp'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
podTemplate(label: 'runDirContext', containers: [
podTemplate(label: '$NAME', containers: [
containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat'),
]) {

node ('runDirContext') {
node ('$NAME') {
stage('Run') {
container('busybox') {
sh 'mkdir hz'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
podTemplate(label: 'mypod', containers: [
podTemplate(label: '$NAME-1', containers: [
containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat'),
]) {
semaphore 'podTemplate1'
Expand All @@ -18,11 +18,11 @@ podTemplate(label: 'mypod', containers: [
}
}

podTemplate(label: 'mypod2', containers: [
podTemplate(label: '$NAME-2', containers: [
containerTemplate(name: 'busybox2', image: 'busybox', ttyEnabled: true, command: '/bin/cat'),
]) {
semaphore 'podTemplate2'
node ('mypod2') {
node ('$NAME-2') {
semaphore 'pod2'
stage('Run') {
container('busybox2') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
podTemplate(label: 'runInPod', containers: [
podTemplate(label: '$NAME', containers: [
containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat'),
]) {
semaphore 'podTemplate'
node ('runInPod') {
node ('$NAME') {
semaphore 'pod'
stage('Run') {
container('busybox') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
podTemplate(label: 'runInPodFromYaml', yaml: """
podTemplate(label: '$NAME', yaml: """
apiVersion: v1
kind: Pod
metadata:
Expand All @@ -22,7 +22,7 @@ spec:
"""
) {

node ('runInPodFromYaml') {
node ('$NAME') {
stage('Run') {
container('busybox') {
sh '''set +x
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
podTemplate(label: 'runInPodNested1', containers: [
podTemplate(label: '$NAME-parent', containers: [
containerTemplate(name: 'golang', image: 'golang:1.6.3-alpine', ttyEnabled: true, command: '/bin/cat'),
]) {

podTemplate(label: 'runInPodNested', containers: [
podTemplate(label: '$NAME', containers: [
containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', ttyEnabled: true, command: '/bin/cat'),
]) {

node ('runInPodNested') {
node ('$NAME') {
stage('Nested') {
container('maven') {
sh "mvn -version"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
podTemplate(label: 'runInPodNestedExplicitInherit1', containers: [
podTemplate(label: '$NAME-parent', containers: [
containerTemplate(name: 'golang', image: 'golang:1.6.3-alpine', ttyEnabled: true, command: '/bin/cat'),
]) {

podTemplate(label: 'runInPodNestedExplicitInherit', inheritFrom: '', containers: [
podTemplate(label: '$NAME', inheritFrom: '', containers: [
containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', ttyEnabled: true, command: '/bin/cat'),
]) {

node ('runInPodNestedExplicitInherit') {
node ('$NAME') {
stage('Nested') {
container('maven') {
sh "mvn -version"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
podTemplate(label: 'runInPodWithDifferentShell', containers: [
podTemplate(label: '$NAME', containers: [
containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat'),
]) {

node ('runInPodWithDifferentShell') {
node ('$NAME') {
stage('Run') {
container(name:'busybox', shell: '/bin/bash') {
sh """
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
podTemplate(label: 'runInPodWithLivenessProbe', volumes: [emptyDirVolume(mountPath: '/my-mount')], containers: [
podTemplate(label: '$NAME', volumes: [emptyDirVolume(mountPath: '/my-mount')], containers: [
containerTemplate(name: 'jnlp', image: 'jenkins/jnlp-slave:3.10-1-alpine', args: '${computer.jnlpmac} ${computer.name}'),
containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: 'cat', livenessProbe: containerLivenessProbe( execArgs: 'uname -a', initialDelaySeconds: 5, timeoutSeconds: 1, failureThreshold: 3, periodSeconds: 10, successThreshold: 1))
]) {

node ('runInPodWithLivenessProbe') {
node ('$NAME') {
stage('Wait for Liveness Probe') {
container('busybox') {
sh 'sleep 6 && echo "Still alive"'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
podTemplate(label: 'runInPodWithMultipleContainers', volumes: [emptyDirVolume(mountPath: '/my-mount')], containers: [
podTemplate(label: '$NAME', volumes: [emptyDirVolume(mountPath: '/my-mount')], containers: [
containerTemplate(name: 'jnlp', image: 'jenkins/jnlp-slave:3.10-1-alpine', args: '${computer.jnlpmac} ${computer.name}'),
containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', ttyEnabled: true, command: 'cat'),
containerTemplate(name: 'golang', image: 'golang:1.6.3-alpine', ttyEnabled: true, command: 'cat')
]) {

node ('runInPodWithMultipleContainers') {
node ('$NAME') {
sh "echo My Kubernetes Pipeline"
sh "ls /"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.csanchez.jenkins.plugins.kubernetes.pipeline

podTemplate(label: 'runInPodWithRestart', containers: [
podTemplate(label: '$NAME', containers: [
containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat'),
]) {

node ('runInPodWithRestart') {
node ('$NAME') {
stage('Run') {
container('busybox') {
sh 'mkdir hz'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.csanchez.jenkins.plugins.kubernetes.pipeline

podTemplate(label: 'runInPodWithRestartWithLongSleep', containers: [
podTemplate(label: '$NAME', containers: [
containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat'),
]) {

node ('runInPodWithRestartWithLongSleep') {
node ('$NAME') {
stage('Run') {
container('busybox') {
sh 'for i in `seq 1 10`; do echo $i; sleep 5; done'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.csanchez.jenkins.plugins.kubernetes.pipeline

podTemplate(label: 'runInPodWithRestartWithMultipleContainerCalls', idleMinutes: 0, containers: [
podTemplate(label: '$NAME', idleMinutes: 0, containers: [
containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat'),
]) {

node ('runInPodWithRestartWithMultipleContainerCalls') {
node ('$NAME') {
stage('Run') {
container('busybox') {
sh 'for i in `seq 1 10`; do echo $i; sleep 5; done'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
podTemplate(label: 'runInPodWithRetention', podRetention: always(), containers: [
podTemplate(label: '$NAME', podRetention: always(), containers: [
containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat'),
]) {

node ('runInPodWithRetention') {
node ('$NAME') {
stage('Run') {
container('busybox') {
sh """
Expand Down
Loading