본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

조건부 실행(Conditionals)

원문 보기 위키 갱신

when 구문으로 스텝을 조건부로 실행할 수 있어요. govaluate 기반의 표현식 문법과 문자열 정규식 매칭까지 예제와 함께 살펴 볼게요.

출처: Argo Workflows 문서

본문

조건부 실행도 지원해요. 문법은 복잡한 표현식을 지원하는 govaluate로 구현됐어요. 예제를 보세요:

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: coinflip-
spec:
  entrypoint: coinflip
  templates:
  - name: coinflip
    steps:
    # flip a coin
    - - name: flip-coin
        template: flip-coin
    # evaluate the result in parallel
    - - name: heads
        template: heads                       # call heads template if "heads"
        when: "{{steps.flip-coin.outputs.result}} == heads"
      - name: tails
        template: tails                       # call tails template if "tails"
        when: "{{steps.flip-coin.outputs.result}} == tails"
    - - name: flip-again
        template: flip-coin
    - - name: complex-condition
        template: heads-tails-or-twice-tails
        # call heads template if first flip was "heads" and second was "tails" OR both were "tails"
        when: >-
            ( {{steps.flip-coin.outputs.result}} == heads &&
              {{steps.flip-again.outputs.result}} == tails
            ) ||
            ( {{steps.flip-coin.outputs.result}} == tails &&
              {{steps.flip-again.outputs.result}} == tails )
      - name: heads-regex
        template: heads                       # call heads template if ~ "hea"
        when: "{{steps.flip-again.outputs.result}} =~ hea"
      - name: tails-regex
        template: tails                       # call heads template if ~ "tai"
        when: "{{steps.flip-again.outputs.result}} =~ tai"

  # Return heads or tails based on a random number
  - name: flip-coin
    script:
      image: python:alpine3.23
      command: [python]
      source: |
        import random
        result = "heads" if random.randint(0,1) == 0 else "tails"
        print(result)

  - name: heads
    container:
      image: alpine:3.23
      command: [sh, -c]
      args: ["echo \"it was heads\""]

  - name: tails
    container:
      image: alpine:3.23
      command: [sh, -c]
      args: ["echo \"it was tails\""]

  - name: heads-tails-or-twice-tails
    container:
      image: alpine:3.23
      command: [sh, -c]
      args: ["echo \"it was heads the first flip and tails the second. Or it was two times tails.\""]

!!! Warning "중첩된 따옴표" 파라미터 값에 따옴표가 들어 있으면 govaluate 표현식이 깨질 수 있어요. 따옴표가 있는 파라미터를 다루려면 조건 안에 expr 표현식을 넣으세요. 예:

when: "{{=inputs.parameters['may-contain-quotes'] == 'example'}}"

더 알아보기 (Learn more)

  • 조건이 참일 때만 태스크를 건너뛸 때는 when 결과가 .Skipped로 기록돼요.
  • 따옴표가 포함된 파라미터 비교는 govaluate 대신 expr 표현식을 사용하세요.
  • conditionals 예제를 직접 제출해 동작을 확인해 보세요.