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.
The issues with the code example above were:
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) }}