ループを使用してリスト、マップ、またはコレクションを反復処理します。
ループはinで定義されたコレクションを反復処理し、反復ごとにindexおよびelement変数を自動的に作成します。JQ 式${{ .steps.<loopStepName>.loop.element }}または${{ .steps.<loopStepName>.loop.index }}を使用してこれらにアクセスします。
ループによって可能になるもの:
- コレクション、リスト、配列を反復処理する
- 各項目を
elementで処理し、index変数で位置を追跡します breakおよびcontinueステートメントによるループフローの制御- 複雑なデータ処理のためのネストループ
パラメーター
for(必須): ループ開始を通知しますin(必須、文字列式): コレクションに評価される式steps(必須): 各反復で実行されるステップ。ネストされたループを含む任意のステップタイプを含めることができます
ワークフロー入力によるループ
name: myWorkflow steps: - name: loopStep type: loop for: in: '${{ .workflowInputs.list }}' steps: - name: step1 type: action action: internal.example.sayHello version: '1' inputs: name: 'Element: ${{ .steps.loopStep.loop.element }}, Index: ${{ .steps.loopStep.loop.index }}'重要な注意事項
for: 必須。ループの開始を示す最上位要素in: 必須。入力コレクションはJava配列にキャスト可能でなければなりませんsteps: 必須。ステップは各反復を実行するelementindexはループの反復ごとに自動的に割り当てられますindexゼロベースelement複雑なコレクションの場合は複合型になる- ループ変数とステップ出力はループ内でのみアクセス可能
- 変数はループ終了時にクリアされ、外部からアクセスした場合はnullになります
- ループはループ外で定義された変数にアクセスできる
整数の単純なループ
name: myWorkflow steps: - name: loopStep type: loop for: in: ${{ [range(1; 6)] }} steps: - name: step1 type: action action: internal.example.sayHello version: '1' inputs: name: 'Element: ${{ .steps.loopStep.loop.element }}, Index: ${{ .steps.loopStep.loop.index }}'マップのシンプルなループ
name: myWorkflow steps: - name: loopStep type: loop for: in: '${{ [ { "key1": "val1" }, { "key2": "val2"} ] }}' steps: - name: step1 type: action action: internal.example.sayHello version: '1' inputs: name: 'Element: ${{ .steps.loopStep.loop.element }}, Index: ${{ .steps.loopStep.loop.index }}'ループ内でジャンプする
同じループ内の名前付きステップ間でのみジャンプします。ループ内/ループ外、内部/外部ループ、または異なるループ間でのジャンプは許可されません。
name: myWorkflow steps: - name: firstStep type: action action: internal.example.sayHello version: '1' - name: loopStep type: loop for: in: '${{ .workflowInputs.list }}' steps: - name: step1 type: action action: internal.example.sayHello version: '1' inputs: name: '${{ .steps.loopStep.loop.element }}' next: step3 # Okay within the loop - name: step2 type: action action: internal.example.sayHello version: '1' inputs: name: '${{ .steps.step1.outputs.greeting }}' - name: step3 type: action action: internal.example.sayHello version: '1' next: firstStep # Not okay, first step is not in the loop contextループ内でbreak/continueを使用する
ループフローをnext: breakまたはnext: continueで変更します。これらはループ内で暗黙的に定義された予約済みのジャンプ ターゲットです。ループ外では、ワークフローの終了にジャンプします。
end ループ内ではbreakとして動作します。nextスイッチ ステップまたは任意のステップ タイプで機能します。
name: myWorkflow steps: - name: loopStep type: loop for: in: '${{ [range(1; 6)] }}' steps: - name: insideLoopStep1 type: action action: internal.example.sayHello version: '1' inputs: name: '${{ .steps.loopStep.loop.element }}' next: continue - name: insideLoopStep2 type: action action: internal.example.sayHello version: '1' inputs: name: '${{ .steps.loopStep.loop.element }}' - name: loopStepAgain type: loop for: in: '${{ .workflowInputs.list }}' steps: - name: switchStep type: switch switch: - condition: '${{ .steps.loopStepAgain.loop.index >= 0 }}' next: break - name: insideLoopStepAgain type: action action: internal.example.sayHello version: '1' inputs: name: '${{ .steps.loopStepAgain.loop.element }}'次のステップ
- 条件付きロジック: 意思決定にはスイッチ文を使用する
- REST APIポーリング:実際のAPIでループの動作を確認する