Good morning! Looking at the documentation on available variables (https://github.com/tektoncd/pipeline/blob/master/docs/variables.md#variables-available-in-a-pipeline), I am unable to understand how to construct an evaluation expression to determine whether or not a resource exists.
For example, upon pipeline run, I may not know if a certain resource exists and I would only want to run it if it does not.
I'm running this on an OpenShift cluster, if that makes a difference. Thank you for your time!
Hi @itscatherinelam !
WhenExpressions takes _params_ or _results_ from previous Tasks as input. To execute a Task based on whether some resource exists, you typically first need to run a Task e.g. does-namespace-exists that output the "fact" (true or false) as a Task Result and then in the next Task, you can use WhenExpression with the result from the does-namespace-exists-result. The does-namespace-exists Task is not included in Tekton, it is up to the user to implement it, currently.
Mornin', @jlpettersson!
Thank you for your reply! I must admit that I'm not well versed in scripting. Let me know if I'm stepping outside my allowable boundaries in this discussion area, but could you provide any insight on what else I should edit in this custom task to get the desired result? I've bolded the areas where I am not sure how to proceed: 1) passing in a string as a param, and 2) grabbing the result from the previous step, and using... scripting language (???) to evaluate it. I put in pseudocode since I have to learn some scripting first, but hopefully it looks mildly comprehensible?
spec:
params:
- default: oc $@
description: The OpenShift CLI arguments to run
name: SCRIPT
type: string
- default:
- help
description: The OpenShift CLI arguments to run
name: ARGS
type: array
resources:
inputs:
- name: source #TODO: Is there a way to just pass in a string, like 'example-build'?
optional: true
type: git
results:
- description: True/false result for if build exists.
name: bool-build-exists
steps:
- args:
- new-build
- $(params.ARGS)
image: 'quay.io/openshift/origin-cli:latest'
name: oc
resources: {}
script: $(params.SCRIPT)
- image: 'bash:latest'
name: evaluate-build-exists
resources: {}
script: |
#!/usr/bin/env bash
(TODO: if builtExists [previous step's result contains "failed, build exists", return true) | tee /tekton/results/bool-build-exists
Thank you for your time, I really appreciate it :) And again - let me know if these kinds of specific questions are frowned upon in this discussion area.
I think you are on the right track @itscatherinelam ! It's a good start.
I don't have full overview or time to test your task, but it mostly looks good and I can give some feedback.
Is there a way to just pass in a string, like 'example-build'?
I think I know what you are looking for, you want to execute a command like:
oc get build <build-name>
and in addition, write true/false or yes/no or something to the _result path_. You are right, it is probably good to write this in the script: | section, as you have started with. In addition, the 'example-build' should be used where I wrote <build-name>, I recommend to use a Task Parameter for this value, and yes, it can be a string.
The part below is for using a git-repository as input for your task - but I don't think you need that(?) (git repository input can also be done through a workspace when using a git-clone task)
So I think you can remove the lines listed below.
resources:
inputs:
- name: source #TODO: Is there a way to just pass in a string, like 'example-build'?
optional: true
type: git
What you need to do now is to complete the script on the script: | field, and use input from the Task parameter, and you are already using the origin-cli image. You may also need to configure some authentication for your oc command.
Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale with a justification.
Stale issues rot after an additional 30d of inactivity and eventually close.
If this issue is safe to close now please do so with /close with a justification.
If this issue should be exempted, mark the issue as frozen with /lifecycle frozen with a justification.
/lifecycle stale
Send feedback to tektoncd/plumbing.
Most helpful comment
Hi @itscatherinelam !
WhenExpressionstakes _params_ or _results_ from previousTasksas input. To execute aTaskbased on whether some resource exists, you typically first need to run aTaske.g.does-namespace-existsthat output the "fact" (true or false) as a Task Result and then in the nextTask, you can useWhenExpressionwith the result from thedoes-namespace-exists-result. Thedoes-namespace-existsTask is not included in Tekton, it is up to the user to implement it, currently.