Skip to content

Commit

Permalink
updating an example pipelinerun with array indexing
Browse files Browse the repository at this point in the history
Validating the expected param values while indexing into an array when task
array params mapped to a pipeline array params and task string params are
an elements of a pipeline array param

Signed-off-by: pritidesai <pdesai@us.ibm.com>
  • Loading branch information
pritidesai authored and JeromeJu committed Feb 6, 2023
1 parent 3c6383e commit 9764444
Showing 1 changed file with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,86 @@ spec:
value: '$(params.environments[0])'
- name: environment2
value: '$(params.environments[1])'
- name: environments
value: '$(params.environments[*])'
taskSpec:
params:
- name: environment1
type: string
- name: environment2
type: string
- name: environments
type: array
steps:
# this step should echo "staging"
- name: echo-params-1
image: bash:3.2
# args must be initialed to "staging"
- name: validate-environment1
image: bash:latest
args: [
"echo",
"$(params.environment1)",
]
# this step should echo "staging"
- name: echo-params-2
script: |
if [[ $# != 1 ]]; then
echo "failed to validate the length of the arguments"
echo "Want: 1, Got: $#"
exit 1
fi
if [[ $1 != "staging" ]]; then
echo "failed to validate the one and only argument of the script"
echo "Want: staging, Got: $1"
exit 1
fi
if [[ $(params.environments[2]) != "prod" ]]; then
echo "failed to validate indexing into an array param"
echo "Want: prod, Got: $(params.environments[2])"
fi
# this step validates string param which must be set to an array element - qa
- name: validate-environment2
image: ubuntu
script: |
#!/bin/bash
VALUE=$(params.environment2)
EXPECTED="qa"
diff=$(diff <(printf "%s\n" "${VALUE[@]}") <(printf "%s\n" "${EXPECTED[@]}"))
if [[ -z "$diff" ]]; then
echo "Get expected: ${VALUE}"
echo "Got expected: ${VALUE}"
exit 0
else
echo "Want: ${EXPECTED} Got: ${VALUE}"
exit 1
fi
# this step validates an array param which must have three values
# also validates indexing into an array param
- name: validate-environments
image: bash:latest
args: [
"$(params.environments[*])",
]
script: |
if [[ $# != 3 ]]; then
echo "failed to validate the length of the arguments"
echo "Want: 3, Got: $#"
exit 1
fi
if [[ $(params.environments[0]) != "staging" ]]; then
echo "failed to validate the first array element while indexing into an array"
echo "Want: staging, Got: $(params.environments[0])"
exit 1
fi
if [[ $(params.environments[1]) != "qa" ]]; then
echo "failed to validate the second array element while indexing into an array"
echo "Want: qa, Got: $(params.environments[1])"
exit 1
fi
if [[ $(params.environments[2]) != "prod" ]]; then
echo "failed to validate the third array element while indexing into an array"
echo "Want: prod, Got: $(params.environments[2])"
fi
---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
name: deployrun
generateName: pipelinerun-param-array-indexing-
spec:
pipelineRef:
name: deploy
Expand Down

0 comments on commit 9764444

Please sign in to comment.