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

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

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

问题

这个 Github 动作旨在将 `ls` 命令的输出转换为 Github Actions 中策略矩阵的输入 [json]。

下面,工作流在 UNIX github runner 上尝试失败。

name: 将 LS 输出转换为策略矩阵

on:
  push:
    branches:
      - main

jobs:
  转换为矩阵:
    runs-on: ubuntu-latest

    steps:
      - name: 获取 LS 输出
        id: matrix_step
        run: |
          list=$(ls -1 /etc/*.conf);
          matrix_values=$(echo '[ "'"$(echo "$list" | sed ':a;N;$!ba;s/\n/", "/g')"" ]')
          echo "Matrix values: $matrix_values"
          echo "matrix_values=$matrix_values" >> $GITHUB_ENV
          
  构建:
    runs-on: ubuntu-latest
    needs: 转换为矩阵
    
    strategy:
      matrix: 
        filename: ${{ needs.setup-matrix.outputs.matrix-combinations }}

    steps:

      - name: 构建文件
        run: |
          echo "构建 ${{ matrix.filename }}"
          # 在此处使用 ${{ matrix.filename }} 变量添加构建步骤

`转换为矩阵` 作业成功并打印出此值 

> 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" ]

但构建作业失败,显示以下错误:

> 将 LS 输出转换为策略矩阵: .github#L1 在评估作业 'build' 的 'strategy' 时出错。.github/workflows/generateinput.yml (行: 27, 列: 19): 预期之外的值 ''.

请问如何将 `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

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

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

strategy:
  matrix: 
    filename: ${{ needs.setup-matrix.outputs.matrix-combinations }}

steps:

  - name: Build the file
    run: |
      echo "Building ${{ matrix.filename }}"
      # 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,以下是正确设置和导出矩阵用法所需输出的更新版本:

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

    steps:
      - name: Get LS Output
        id: matrix_step
        run: |
          list=$(ls -1 /etc/*.conf);
          matrix_values=$(echo '[ "'"$(echo "$list" | sed ':a;N;$!ba;s/\n/", "/g')"" ]"')
          echo "Matrix values: $matrix_values"
          echo "matrix_values=$matrix_values" >> $GITHUB_OUTPUT          
    outputs:
      matrix-combination: ${{ steps.matrix_step.output.matrix_values }}   
  build:
    runs-on: ubuntu-latest
    needs: convert-to-matrix
    
    strategy:
      matrix: 
        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:

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

    steps:
      - name: Get LS Output
        id: matrix_step
        run: |
          list=$(ls -1 /etc/*.conf);
          matrix_values=$(echo '[ "'"$(echo "$list" | sed ':a;N;$!ba;s/\n/", "/g')"'" ]')
          echo "Matrix values: $matrix_values"
          echo "matrix_values=$matrix_values" >> $GITHUB_OUTPUT
    outputs:
      matrix-combination: ${{ steps.matrix_step.output.matrix_values }}   
  build:
    runs-on: ubuntu-latest
    needs: convert-to-matrix
    
    strategy:
      matrix: 
        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:

确定