From 8f6f3e76454f89d10d2df182cbe20f3a35c1e322 Mon Sep 17 00:00:00 2001 From: vinamra28 Date: Thu, 25 Jun 2020 12:39:43 +0530 Subject: [PATCH] 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 --- trigger-jenkins-pipeline/README.md | 90 +++++++++++++++++++ trigger-jenkins-pipeline/examples/run.yaml | 17 ++++ .../examples/secrets.yaml | 8 ++ .../trigger-jenkins-pipeline.yaml | 44 +++++++++ 4 files changed, 159 insertions(+) create mode 100644 trigger-jenkins-pipeline/README.md create mode 100644 trigger-jenkins-pipeline/examples/run.yaml create mode 100644 trigger-jenkins-pipeline/examples/secrets.yaml create mode 100644 trigger-jenkins-pipeline/trigger-jenkins-pipeline.yaml diff --git a/trigger-jenkins-pipeline/README.md b/trigger-jenkins-pipeline/README.md new file mode 100644 index 0000000000..444346b006 --- /dev/null +++ b/trigger-jenkins-pipeline/README.md @@ -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: {} + ``` diff --git a/trigger-jenkins-pipeline/examples/run.yaml b/trigger-jenkins-pipeline/examples/run.yaml new file mode 100644 index 0000000000..2eb6297263 --- /dev/null +++ b/trigger-jenkins-pipeline/examples/run.yaml @@ -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: {} diff --git a/trigger-jenkins-pipeline/examples/secrets.yaml b/trigger-jenkins-pipeline/examples/secrets.yaml new file mode 100644 index 0000000000..9e5b212cc8 --- /dev/null +++ b/trigger-jenkins-pipeline/examples/secrets.yaml @@ -0,0 +1,8 @@ +apiVersion: v1 +kind: Secret +metadata: + name: jenkins-credentials +type: Opaque +stringData: + username: username + apitoken: api-token diff --git a/trigger-jenkins-pipeline/trigger-jenkins-pipeline.yaml b/trigger-jenkins-pipeline/trigger-jenkins-pipeline.yaml new file mode 100644 index 0000000000..2bf8202fdf --- /dev/null +++ b/trigger-jenkins-pipeline/trigger-jenkins-pipeline.yaml @@ -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