无法将命令的输出分配给 $GITHUB_OUTPUT 在 GitHub Actions 中

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

Unable to assign output of command to $GITHUB_OUTPUT in github actions

问题

  1. 这个 Github 动作旨在将 `ls` 命令的输出转换为 Github Actions 中策略矩阵的输入 [json]。
  2. 下面,工作流在 UNIX github runner 上尝试失败。
  3. name: LS 输出转换为策略矩阵
  4. on:
  5. push:
  6. branches:
  7. - main
  8. jobs:
  9. 转换为矩阵:
  10. runs-on: ubuntu-latest
  11. steps:
  12. - name: 获取 LS 输出
  13. id: matrix_step
  14. run: |
  15. list=$(ls -1 /etc/*.conf);
  16. matrix_values=$(echo '[ "'"$(echo "$list" | sed ':a;N;$!ba;s/\n/", "/g')"" ]')
  17. echo "Matrix values: $matrix_values"
  18. echo "matrix_values=$matrix_values" >> $GITHUB_ENV
  19. 构建:
  20. runs-on: ubuntu-latest
  21. needs: 转换为矩阵
  22. strategy:
  23. matrix:
  24. filename: ${{ needs.setup-matrix.outputs.matrix-combinations }}
  25. steps:
  26. - name: 构建文件
  27. run: |
  28. echo "构建 ${{ matrix.filename }}"
  29. # 在此处使用 ${{ matrix.filename }} 变量添加构建步骤
  30. `转换为矩阵` 作业成功并打印出此值
  31. > Matrix values: [ "/etc/adduser.conf", "/etc/apt-fast.conf",
  32. > "/etc/ca-certificates.conf", "/etc/cgconfig.conf",
  33. > "/etc/cgrules.conf", "/etc/debconf.conf", "/etc/sudo_logsrvd.conf",
  34. > "/etc/sysctl.conf", "/etc/ucf.conf", "/etc/usb_modeswitch.conf",
  35. > "/etc/waagent.conf", "/etc/xattr.conf" ]
  36. 但构建作业失败,显示以下错误:
  37. > 将 LS 输出转换为策略矩阵: .github#L1 在评估作业 'build' 的 'strategy' 时出错。.github/workflows/generateinput.yml (行: 27, 列: 19): 预期之外的值 ''.
  38. 请问如何将 `ls` 命令的输出转换为策略矩阵的合理输入,您能给予建议吗?
英文:

This github action aims to convert the output of ls command into input [json] for the strategy matrix in github actions.

Below, workflow attempt fails on a UNIX github runner.

name: Convert LS Output to Strategy Matrix

on:
push:
branches:
- main

jobs:
convert-to-matrix:
runs-on: ubuntu-latest

  1. steps:
  2. - name: Get LS Output
  3. id: matrix_step
  4. run: |
  5. list=$(ls -1 /etc/*.conf);
  6. matrix_values=$(echo '[ "'"$(echo "$list" | sed ':a;N;$!ba;s/\n/", "/g')"'" ]')
  7. echo "Matrix values: $matrix_values"
  8. echo "matrix_values=$matrix_values" >> $GITHUB_ENV

build:
runs-on: ubuntu-latest
needs: convert-to-matrix

  1. strategy:
  2. matrix:
  3. filename: ${{ needs.setup-matrix.outputs.matrix-combinations }}
  4. steps:
  5. - name: Build the file
  6. run: |
  7. echo "Building ${{ matrix.filename }}"
  8. # Add your build steps here using the ${{ matrix.filename }} variable

convert-to-matrix job is successful and prints this value

> Matrix values: [ "/etc/adduser.conf", "/etc/apt-fast.conf",
> "/etc/ca-certificates.conf", "/etc/cgconfig.conf",
> "/etc/cgrules.conf", "/etc/debconf.conf", "/etc/sudo_logsrvd.conf",
> "/etc/sysctl.conf", "/etc/ucf.conf", "/etc/usb_modeswitch.conf",
> "/etc/waagent.conf", "/etc/xattr.conf" ]

But the build job fails with the following error:

> Convert LS Output to Strategy Matrix: .github#L1 Error when evaluating
> 'strategy' for job 'build'. .github/workflows/generateinput.yml (Line:
> 27, Col: 19): Unexpected value ''

Can you please suggest how can I convert the output of ls to something reasonable input to a strategy matrix.

答案1

得分: 1

上述代码示例的问题包括:

  • 作业级输出

    • 当在不同作业之间使用输出时,应该在作业级别而不仅仅是步骤级别定义所需的输出。
  • fromJSON

    • 然后需要将matrix_values输出用于strategy.matrix,这需要使用fromJSON将值转换为数组。

有关详细讨论和通用示例,请参阅此答案:Github Actions:如何在脚本中使用strategy/matrix

根据评论中提供的url,以下是正确设置和导出矩阵用法所需输出的更新版本:

  1. jobs:
  2. convert-to-matrix:
  3. runs-on: ubuntu-latest
  4. steps:
  5. - name: Get LS Output
  6. id: matrix_step
  7. run: |
  8. list=$(ls -1 /etc/*.conf);
  9. matrix_values=$(echo '[ "'"$(echo "$list" | sed ':a;N;$!ba;s/\n/", "/g')"" ]"')
  10. echo "Matrix values: $matrix_values"
  11. echo "matrix_values=$matrix_values" >> $GITHUB_OUTPUT
  12. outputs:
  13. matrix-combination: ${{ steps.matrix_step.output.matrix_values }}
  14. build:
  15. runs-on: ubuntu-latest
  16. needs: convert-to-matrix
  17. strategy:
  18. matrix:
  19. filename: ${{ fromJSON(needs.setup-matrix.outputs.matrix-combinations) }}

英文:

The issues with the code example above were:

  • job-level outputs
    • When using outputs across different jobs, the required outputs should be defined on job level and not step level only.
  • fromJSON
    • matrix_values output then needs to be used in strategy.matrix which requires fromJSON to convert the value to an array.

Detailed discussion and generic example available in this answer: Github Actions: How use strategy/matrix with script

Based on the url provided in comments, here is the updated version with correctly setting and exporting the required output for matrix usage:

  1. jobs:
  2. convert-to-matrix:
  3. runs-on: ubuntu-latest
  4. steps:
  5. - name: Get LS Output
  6. id: matrix_step
  7. run: |
  8. list=$(ls -1 /etc/*.conf);
  9. matrix_values=$(echo '[ "'"$(echo "$list" | sed ':a;N;$!ba;s/\n/", "/g')"'" ]')
  10. echo "Matrix values: $matrix_values"
  11. echo "matrix_values=$matrix_values" >> $GITHUB_OUTPUT
  12. outputs:
  13. matrix-combination: ${{ steps.matrix_step.output.matrix_values }}
  14. build:
  15. runs-on: ubuntu-latest
  16. needs: convert-to-matrix
  17. strategy:
  18. matrix:
  19. filename: ${{ fromJSON(needs.setup-matrix.outputs.matrix-combinations) }}

huangapple
  • 本文由 发表于 2023年7月24日 00:12:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76749213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定