Search code examples
github-actionsdevops

Receiving empty string in Github Action


I'm currently trying to convert an array of paths to files in the repository that can be used to deploy APIs. But the issue is, every time I try to pass the array of paths into another job, I receive this error:

Error when evaluating 'strategy' for job 'Lint-spec-file'. .github/workflows/detect-and-lint.yml (Line: 42, Col: 20): Unexpected value ''

My bash script fetches through changes in the current commit, if a path matches then it gets appended into a new file and counted. The count triggers the workflow in the second job, and the path is used to deploy an API. But for some reason, an empty string keeps being passed back, not sure why. When I run it locally, I receive the following "path=["path/to/file/", "path/to/file/"].

Below is my bash script in Github:

jobs:
  Detect-spec-file:
    runs-on: [ubuntu-latest]
    outputs:
      repo-path: ${{ steps.spec-file-change.outputs.path }}
      path-count: ${{ steps.spec-file-change.outputs.spec_file_count }}

    steps:
    - name: Checkout Repository
      uses: actions/checkout@v2
    - name: Detect Spec File
      id: spec-file-change
      run: |
        array=()
        git log -m -1 --name-only --pretty='format:' '${{ github.sha }}' >> output.txt
        grep -i 'path/to/file/*' output.txt >> specpath.txt
        SPEC_COUNT=$(wc -l specpath.txt)
        echo "spec_file_count=$SPEC_COUNT" >> "GITHUB_OUTPUT"

        while IFS= read -r line; do
            array+=("$line")
        done < "specpath.txt"
        echo "path=$(printf '%s\n' "${array[@]}" | jq -R . | jq -cs )" >> "GITHUB_OUTPUT"
       
  Lint-spec-file:
    needs: Detect-spec-file
    runs-on: [ubuntu-latest]
    strategy:
      max-parallel: 1
      matrix: 
        spec-file: ${{needs.Detect-spec-file.outputs.repo-path}}

I use "JQ" as a way to separate the values in the array. And it works well in with "FromJson".


Solution

  • You have to use fromJson while passing the array into the matrix.

    Lint-spec-file:
        needs: Detect-spec-file
        runs-on: [ubuntu-latest]
        strategy:
          max-parallel: 1
          matrix: 
            spec-file: ${{fromJson(needs.Detect-spec-file.outputs.repo-path)}}