You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在GitHub Action中拆分变量值并迭代执行步骤

解决方案

要实现拆分多行的poetry-config变量并逐行执行poetry config命令,你可以在action.yml中使用Bash循环遍历每一行配置,具体修改如下:

修改后的action.yml

name: Setup Poetry
description: Set up python and poetry

runs:
  using: "composite"
  steps:
    - name: Apply poetry configs
      shell: bash
      run: |
        while IFS= read -r line; do
          # 移除行首尾的双引号,拆分参数执行命令
          poetry config $(echo "$line" | sed -e 's/^"//' -e 's/"$//')
        done <<< "${{ inputs.poetry-config }}"
      working-directory: ${{ inputs.poetry-project }}

关键说明

  • while IFS= read -r line循环读取poetry-config的每一行内容:IFS=确保保留行首尾的空格,-r避免反斜杠被错误转义。
  • sed -e 's/^"//' -e 's/"$//'用于移除你在工作流中给每行配置添加的首尾双引号,让参数能被poetry config正确识别。
  • 处理后的行内容会作为参数传入poetry config,实现逐行执行配置命令。

可选优化:简化工作流参数写法

你也可以去掉工作流中poetry-config每行的双引号,直接写成:

poetry-config: |
  http-basic.abc1 test1 value1
  http-basic.abc2 test2 value2

这种情况下,action.yml里的sed处理步骤可以省略,直接用poetry config $line即可完成命令执行。

内容的提问来源于stack exchange,提问作者Ashish Arora

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 23:28:16