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

Windows环境下GitHub Actions中deploy_path变量字符替换求助

解决方案

在Windows 2019的GitHub Actions工作流中,你可以通过以下两种可靠方式实现变量转换并持久化环境变量:

方案一:使用PowerShell(推荐)

指定shell: powershell确保步骤用PowerShell执行,通过PowerShell的-replace完成两次字符替换,再将结果写入GITHUB_ENV让后续步骤可访问:

- name: 转换deploy_path为newstr
  shell: powershell
  run: |
    # 执行两次替换:将\替换为/,去掉:
    $transformedPath = $env:deploy_path -replace '\\', '/' -replace ':', ''
    # 将结果写入GITHUB_ENV,持久化环境变量
    echo "newstr=$transformedPath" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append

关键说明:

  • $env:deploy_path:直接读取工作流中已定义的deploy_path环境变量
  • -replace '\\', '/':PowerShell中\是正则转义符,所以用\\匹配原路径中的反斜杠,替换为正斜杠
  • -replace ':', '':匹配冒号并替换为空字符
  • 写入GITHUB_ENV:仅在当前PowerShell会话中设置$env:newstr无法让后续步骤读取,必须通过Out-File写入GITHUB_ENV文件实现持久化

方案二:使用CMD脚本

如果偏好CMD语法,可以分步完成两次替换,同样需要将结果写入GITHUB_ENV:

- name: 转换deploy_path为newstr
  shell: cmd
  run: |
    # 第一步:将\替换为/
    set "temp_path=%deploy_path:\=/%"
    # 第二步:去掉:
    set "newstr=%temp_path::=%"
    # 写入GITHUB_ENV持久化变量
    echo newstr=%newstr% >> %GITHUB_ENV%

验证使用

后续步骤中可以通过${{ env.newstr }}访问转换后的变量,比如:

- name: 验证转换结果
  run: echo 转换后的路径:${{ env.newstr }}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 12:02:21