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

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

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

问题

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

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: check-git-remote-release-branches
spec:
  params:
    - default: .
      description: 根文件夹的位置
      name: PATH_CONTEXT
      type: string
    - default: master
      description: Git参考
      name: GIT_REF
      type: string
  results:
    - description: 如果是发布分支,则为true
      name: is-release-branch
  steps:
    - image: 'registry.redhat.io/rhel8/nodejs-16:latest'
      name: 检索远程发布分支
      resources: {}
      script: >
        #!/bin/bash  

        set -x

        git config --global --add safe.directory '*'

        git config --global user.email "git@48hg.ch" 

        git config --global user.name "48hG - CICD" 

        REMOTE_RELEASE_BRANCHES=($(git ls-remote --heads origin |  grep release/ | awk '{print $2}'))

        echo "当前引用 $(params.GIT_REF)"

        echo REMOTE_RELEASE_BRANCHES

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

        for element in "${REMOTE_RELEASE_BRANCHES[@]}";  do
            echo "$element"
            if [[ $element == $(params.GIT_REF) ]]; then
                echo "true" | tee $(results.is-release-branch.path)
            fi
        done

        cat $(results.is-release-branch.path)        
      workingDir: /workspace/source/$(params.PATH_CONTEXT)
  workspaces:
    - name: source

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

    - name: check-git-remote-release-branches
          params:
            - name: PATH_CONTEXT
              value: $(params.context_path)
            - name: GIT_REF
              value: $(params.git-ref)
          runAfter:
            - continuous-integration-nodejs
          taskRef:
            kind: Task
            name: check-git-remote-release-branches
          workspaces:
            - name: source
              workspace: source-workspace

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

    - name: release-increase-version
      params:
        - name: PATH_CONTEXT
          value: $(params.context_path)
      runAfter:
        - check-git-remote-release-branches
      taskRef:
        kind: Task
        name: nodejs-release-commit
      when:
        - input: $(tasks.check-git-remote-release-branches.results.is-release-branch)
          operator: in
          values:
            - 'true'

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

英文:

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

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: check-git-remote-release-branches
spec:
  params:
    - default: .
      description: The location of the root folder
      name: PATH_CONTEXT
      type: string
    - default: master
      description: Git reference
      name: GIT_REF
      type: string
  results:
    - description: true if release branch
      name: is-release-branch
  steps:
    - image: 'registry.redhat.io/rhel8/nodejs-16:latest'
      name: retrieving-remote-release-branches
      resources: {}
      script: >
        #!/bin/bash  

        set -x

        git config --global --add safe.directory '*'

        git config --global user.email "git@48hg.ch" 

        git config --global user.name "48hG - CICD" 

        REMOTE_RELEASE_BRANCHES=($(git ls-remote --heads origin |  grep release/
        | awk '{print $2}'))

        echo "Current ref $(params.GIT_REF)"

        echo REMOTE_RELEASE_BRANCHES

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

        for element in "${REMOTE_RELEASE_BRANCHES[@]}";  do
            echo "$element"
            if [[ $element == $(params.GIT_REF) ]]; then
                echo "true" | tee $(results.is-release-branch.path)
            fi
        done

        cat $(results.is-release-branch.path)
      workingDir: /workspace/source/$(params.PATH_CONTEXT)
  workspaces:
    - name: source

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

    - name: check-git-remote-release-branches
          params:
            - name: PATH_CONTEXT
              value: $(params.context_path)
            - name: GIT_REF
              value: $(params.git-ref)
          runAfter:
            - continuous-integration-nodejs
          taskRef:
            kind: Task
            name: check-git-remote-release-branches
          workspaces:
            - name: source
              workspace: source-workspace

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

    - name: release-increase-version
      params:
        - name: PATH_CONTEXT
          value: $(params.context_path)
      runAfter:
        - check-git-remote-release-branches
      taskRef:
        kind: Task
        name: nodejs-release-commit
      when:
        - input: $(tasks.check-git-remote-release-branches.results.is-release-branch)
          operator: in
          values:
            - '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

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

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

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

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

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

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:

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:

确定