Automate OpenShift Pipelines With Triggers
In this blog, we will automate our PipelineRuns using Triggers. We will create a trigger that will to kick off PipelineRuns whenever it detects a code push to our GitHub repository.
Background#
Red Hat OpenShift is the leading enterprise Kubernetes platform that provides a consistent experience to manage hybrid cloud, multi-cloud, and edge deployments. We will be using OpenShift to deploy and manage our Spring Boot application.
OpenShift Pipelines (based on Tekton) is a cloud-native, CI/CD solution. We will use the pipeline to automate the build and deploy phases of the application lifecycle.
Before we begin#
Please refer to my previous blog “Maven Deploy with Nexus Repository in OpenShift 4” on how to build a CI/CD pipeline in OpenShift 4 using OpenShift Pipelines. We will have to have a pipeline created before creating a webhook trigger to automate it.
1. Creating a trigger for the pipeline#
Within the OpenShift 4 Pipeline view, you should be able to create a trigger using the Actions dropdown (“Add Trigger”):
And configure the trigger with values you would set during a typical PipelineRun. For Git Provider type, we want to select github-push
since we will be using GitHub and trigger based on git push:
Then click “Add”.
2. EventListeners and TriggerTemplates#
EventListeners#
After you create your trigger, go to Pipelines > Triggers > EventListers and verify the trigger created an EventListener:
When you select that event-listener, you will see a URL tied to it. This will be the Github webhook payload URL we need to use (copy the link):
Create a webhook for your project in Github (project > Settings > Webhook > Add Webhook):
Make sure the Content Type is application/json. Then add webhook.
NOTE: When the trigger was created, it automatically created our EventListener and a route associated to it (which maps back to our EventListener service):
TriggerTemplates#
When we created our trigger, it also created a TriggerTemplate.
The TriggerTemplate helps define the Pipeline and all the parameters needed for the PipelineRun to our trigger:
apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerTemplate
metadata:
name: trigger-template-animals-counter-pipeline-2mc2tm
namespace: pipeline-tutorial
spec:
params:
- name: git-revision
- name: git-commit-message
- name: git-repo-url
- name: git-repo-name
- name: content-type
- name: pusher-name
resourcetemplates:
- apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
annotations: {}
generateName: animals-counter-pipeline-
labels:
tekton.dev/pipeline: animals-counter-pipeline
namespace: pipeline-tutorial
spec:
params:
- name: IMAGE_TAG
value: latest
pipelineRef:
name: animals-counter-pipeline
resources: []
status: null
workspaces:
- name: shared-workspace
persistentVolumeClaim:
claimName: pvc-animals-counter
- configMap:
name: custom-maven-settings
name: maven-settings
3. Make a code change and verify the Webhook#
In my project, I updated the README file and pushed the commit.
In my Pipeline view, we can see the pipeline automatically kicked off from our trigger configuration:
And we can see our pipeline completed successfully!