Skip to content

Commit

Permalink
Add Task to Trigger Jenkins Pipeline using Tekton
Browse files Browse the repository at this point in the history
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
vinamra28 committed Jun 25, 2020
1 parent cf584d8 commit 8f6f3e7
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
90 changes: 90 additions & 0 deletions trigger-jenkins-pipeline/README.md
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: {}
```
17 changes: 17 additions & 0 deletions trigger-jenkins-pipeline/examples/run.yaml
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: {}
8 changes: 8 additions & 0 deletions trigger-jenkins-pipeline/examples/secrets.yaml
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
44 changes: 44 additions & 0 deletions trigger-jenkins-pipeline/trigger-jenkins-pipeline.yaml
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

0 comments on commit 8f6f3e7

Please sign in to comment.