yamlcontinuous-integrationgithub-actionscicd

How to print GitHub Actions matrix values from another job?


Suppose I defined this job:

name: workflow

on:
  push:
    branches:
      - main2
jobs:

  job1:
    runs-on: ubuntu-latest
    env:
      GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    strategy: 
      matrix:
        values: [val1, val2, val3, val4]
    steps:
      - uses: actions/checkout@v4   
  
    ....... (other steps here) .........


  job2:
    needs: [job1]
    runs-on: ubuntu-latest
    env:
      GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - uses: actions/checkout@v4   
      - name: List of matrix values
        run: |
          # I want to print matrix values here

whether printed as comma-separate values,

val1, val2, val3, val4

or in column with new-line separated values.

val1
val2
val3
val4

I tried:

name: workflow

on:
  push:
    branches:
      - main2
jobs:

  job1:
    runs-on: ubuntu-latest
    env:
      GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    strategy: 
      matrix:
        values: [val1, val2, val3, val4]
      outputs:
        values: ${{ steps.values_id.outputs.values_array }}
    steps:
      - uses: actions/checkout@v4   
  
    ....... (other steps here) .........
      - name: Export nuclei names
        id: values_id
        run: echo "values_array=${{ matrix.values }}" >> "$GITHUB_OUTPUT"



  job2:
    needs: [job1]
    runs-on: ubuntu-latest
    env:
      GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - uses: actions/checkout@v4   
      - name: List of matrix values
        run: |
          echo "${{ needs.job1.outputs.values }}"

which prints only the last executed matrix value instead of all of the matrix values. I consulted matrix strategy from GitHub Actions documentation, I didn't find something relevant to my question.


Solution

  • This should work:

    job1:
      runs-on: ubuntu-latest
      strategy: 
        matrix:
          values: [val1, val2, val3, val4]     
      steps:
        - run: echo ${{ matrix.values }} > value
        - uses: actions/upload-artifact@v4
          with:
            name: matrix-${{ matrix.values }}
            path: value
    
    collect:
      needs: job1
      runs-on:  ubuntu-latest
      steps:
        - uses: actions/download-artifact@v4
          with:
            pattern: matrix-*
        - run: |
            cat */value
    
        - uses: geekyeggo/delete-artifact@v5
          with:
            name: matrix-*