Science and technology

Create conditional pipelines with CEL

You simply adopted a information to start out your Tekton pipeline (or process) when a merge request is created or up to date in your GitLab undertaking. So you configured GitLab to ship merge request occasions as webhooks. And you deployed some Tekton parts:

  • EventListener: receives webhooks from GitLab
  • Trigger: begins your Pipeline each time the EventListener receives a brand new webhook from GitLab
  • Pipeline: fetches the supply code from GitLab and builds it

Then you discover that any occasion in your merge request (a brand new remark, a tag change) triggers the pipeline. That’s not the habits you need. You needn’t construct a remark or a tag, in spite of everything. You solely need the pipeline to run when there’s precise new code to construct. Here’s how I take advantage of Tekton’s CEL Interceptor to create conditionals for my pipelines.

Have your set off prepared

I anticipate you could have a set off already outlined. It’s most likely one thing much like the snippet beneath.

The set off’s interceptor rejects something that is not coming from a merge request. Still, the interceptor shouldn’t be in a position to differentiate between code and non-code updates (like new feedback).

apiVersion: triggers.tekton.dev/v1beta1
type
: Trigger
metadata
:
  identify
: webhook-listener-trigger
spec
:
  interceptors
:
   # reject any payload that is not a merge request webhook
    - identify
: "filter-event-types"
      ref
:
        identify
: "gitlab"
        type
: ClusterInterceptor
      params
:
        - identify
: eventTypes
          worth
:
           - "Merge Request Hook"
  bindings
:
    - ref
: binding
  template
:
    ref
: template

Add a CEL interceptor

Here comes the cel interceptor. This interceptor filters the webhook payload utilizing the CEL expression language. If the filter expression evaluates to true, the pipeline begins.

Here I’m checking for the object_attributes.oldrev discipline to exist within the JSON physique of the webhook payload. If object_attributes.oldrev exists, then which means this occasion is a few code change. If there wasn’t a code change, there isn’t any earlier revision (oldrev) to consult with.

spec:
  interceptors
:
    - identify
: "allow-code-changes-only"
      ref
:
        identify
: cel
        type
: ClusterInterceptor
      params
:
        - identify
: filter
          worth
: >
           has(physique.object_attributes.oldrev)

Add the brand new interceptor to your set off. Now your set off appears like this:

apiVersion: triggers.tekton.dev/v1beta1
type
: Trigger
metadata
:
  identify
: gitlab-listener-trigger
spec
:
  interceptors
:
    - identify
: "verify-gitlab-payload"
      ref
:
        identify
: "gitlab"
        type
: ClusterInterceptor
      params
:
        - identify
: eventTypes
          worth
:
           - "Merge Request Hook"
    - identify
: "allow-code-changes-only"
      ref
:
        identify
: "cel"
        type
: ClusterInterceptor
      params
:
        - identify
: filter
          worth
: >
           has(physique.object_attributes.oldrev)
  bindings
:
    - ref
: binding
  template
:
    ref
: template

Deploy this new model of the set off and benefit from the powers of automation. From now on, your pipeline solely begins if there’s some new code to construct.

Tips

There aren’t any limits to the situations you may set in a CEL filter.

You might verify that the merge request is at the moment open:

physique.object_attributes.state in ['opened']

You can ensure the contributor completed their work on the code:

physique.object_attributes.work_in_progress == false

You simply should concatenate a number of situations appropriately:

- identify: filter
  worth
: >
   has(physique.object_attributes.oldrev) &&
    physique.object_attributes.state in ['opened'] &&
    physique.object_attributes.work_in_progress == false

Check out the merge request events documentation to get impressed to write down your personal situations.

You may have the CEL language definition to know find out how to translate your ideas into code.

To consider sorts aside from strings, you need to know the mapping between JSON and CEL sorts.

Most Popular

To Top