Skip to content

Latest commit

 

History

History
203 lines (136 loc) · 5.4 KB

README.md

File metadata and controls

203 lines (136 loc) · 5.4 KB

Openshift Pipeline Trigger Demo

Prerequisites

Pipeline Diagram

pipeline-diagram

Prepare Project

oc new-project vote-app

Every resource should be created in the vote-app project.

Init Source Repository

You can find a guide on how to set up a Gitea Git Server on Openshift here: link.

This step is optional and can be done by other means. For the pipeline triggers to work you will need:

  • a Gitea user named gitea
  • a vote-ui gitea repository
  • a vote-api gitea repository

If you followed the guide to setup Gitea entirely you should have the user gitea. Cloning the repo can be done via a task run.

sed "s#@GITEA_SERVER_CLUSTER_IP@#$(oc -n gitea-git-server get svc gitea -o template --template='{{.spec.clusterIP}}')#g" templates/clone-vote-app-task-run.yaml | oc create -f -

Follow the logs.

tkn tr logs -f -a $(tkn tr ls -n vote-app | awk 'NR==2{print $1}')

You should see the task run.

tkn taskrun ls
NAME                   STARTED   DURATION   STATUS
clone-vote-app-qvnd7   now       15s        Succeeded

Quick Setup

If you want to setup all resources in on step then use the command below and then go to the Webhook step. If you want to setup every resource step by step then ignore the command below and start from Create PVC.

oc create -k kustomization.yaml

Delete.

oc delete -k kustomization.yaml

Create PVC

Used to share data between pipeline tasks.

oc apply -f templates/source-pvc.yaml

Create Tasks

The pipeline will use two custom tasks and two build in tasks. The custom tasks are apply-manifests and update-deployment.

oc create -f templates/tasks.yaml

You should see the tasks.

tkn tasks ls
NAME                   DESCRIPTION   AGE
apply-manifests                      now
update-deployment                    now

Create Pipeline

oc create -f templates/pipeline.yaml

You should see the pipeline.

tkn pipeline ls
NAME               AGE   LAST RUN   STARTED   DURATION   STATUS
build-and-deploy   now   -          -         -          -

Trigger Templates

Trigger template generates Tekton resources in response to events from the event listener.

oc create -f templates/trigger-templates.yaml

You should see the templates.

tkn tt ls
NAME                        AGE
vote-api-trigger-template   now
vote-ui-trigger-template    now

Trigger Binding

Trigger binding binds attributes from the event payload with parameters in the trigger template. We will use two attributes from the event payload:

  • after: commit hash of the latest pushed commit
  • repository.clone_url: Git repo clone url

Values can be retrieved using JSONPath expressions.

oc create -f templates/trigger-binding.yaml

You should see the trigger binding.

tkn tb ls
NAME                             AGE
gitea-vote-app-trigger-binding   now

Event Listener

Event listener is an inteface which recieves external events. When an event is recieved it will trigger the creation of Tekton resources like pipelines depending on how they are defined in the trigger template. Webhooks from Gitea will send their events to this resource.

oc create -f templates/event-listeners.yaml

You should see the event listeners.

tkn el ls
NAME                     AGE   URL                                                                AVAILABLE
gitea-webhook-vote-api   now   http://el-gitea-webhook-vote-api.vote-api.svc.cluster.local:8080   True
gitea-webhook-vote-ui    now   http://el-gitea-webhook-vote-ui.vote-api.svc.cluster.local:8080    True

Webhook

You can create the webhook manually via the Gitea UI or use the webhook task run. The task run assumes that the Gitea server and k8 service are running in the gitea-git-server project/namespace.

sed "s#@GITEA_SERVER_CLUSTER_IP@#$(oc -n gitea-git-server get svc gitea -o template --template='{{.spec.clusterIP}}')#g" templates/gitea-webhook-task-run.yaml | oc create -f -

View task run logs.

tkn tr logs -f -a $(tkn tr ls | awk 'NR==2{print $1}')

Triggers in Action

If you have watch installed open a new terminal and watch the pipelines run.

watch tkn pr ls

Clone and edit the source and push the changes.

git clone http://gitea-vote-app.apps-crc.testing/gitea/pipelines-vote-ui.git
git clone http://gitea-vote-app.apps-crc.testing/gitea/pipelines-vote-api.git

Make a change, commit and push the changes. A pipeline run should be triggered.

Every 2.0s: tkn pr  ls

NAME                                 STARTED      DURATION   STATUS
build-and-deploy-vote-ui-app-bzq5v   now          1m41s      Succeeded

You can also checkout the pipeline run in the Openshift console.

Resources