Skip to content

Commit b1e971e

Browse files
authored
Merge pull request #141 from garethjevans/params
feat: add some default parameters when starting a pipelinerun
2 parents 612f163 + 4513e40 commit b1e971e

File tree

8 files changed

+225
-142
lines changed

8 files changed

+225
-142
lines changed

pom.xml

+21
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@
8888
<artifactId>git</artifactId>
8989
<scope>test</scope>
9090
</dependency>
91+
<dependency>
92+
<groupId>org.jenkins-ci.plugins</groupId>
93+
<artifactId>github-branch-source</artifactId>
94+
<version>2.9.7</version>
95+
<scope>test</scope>
96+
</dependency>
9197
<dependency>
9298
<groupId>org.jenkins-ci.plugins</groupId>
9399
<artifactId>envinject</artifactId>
@@ -133,6 +139,21 @@
133139
<artifactId>jackson-module-jaxb-annotations</artifactId>
134140
<version>2.12.1</version>
135141
</dependency>
142+
<dependency>
143+
<groupId>com.squareup.okio</groupId>
144+
<artifactId>okio</artifactId>
145+
<version>1.17.5</version>
146+
</dependency>
147+
<dependency>
148+
<groupId>com.squareup.okhttp3</groupId>
149+
<artifactId>okhttp</artifactId>
150+
<version>3.14.9</version>
151+
</dependency>
152+
<dependency>
153+
<groupId>com.squareup.okhttp3</groupId>
154+
<artifactId>logging-interceptor</artifactId>
155+
<version>3.14.9</version>
156+
</dependency>
136157
</dependencies>
137158
</dependencyManagement>
138159

src/main/java/org/waveywaves/jenkins/plugins/tekton/client/TektonUtils.java

-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ public static InputStream urlToByteArrayStream(URL url) {
142142
}
143143
}
144144

145-
146145
return inputStream;
147146
}
148147

src/main/java/org/waveywaves/jenkins/plugins/tekton/client/build/create/CreateRaw.java

+56-13
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
import hudson.util.ListBoxModel;
2121
import io.fabric8.kubernetes.client.KubernetesClient;
2222
import io.fabric8.tekton.client.TektonClient;
23+
import io.fabric8.tekton.pipeline.v1beta1.ArrayOrString;
24+
import io.fabric8.tekton.pipeline.v1beta1.Param;
2325
import io.fabric8.tekton.pipeline.v1beta1.Pipeline;
2426
import io.fabric8.tekton.pipeline.v1beta1.PipelineRun;
27+
import io.fabric8.tekton.pipeline.v1beta1.PipelineRunSpec;
2528
import io.fabric8.tekton.pipeline.v1beta1.Task;
2629
import io.fabric8.tekton.pipeline.v1beta1.TaskRun;
2730
import io.jenkins.plugins.checks.api.ChecksConclusion;
@@ -32,6 +35,9 @@
3235
import io.jenkins.plugins.checks.api.ChecksStatus;
3336
import java.time.LocalDateTime;
3437
import java.time.ZoneOffset;
38+
import java.util.ArrayList;
39+
import java.util.Arrays;
40+
import java.util.Optional;
3541
import org.jenkinsci.Symbol;
3642
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider;
3743
import org.kohsuke.stapler.DataBoundConstructor;
@@ -143,7 +149,7 @@ public String getClusterName() {
143149
return clusterName;
144150
}
145151

146-
protected String createWithResourceSpecificClient(TektonResourceType resourceType, InputStream inputStream) throws Exception {
152+
protected String createWithResourceSpecificClient(TektonResourceType resourceType, InputStream inputStream, EnvVars envVars) throws Exception {
147153
switch (resourceType) {
148154
case task:
149155
return createTask(inputStream);
@@ -152,7 +158,7 @@ protected String createWithResourceSpecificClient(TektonResourceType resourceTyp
152158
case pipeline:
153159
return createPipeline(inputStream);
154160
case pipelinerun:
155-
return createPipelineRun(inputStream);
161+
return createPipelineRun(inputStream, envVars);
156162
default:
157163
return "";
158164
}
@@ -220,35 +226,71 @@ public String createPipeline(InputStream inputStream) {
220226
return resourceName;
221227
}
222228

223-
public String createPipelineRun(InputStream inputStream) throws Exception {
229+
public String createPipelineRun(InputStream inputStream, EnvVars envVars) throws Exception {
224230
if (pipelineRunClient == null) {
225231
TektonClient tc = (TektonClient) tektonClient;
226232
setPipelineRunClient(tc.v1beta1().pipelineRuns());
227233
}
228234
String resourceName;
229-
PipelineRun pipelineRun = pipelineRunClient.load(inputStream).get();
235+
final PipelineRun pipelineRun = pipelineRunClient.load(inputStream).get();
230236
if (!Strings.isNullOrEmpty(namespace) && Strings.isNullOrEmpty(pipelineRun.getMetadata().getNamespace())) {
231237
pipelineRun.getMetadata().setNamespace(namespace);
232238
}
239+
240+
// GIT_BRANCH=origin/main
241+
// GIT_COMMIT=bb1ef888f5375bb19d5bb227e35bfcfed49759ed
242+
// GIT_PREVIOUS_COMMIT=9c7648f892913dfa12963a38fe489b1291e033a8
243+
// GIT_PREVIOUS_SUCCESSFUL_COMMIT=9c7648f892913dfa12963a38fe489b1291e033a8
244+
// GIT_URL=https://github.com/garethjevans/test-tekton-client
245+
246+
setParamOnPipelineRunSpec(pipelineRun.getSpec(), "BUILD_ID", envVars.get("BUILD_ID"));
247+
// JOB_NAME
248+
// JOB_SPEC
249+
// JOB_TYPE
250+
// PULL_BASE_REF
251+
setParamOnPipelineRunSpec(pipelineRun.getSpec(), "PULL_BASE_SHA", envVars.get("GIT_COMMIT"));
252+
// PULL_NUMBER
253+
// PULL_PULL_REF
254+
// PULL_PULL_SHA
255+
// PULL_REFS
256+
// REPO_NAME
257+
// REPO_OWNER
258+
setParamOnPipelineRunSpec(pipelineRun.getSpec(), "REPO_URL", envVars.get("GIT_URL"));
259+
233260
String ns = pipelineRun.getMetadata().getNamespace();
234-
if (Strings.isNullOrEmpty(ns)) {
235-
pipelineRun = pipelineRunClient.create(pipelineRun);
236-
} else {
237-
pipelineRun = pipelineRunClient.inNamespace(ns).create(pipelineRun);
238-
}
239-
resourceName = pipelineRun.getMetadata().getName();
261+
262+
LOGGER.info("Creating PipelineRun " + pipelineRun);
263+
264+
PipelineRun updatedPipelineRun = Strings.isNullOrEmpty(ns) ?
265+
pipelineRunClient.create(pipelineRun) :
266+
pipelineRunClient.inNamespace(ns).create(pipelineRun);
267+
268+
resourceName = updatedPipelineRun.getMetadata().getName();
240269

241270
ChecksDetails checkDetails = new ChecksDetails.ChecksDetailsBuilder()
242-
.withName("Tekton: " + pipelineRun.getMetadata().getName())
271+
.withName("Tekton: " + updatedPipelineRun.getMetadata().getName())
243272
.withStatus(ChecksStatus.IN_PROGRESS)
244273
.withConclusion(ChecksConclusion.NONE)
245274
.build();
246275
checksPublisher.publish(checkDetails);
247276

248-
streamPipelineRunLogsToConsole(pipelineRun);
277+
streamPipelineRunLogsToConsole(updatedPipelineRun);
278+
249279
return resourceName;
250280
}
251281

282+
private void setParamOnPipelineRunSpec(@NonNull PipelineRunSpec spec, String paramName, String paramValue) {
283+
if (spec.getParams() == null) {
284+
spec.setParams(new ArrayList<>());
285+
}
286+
Optional<Param> param = spec.getParams().stream().filter(p -> p.getName().equals(paramName)).findAny();
287+
if (param.isPresent()) {
288+
param.get().setValue(new ArrayOrString(paramValue));
289+
} else {
290+
spec.getParams().add(new Param(paramName, new ArrayOrString(paramValue)));
291+
}
292+
}
293+
252294
public void streamTaskRunLogsToConsole(TaskRun taskRun) throws Exception {
253295
synchronized (consoleLogger) {
254296
KubernetesClient kc = (KubernetesClient) kubernetesClient;
@@ -338,7 +380,7 @@ protected String runCreate(Run<?, ?> run, FilePath workspace, EnvVars envVars) {
338380
} else {
339381
TektonResourceType resourceType = kind.get(0);
340382
LOGGER.info("creating kind " + resourceType.name());
341-
createdResourceName = createWithResourceSpecificClient(resourceType, new ByteArrayInputStream(data));
383+
createdResourceName = createWithResourceSpecificClient(resourceType, new ByteArrayInputStream(data), envVars);
342384
}
343385
}
344386
} catch (Throwable e) {
@@ -488,6 +530,7 @@ private byte[] processTektonCatalog(EnvVars envVars, File dir, File file, byte[]
488530
builder.command(binary, "-b", "--add-defaults", "-f", filePath, "-o", outputFile.getPath());
489531
if (envVars != null) {
490532
for (Map.Entry<String, String> entry : envVars.entrySet()) {
533+
LOGGER.info("Adding env var " + entry.getKey() + "=" + entry.getValue());
491534
builder.environment().put(entry.getKey(), entry.getValue());
492535
}
493536
}

src/main/java/org/waveywaves/jenkins/plugins/tekton/client/logwatch/PipelineRunLogWatch.java

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public void run() {
9494
}
9595
}
9696
}
97+
9798
if (taskComplete) {
9899
logMessageLn("completed PipelineTask " + pipelineTaskName);
99100
break;

src/test/java/org/waveywaves/jenkins/plugins/tekton/client/build/create/CreateRawMockServerTest.java

+30-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.waveywaves.jenkins.plugins.tekton.client.build.create;
22

3+
import hudson.EnvVars;
34
import io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition;
45
import io.fabric8.kubernetes.client.KubernetesClient;
56

@@ -95,15 +96,15 @@ public void testTaskRunCreate() {
9596

9697
// Mocked Responses
9798
TaskRunBuilder taskRunBuilder = new TaskRunBuilder()
98-
.withNewMetadata().withName("home-is-set-1234").endMetadata();
99-
List<TaskRun> trList = new ArrayList<TaskRun>();
100-
TaskRun testTaskRun = taskRunBuilder.build();
101-
trList.add(testTaskRun);
102-
TaskRunList taskRunList = new TaskRunList();
103-
taskRunList.setItems(trList);
99+
.withNewMetadata()
100+
.withName("home-is-set-1234")
101+
.endMetadata();
102+
TaskRunList taskRunList = new TaskRunListBuilder()
103+
.addToItems(taskRunBuilder.build())
104+
.build();
104105

105106
server.expect().post().withPath("/apis/tekton.dev/v1beta1/namespaces/test/taskruns")
106-
.andReturn(HttpURLConnection.HTTP_CREATED, testTaskRun).once();
107+
.andReturn(HttpURLConnection.HTTP_CREATED, taskRunBuilder.build()).once();
107108
server.expect().get().withPath("/apis/tekton.dev/v1beta1/namespaces/test/taskruns")
108109
.andReturn(HttpURLConnection.HTTP_OK, taskRunList).once();
109110

@@ -151,15 +152,15 @@ public void testPipelineCreate() {
151152

152153
// Mocked Responses
153154
PipelineBuilder pipelineBuilder = new PipelineBuilder()
154-
.withNewMetadata().withName("testPipeline").endMetadata();
155-
List<Pipeline> pList = new ArrayList<Pipeline>();
156-
Pipeline testPipeline = pipelineBuilder.build();
157-
pList.add(testPipeline);
158-
PipelineList pipelineList = new PipelineList();
159-
pipelineList.setItems(pList);
155+
.withNewMetadata()
156+
.withName("testPipeline")
157+
.endMetadata();
158+
PipelineList pipelineList = new PipelineListBuilder()
159+
.addToItems(pipelineBuilder.build())
160+
.build();
160161

161162
server.expect().post().withPath("/apis/tekton.dev/v1beta1/namespaces/test/pipelines")
162-
.andReturn(HttpURLConnection.HTTP_CREATED, testPipeline).once();
163+
.andReturn(HttpURLConnection.HTTP_CREATED, pipelineBuilder.build()).once();
163164
server.expect().get().withPath("/apis/tekton.dev/v1beta1/namespaces/test/pipelines")
164165
.andReturn(HttpURLConnection.HTTP_OK, pipelineList).once();
165166

@@ -185,7 +186,9 @@ public void testPipelineRunCreate() {
185186
String testPipelineRunYaml = "apiVersion: tekton.dev/v1beta1\n" +
186187
"kind: PipelineRun\n" +
187188
"metadata:\n" +
188-
" name: testPipelineRun\n";
189+
" name: testPipelineRun\n" +
190+
"spec:\n" +
191+
" params: []\n";
189192

190193
KubernetesClient client = server.getClient();
191194
InputStream crdAsInputStream = getClass().getResourceAsStream("/pipeline-crd.yaml");
@@ -195,15 +198,19 @@ public void testPipelineRunCreate() {
195198

196199
// Mocked Responses
197200
PipelineRunBuilder pipelineRunBuilder = new PipelineRunBuilder()
198-
.withNewMetadata().withName("testPipelineRun").endMetadata();
199-
List<PipelineRun> prList = new ArrayList<PipelineRun>();
200-
PipelineRun testPipelineRun = pipelineRunBuilder.build();
201-
prList.add(testPipelineRun);
202-
PipelineRunList pipelineRunList = new PipelineRunList();
203-
pipelineRunList.setItems(prList);
201+
.withNewMetadata()
202+
.withName("testPipelineRun")
203+
.endMetadata()
204+
.withNewSpec()
205+
.withParams(new Param("param", new ArrayOrString("value")))
206+
.endSpec();
207+
208+
PipelineRunList pipelineRunList = new PipelineRunListBuilder()
209+
.addToItems(pipelineRunBuilder.build())
210+
.build();
204211

205212
server.expect().post().withPath("/apis/tekton.dev/v1beta1/namespaces/test/pipelines")
206-
.andReturn(HttpURLConnection.HTTP_CREATED, testPipelineRun).once();
213+
.andReturn(HttpURLConnection.HTTP_CREATED, pipelineRunBuilder.build()).once();
207214
server.expect().get().withPath("/apis/tekton.dev/v1beta1/namespaces/test/pipelines")
208215
.andReturn(HttpURLConnection.HTTP_OK, pipelineRunList).once();
209216

@@ -226,7 +233,7 @@ public void streamPipelineRunLogsToConsole(PipelineRun pipelineRun) {
226233
String createdPipelineName = "";
227234
try {
228235
createdPipelineName = createRaw.createPipelineRun(
229-
new ByteArrayInputStream(testPipelineRunYaml.getBytes(StandardCharsets.UTF_8)));
236+
new ByteArrayInputStream(testPipelineRunYaml.getBytes(StandardCharsets.UTF_8)), new EnvVars());
230237
} catch (Exception e) {
231238
fail(e.getMessage(), e);
232239
}

src/test/java/org/waveywaves/jenkins/plugins/tekton/client/build/create/mock/CreateRawMock.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.waveywaves.jenkins.plugins.tekton.client.build.create.mock;
22

3+
import hudson.EnvVars;
34
import io.fabric8.tekton.pipeline.v1beta1.PipelineRun;
45
import io.fabric8.tekton.pipeline.v1beta1.TaskRun;
56
import org.waveywaves.jenkins.plugins.tekton.client.TektonUtils;
@@ -28,7 +29,7 @@ public String createPipeline(InputStream inputStream) {
2829
}
2930

3031
@Override
31-
public String createPipelineRun(InputStream inputStream) { return TektonUtils.TektonResourceType.pipelinerun.toString(); }
32+
public String createPipelineRun(InputStream inputStream, EnvVars envVars) { return TektonUtils.TektonResourceType.pipelinerun.toString(); }
3233

3334
@Override
3435
public void streamTaskRunLogsToConsole(TaskRun taskRun) {

src/test/java/org/waveywaves/jenkins/plugins/tekton/client/build/create/mock/FakeCreateRaw.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.waveywaves.jenkins.plugins.tekton.client.build.create.mock;
22

3+
import hudson.EnvVars;
34
import org.apache.commons.io.IOUtils;
45

56
import java.io.IOException;
@@ -33,7 +34,7 @@ public String createPipeline(InputStream inputStream) {
3334
}
3435

3536
@Override
36-
public String createPipelineRun(InputStream inputStream) {
37+
public String createPipelineRun(InputStream inputStream, EnvVars envVars) {
3738
return createResource(inputStream);
3839
}
3940

0 commit comments

Comments
 (0)