Tekton: 调试为什么 when 子句被评估为 false

huangapple go评论77阅读模式
英文:

Tekton: debugging why a when-clause is evaluated as false

问题

我创建了一个非常简单的任务,用于检查Git仓库上的当前分支是否是一个发布分支。

  1. apiVersion: tekton.dev/v1beta1
  2. kind: Task
  3. metadata:
  4. name: check-git-remote-release-branches
  5. spec:
  6. params:
  7. - default: .
  8. description: 根文件夹的位置
  9. name: PATH_CONTEXT
  10. type: string
  11. - default: master
  12. description: Git参考
  13. name: GIT_REF
  14. type: string
  15. results:
  16. - description: 如果是发布分支,则为true
  17. name: is-release-branch
  18. steps:
  19. - image: 'registry.redhat.io/rhel8/nodejs-16:latest'
  20. name: 检索远程发布分支
  21. resources: {}
  22. script: >
  23. #!/bin/bash
  24. set -x
  25. git config --global --add safe.directory '*'
  26. git config --global user.email "git@48hg.ch"
  27. git config --global user.name "48hG - CICD"
  28. REMOTE_RELEASE_BRANCHES=($(git ls-remote --heads origin | grep release/ | awk '{print $2}'))
  29. echo "当前引用 $(params.GIT_REF)"
  30. echo REMOTE_RELEASE_BRANCHES
  31. echo "false" | tee $(results.is-release-branch.path)
  32. for element in "${REMOTE_RELEASE_BRANCHES[@]}"; do
  33. echo "$element"
  34. if [[ $element == $(params.GIT_REF) ]]; then
  35. echo "true" | tee $(results.is-release-branch.path)
  36. fi
  37. done
  38. cat $(results.is-release-branch.path)
  39. workingDir: /workspace/source/$(params.PATH_CONTEXT)
  40. workspaces:
  41. - name: source

在我的Tekton流水线中,我设置了一个任务,如果分支是发布分支,则在"result"上写入true。

  1. - name: check-git-remote-release-branches
  2. params:
  3. - name: PATH_CONTEXT
  4. value: $(params.context_path)
  5. - name: GIT_REF
  6. value: $(params.git-ref)
  7. runAfter:
  8. - continuous-integration-nodejs
  9. taskRef:
  10. kind: Task
  11. name: check-git-remote-release-branches
  12. workspaces:
  13. - name: source
  14. workspace: source-workspace

随后的任务使用此结果来评估一个"when"子句:

  1. - name: release-increase-version
  2. params:
  3. - name: PATH_CONTEXT
  4. value: $(params.context_path)
  5. runAfter:
  6. - check-git-remote-release-branches
  7. taskRef:
  8. kind: Task
  9. name: nodejs-release-commit
  10. when:
  11. - input: $(tasks.check-git-remote-release-branches.results.is-release-branch)
  12. operator: in
  13. values:
  14. - 'true'

即使在正确的文件中写入true,"when"子句也永远不起作用。我该如何调试这个问题?有没有办法检查为什么比较失败?

英文:

I created a very simple task that checks if the current branch on a git repo is a release branch

  1. apiVersion: tekton.dev/v1beta1
  2. kind: Task
  3. metadata:
  4. name: check-git-remote-release-branches
  5. spec:
  6. params:
  7. - default: .
  8. description: The location of the root folder
  9. name: PATH_CONTEXT
  10. type: string
  11. - default: master
  12. description: Git reference
  13. name: GIT_REF
  14. type: string
  15. results:
  16. - description: true if release branch
  17. name: is-release-branch
  18. steps:
  19. - image: 'registry.redhat.io/rhel8/nodejs-16:latest'
  20. name: retrieving-remote-release-branches
  21. resources: {}
  22. script: >
  23. #!/bin/bash
  24. set -x
  25. git config --global --add safe.directory '*'
  26. git config --global user.email "git@48hg.ch"
  27. git config --global user.name "48hG - CICD"
  28. REMOTE_RELEASE_BRANCHES=($(git ls-remote --heads origin | grep release/
  29. | awk '{print $2}'))
  30. echo "Current ref $(params.GIT_REF)"
  31. echo REMOTE_RELEASE_BRANCHES
  32. echo "false" | tee $(results.is-release-branch.path)
  33. for element in "${REMOTE_RELEASE_BRANCHES[@]}"; do
  34. echo "$element"
  35. if [[ $element == $(params.GIT_REF) ]]; then
  36. echo "true" | tee $(results.is-release-branch.path)
  37. fi
  38. done
  39. cat $(results.is-release-branch.path)
  40. workingDir: /workspace/source/$(params.PATH_CONTEXT)
  41. workspaces:
  42. - name: source

In my Tekton pipeline i setup a task that writes on a "result" true if the branch is a release branch.

  1. - name: check-git-remote-release-branches
  2. params:
  3. - name: PATH_CONTEXT
  4. value: $(params.context_path)
  5. - name: GIT_REF
  6. value: $(params.git-ref)
  7. runAfter:
  8. - continuous-integration-nodejs
  9. taskRef:
  10. kind: Task
  11. name: check-git-remote-release-branches
  12. workspaces:
  13. - name: source
  14. workspace: source-workspace

A subsequent task uses this result to evaluate a when clause:

  1. - name: release-increase-version
  2. params:
  3. - name: PATH_CONTEXT
  4. value: $(params.context_path)
  5. runAfter:
  6. - check-git-remote-release-branches
  7. taskRef:
  8. kind: Task
  9. name: nodejs-release-commit
  10. when:
  11. - input: $(tasks.check-git-remote-release-branches.results.is-release-branch)
  12. operator: in
  13. values:
  14. - 'true'

the when clause does never work even if true is wrote in the right file.
How can i debug this? is there a way for me to check why the comparison fails?

答案1

得分: 2

我怀疑问题与结果生成方式有关:

  1. echo "true" | tee $(results.is-release-branch.path)

使用echo会生成true\n作为结果,这与when表达式不匹配。
我建议将任务定义更改为:

  1. printf "true" | tee $(results.is-release-branch.path)
英文:

I suspect the issue is related to how the result is produced:

  1. echo "true" | tee $(results.is-release-branch.path)

Using echo will produce true\n as a result, which does not match the when expression.
I would recommend changing the task definition to:

  1. printf "true" | tee $(results.is-release-branch.path)

huangapple
  • 本文由 发表于 2023年6月8日 01:31:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定