nektos / act

Run your GitHub Actions locally 🚀
https://nektosact.com
MIT License
54.81k stars 1.37k forks source link

Matrix expession evaluated when not expecting it to be #1482

Open 20k-ultra opened 1 year ago

20k-ultra commented 1 year ago

Bug report info

act version:            0.2.33-12-ga108f10
GOOS:                   linux
GOARCH:                 amd64
NumCPU:                 16
Docker host:            DOCKER_HOST environment variable is unset/empty.
Sockets found:
    /var/run/docker.sock
Config files:           
    /home/mig/.actrc:
        -P ubuntu-latest=catthehacker/ubuntu:act-latest
        -P ubuntu-22.04=catthehacker/ubuntu:act-22.04
        -P ubuntu-20.04=catthehacker/ubuntu:act-20.04
        -P ubuntu-18.04=catthehacker/ubuntu:act-18.04
    .actrc:
        -P ubuntu-latest=catthehacker/ubuntu:act-latest
        -P ubuntu-22.04=catthehacker/ubuntu:act-22.04
        -P ubuntu-20.04=catthehacker/ubuntu:act-20.04
        -P ubuntu-18.04=catthehacker/ubuntu:act-18.04
Build info:
    Go version:            go1.19.3
    Module path:           command-line-arguments
    Main version:          
    Main path:             
    Main checksum:         
    Build settings:
        -compiler:            gc
        -ldflags:             -X main.version=0.2.33-12-ga108f10
        CGO_ENABLED:          1
        CGO_CFLAGS:           
        CGO_CPPFLAGS:         
        CGO_CXXFLAGS:         
        CGO_LDFLAGS:          
        GOARCH:               amd64
        GOOS:                 linux
        GOAMD64:              v1
Docker Engine:
    Engine version:        20.10.21
    Engine runtime:        runc
    Cgroup version:        2
    Cgroup driver:         systemd
    Storage driver:        overlay2
    Registry URI:          https://index.docker.io/v1/
    OS:                    Arch Linux
    OS type:               linux
    OS version:            
    OS arch:               x86_64
    OS kernel:             6.0.8-zen1-1-zen
    OS CPU:                16
    OS memory:             31784 MB
    Security options:
        name=seccomp,profile=default
        name=cgroupns

Command used with act

act -v push -W sample_workflow.yml

Describe issue

I have a step in my workflow which uses another job's output as a matrix parameter. The problem is that this job output is null can be null for specific conditions. I protect my step from trying to evaluate null within the matrix by using an if expression.

On Github, this setup is fine and the job is skipped as expected. With act, the job should be skipped also but instead, an error is thrown from not being able to unmarshal nil to JSON.

Link to GitHub repository

No response

Workflow content

on: push
jobs:
  versions:
    runs-on: ubuntu-latest
    outputs:
      python: ${{ steps.python_versions.outputs.json }}
    steps:
      - name: Set Python versions
        if: inputs.python_enabled == 'true'
        id: python_versions
        run: echo "json=[\"\^3.7\"]" >> $GITHUB_OUTPUT
  python_stuff:
    runs-on: ubuntu-latest
    needs:
      - versions
    if: inputs.python_enabled == 'true'
    strategy:
      matrix:
        python-version: ${{ fromJSON(needs.versions.outputs.python_versions) }}
    steps:
      - run: echo "doing stuff with ${{ matrix.python-version }}"

Relevant log output

DEBU[0000] evaluating expression '${{ fromJSON(needs.versions.outputs.python_versions) }}' 
DEBU[0000] expression '${{ fromJSON(needs.versions.outputs.python_versions) }}' evaluated to '%!t(<nil>)' 
ERRO[0000] Error while evaluating matrix: Cannot parse non-string type invalid as JSON 
DEBU[0000]                                              
FATA[0000] yaml: unmarshal errors:
  line 19: cannot unmarshal !!str `${{ fro...` into []interface {}

Additional information

Here is a link to github run: https://github.com/20k-ultra/action-tester/actions/runs/3580010001

20k-ultra commented 1 year ago

By the way, if I set if: false for the step I want to skip, this error is still thrown so I don't think I can add any expression to try and work around this.

20k-ultra commented 1 year ago

Maybe there's a way I can do something like this if it exists (just tested and it doesn't work but maybe there's a correct way to do this)

      python: ${{ steps.python_versions.outputs.json }} || "[]"
github-actions[bot] commented 1 year ago

Issue is stale and will be closed in 14 days unless there is new activity

github-actions[bot] commented 1 year ago

Issue is stale and will be closed in 14 days unless there is new activity

vfilenga commented 1 year ago

I am finding this same bug at this exact moment

EDIT:

Just so you know, I worked around this by always passing at least an empty array "[]" to the matrix job, and also doing a multi-statement if check to make sure the array is not empty, because it was running the matrix job even with an empty array LOL

ChristopherHX commented 1 year ago

it was running the matrix job even with an empty array LOL

That's in parity with GitHub Actions (Part of undocumented behavior).

BTW I'm not affected by this problem due to how I use act, so it doesn't gain much attention from my side

bonzofenix commented 9 months ago

I found this same issue when I tried to generate the input for the matrix in a dependent job of the main one.