-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Task to Trigger Jenkins Pipeline using Tekton
The following task can be used to trigger an existing Jenkins pipeline from Tekton using the CURL request by providing the required parameters. Signed-off-by: vinamra28 <[email protected]>
- Loading branch information
Showing
4 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Trigger Jenkins | ||
|
||
The following task can be used to trigger a Jenkins pipeline using CURL request from a Tekton Task. | ||
|
||
More details on Remote Access API can be found [here](https://www.jenkins.io/doc/book/using/remote-access-api/) | ||
|
||
## Install the Task | ||
|
||
```bash | ||
kubectl apply -f https://raw.githubusercontent.com/tektoncd/catalog/master/trigger-jenkins-pipeline/trigger-jenkins-pipeline.yaml | ||
``` | ||
|
||
## Parameters | ||
|
||
- **JENKINS_HOST_URL**: The URL on which Jenkins is running (**Required**) | ||
- **JOB_NAME**: The Job name which needs to be triggered (**Required**) | ||
- **JENKINS_SECRETS**: The name of the secret containing the username and API token for authenticating the Jenkins (_Default_: jenkins-credentials) (**Required**) | ||
- **ARGS**: Extra parameters which needs to be appended in the `CURL` request. (_Default_: ""). `ARGS` is of type `array` so multiple arguments can be appended. `ARGS` can be provided as follows:- | ||
|
||
```yaml | ||
params: | ||
- name: ARGS | ||
value: | | ||
- "--form file0=@PATH_TO_FILE" | ||
- "-form json='{"parameter": [{"name":"FILE_LOCATION_AS_SET_IN_JENKINS", "file":"file0"}]}'" | ||
``` | ||
## Workspaces | ||
- **source**: In case any file needs to be provided to the Jenkins Job. (_Default_: `emptyDir: {}`) | ||
|
||
## Secrets | ||
|
||
This is a secret containing username and API token that is used in the task for making the CURL request. | ||
|
||
```yaml | ||
apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: jenkins-credentials | ||
type: Opaque | ||
stringData: | ||
username: username | ||
apitoken: api-token | ||
``` | ||
|
||
## Usage | ||
|
||
1. Without `ARGS` parameters | ||
|
||
```yaml | ||
apiVersion: tekton.dev/v1beta1 | ||
kind: TaskRun | ||
metadata: | ||
name: trigger-jenkins | ||
spec: | ||
taskRef: | ||
name: trigger-jenkins-pipeline | ||
params: | ||
- name: JENKINS_HOST_URL | ||
value: "http://localhost:8080" | ||
- name: JOB_NAME | ||
value: tekton | ||
workspaces: | ||
- name: source | ||
emptyDir: {} | ||
``` | ||
|
||
1. With `ARGS` parameters | ||
|
||
```yaml | ||
apiVersion: tekton.dev/v1beta1 | ||
kind: TaskRun | ||
metadata: | ||
name: trigger-jenkins | ||
spec: | ||
taskRef: | ||
name: trigger-jenkins-pipeline | ||
params: | ||
- name: JENKINS_HOST_URL | ||
value: "http://localhost:8080" | ||
- name: JOB_NAME | ||
value: tekton | ||
- name: ARGS | ||
value: | | ||
- "--data-urlencode json='{"parameter": [{"name":"id", "value":"123"}, {"name":"verbosity", "value":"high"}]}'" | ||
workspaces: | ||
- name: source | ||
emptyDir: {} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
apiVersion: tekton.dev/v1beta1 | ||
kind: TaskRun | ||
metadata: | ||
name: trigger-jenkins | ||
spec: | ||
taskRef: | ||
name: trigger-jenkins-pipeline | ||
params: | ||
- name: JENKINS_HOST_URL | ||
value: http://localhost:8080 | ||
- name: JOB_NAME | ||
value: tekton | ||
- name: JENKINS_SECRETS | ||
value: jenkins-credentials | ||
workspaces: | ||
- name: source | ||
emptyDir: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: jenkins-credentials | ||
type: Opaque | ||
stringData: | ||
username: username | ||
apitoken: api-token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
apiVersion: tekton.dev/v1beta1 | ||
kind: Task | ||
metadata: | ||
name: trigger-jenkins-pipeline | ||
spec: | ||
workspaces: | ||
- name: source | ||
params: | ||
- name: JENKINS_HOST_URL | ||
type: string | ||
description: Server URL on which Jenkins is running | ||
- name: JOB_NAME | ||
type: string | ||
description: Jenkins Job which needs to be triggered | ||
- name: JENKINS_SECRETS | ||
type: string | ||
description: Jenkins secret containing credentials | ||
default: jenkins-credentials | ||
- name: ARGS | ||
type: array | ||
description: Extra arguments to append as a part of CURL request | ||
default: [""] | ||
steps: | ||
- name: trigger-pipeline | ||
image: registry.access.redhat.com/ubi8/ubi:latest | ||
workingDir: $(workspaces.source.path) | ||
args: | ||
- $(params.ARGS) | ||
script: | | ||
#!/usr/bin/env bash | ||
curl -X POST "$(params.JENKINS_HOST_URL)"/job/"$(params.JOB_NAME)"/build \ | ||
--user $USERNAME:$API_TOKEN $@ | ||
env: | ||
- name: USERNAME | ||
valueFrom: | ||
secretKeyRef: | ||
name: $(params.JENKINS_SECRETS) | ||
key: username | ||
- name: API_TOKEN | ||
valueFrom: | ||
secretKeyRef: | ||
name: $(params.JENKINS_SECRETS) | ||
key: apitoken |