Repository: nektos/act (language: go)

Issue:
Event details not loaded from JSON file provided via --eventpath param
# Setup

Operating system: `macOS Monterey v12.6`
Act version: `0.2.32`

# Issue

The `act` command doesn't seem to read the contents of the GH event JSON file provided via the CLI parameter `--eventpath`.

In particular, the event name, the head ref and the base ref do not get read from the event JSON file. Possibly some other values might not be read either, but I haven't gotten that far.

# Expected result

The values for the event name, the head ref and the base ref should be read from the event JSON file. Ideally, this should also apply to any other values specified in the file, conforming with GitHub's [event properties](https://docs.github.com/en/developers/webhooks-and-events/events/github-event-types)).

# How to reproduce

### 1. Executed the command

```sh
act \
    --platform=self-hosted=node:16-bullseye-slim \
    --container-architecture=linux/amd64 \
    --secret-file=/path/to/my-custom-secrets.secrets \
    --env-file=/path/to/my-custom-env.env \
    --eventpath=/path/to/my-custom-event.json \
    --actor=my-actor-name \
    --job=my-custom-job \
    --artifact-server-path=localhost \
    --artifact-server-port=34567 \
    --verbose
```

### 2. Provide content of the event file (`my-custom-event.json`)

```json
{
  "action": "opened",
  "pull_request": {
    "head": {
      "ref": "my-topic-branch"
    },
    "base": {
      "ref": "main"
    }
  }
}
```

### 3. Verify environment setup in the command's output

<details>
<summary>Expand  (collapsed due to verbosity of output)</summary>

```
[my-custom-job] [DEBUG] setupEnv => map[
	ACT: true
	ACTIONS_RUNTIME_TOKEN: token
	ACTIONS_RUNTIME_URL: http://192.168.1.5:34567/
	CI: true
	GITHUB_ACTION: 4
	GITHUB_ACTIONS:true
	GITHUB_ACTION_PATH: /var/run/act/actions/my-private-company-my-private-github-actions-repo-my-custom-build-action@v123/my-custom-build-action
	GITHUB_ACTION_REF: 
	GITHUB_ACTION_REPOSITORY: 
	GITHUB_ACTOR: my-actor-name
	GITHUB_API_URL: https://api.github.com
(1)	GITHUB_BASE_REF: 
	GITHUB_ENV: /var/run/act/workflow/envs.txt
(2)	GITHUB_EVENT_NAME: push
	GITHUB_EVENT_PATH: /var/run/act/workflow/event.json
	GITHUB_GRAPHQL_URL: https://api.github.com/graphql
(3)	GITHUB_HEAD_REF: 
	GITHUB_JOB: my-custom-job
	GITHUB_PATH: /var/run/act/workflow/paths.txt
(4)	GITHUB_REF: refs/heads/main
	GITHUB_REF_NAME: main
	GITHUB_REF_TYPE: branch
	GITHUB_REPOSITORY: my-private-company/my-private-repo
	GITHUB_REPOSITORY_OWNER: my-private-company
	GITHUB_RETENTION_DAYS: 0
	GITHUB_RUN_ID: 1
	GITHUB_RUN_NUMBER: 1
	GITHUB_SERVER_URL: https://github.com
	GITHUB_SHA: my-main-branch-git-sha-long
	GITHUB_TOKEN: ***
	GITHUB_WORKFLOW: my-custom-workflow
	GITHUB_WORKSPACE: /path/to/my-private-repo
	GIT_SHA_LONG: my-custom-branch-git-sha-long
	NODE_VERSION: 16.15.0
	PATH: /opt/hostedtoolcache/node/16.17.1/x64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
	RUNNER_PERFLOG: /dev/null
	RUNNER_TRACKING_ID: 
	YARN_VERSION: 1.22.18
]
```

References:

(1) GH base ref not read from file! Expected value -> "main"
(2) GH event name not read from file and defaulted to "push"
(3) GH head ref not read from file! Expected value -> "my-topic-branch"
(4) GH ref not read from file! Expected value -> "my-topic-branch"
</details>

# Further symptoms of the issue

One of the steps of the workflow needs to only run when building the "main" branch after a "push" event. The event file specifies the "my-topic-branch" branch and the "pull_request" event, but then the following check succeeds when it should fail:

```
DEBU[0514] evaluating expression 'github.ref == 'refs/heads/main' && github.event_name == 'push''
DEBU[0514] expression 'github.ref == 'refs/heads/main' && github.event_name == 'push'' evaluated to 'true'
```

# Related issues

https://github.com/nektos/act/issues/671


Discussion / hints:
Please read this
https://github.com/nektos/act#example-commands
Event name is not part of a webhook payload, it is part of http headers.
All what can be done is guessing, especially if the user omit properties. In your example it is impossible to distinguish pull_request from pull_request_target etc..

You are supposed to call `act pull_request ...`

I agree, the other values could be mapped.
@ChristopherHX If it is ok with you, I can open another issue just referring to head ref and base ref values from the file not being mapped.
No head ref and base ref is working https://github.com/nektos/act/blob/41b03b581c7aec251d96e9a5ea27009542a798d4/pkg/runner/testdata/pull-request/main.yaml and has a Test.
It is even explained in the ReadMe file. The push trigger don't have a value for head ref and base ref, but that's expected.

@ChristopherHX Are you saying that the mapping of head ref and base ref from the file specified via `--eventpath` is working? I don't see that reflected in what Act logs, when running in verbose mode.

The command I'm running is:

```sh
act \
    pull_request \
    --platform=self-hosted=node:16-bullseye-slim \
    --container-architecture=linux/amd64 \
    --secret-file=/path/to/secrets.secrets \
    --env-file=/path/to/env.env \
    --eventpath=/path/to/event.json \
    --actor=my-gh-user \
    --job=my-build-job \
    --artifact-server-path=localhost \
    --artifact-server-port=34567 \
    --verbose
```

And my `event.json` file is:

```json
{
  "pull_request": {
    "head": {
      "ref": "my-topic-branch"
    },
    "base": {
      "ref": "main"
    }
  }
}
```

But then, Act logs this:

```
[my-build-job] [DEBUG] setupEnv => map[
	ACT:true
	ACTIONS_RUNTIME_TOKEN:token
	ACTIONS_RUNTIME_URL:http://192.168.103.209:34567/
	CI:true
	GITHUB_ACTION:3
	GITHUB_ACTIONS:true
	GITHUB_ACTION_PATH:/var/run/act/actions/***
	GITHUB_ACTION_REF: 
	GITHUB_ACTION_REPOSITORY: 
	GITHUB_ACTOR:my-gh-user
	GITHUB_API_URL:https://api.github.com
(*)	GITHUB_BASE_REF: 
	GITHUB_ENV:/var/run/act/workflow/envs.txt
	GITHUB_EVENT_NAME:pull_request
	GITHUB_EVENT_PATH:/var/run/act/workflow/event.json
	GITHUB_GRAPHQL_URL:https://api.github.com/graphql
(*)	GITHUB_HEAD_REF: 
	GITHUB_JOB:my-build-job
	GITHUB_PATH:/var/run/act/workflow/paths.txt
	GITHUB_REF:refs/pull/%!f(<nil>)/merge
	GITHUB_REF_NAME: 
	GITHUB_REF_TYPE: 
	GITHUB_REPOSITORY:my-gh-repo
	GITHUB_REPOSITORY_OWNER:my-company
	GITHUB_RETENTION_DAYS:0
	GITHUB_RUN_ID:1
	GITHUB_RUN_NUMBER:1
	GITHUB_SERVER_URL:https://github.com
	GITHUB_SHA:main-branch-sha
	GITHUB_TOKEN:***
	GITHUB_WORKFLOW:my-workflow
	GITHUB_WORKSPACE:/path/to/my-project
	GIT_SHA_LONG:my-topic-branch-sha
	NODE_VERSION:16.15.0
	PATH:/opt/hostedtoolcache/node/16.17.1/x64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
	RUNNER_PERFLOG:/dev/null
	RUNNER_TRACKING_ID: 
	YARN_VERSION:1.22.18
]
```

You can see that `GITHUB_BASE_REF` and `GITHUB_HEAD_REF` are empty, so they are not being mapped.
Notice that I'm NOT using the push trigger. I'm actually using the pull_request trigger and I'm using the same structure for the event JSON file as the one provided by the Act docs.
Yes I say it is working and I have no idea why it shouldn't work for you.

I can see this on my computer and in the actions checks on github:

I used the latest release from the releases page

`act.exe pull_request -W w.yml -e ev.json`
```
[w.yml/_] 🚀  Start image=ubuntu:latest
[w.yml/_]   🐳  docker pull image=ubuntu:latest platform= username= forcePull=false
[w.yml/_]   🐳  docker create image=ubuntu:latest platform= entrypoint=["/usr/bin/tail" "-f" "/dev/null"] cmd=[]
[w.yml/_]   🐳  docker run image=ubuntu:latest platform= entrypoint=["/usr/bin/tail" "-f" "/dev/null"] cmd=[]
[w.yml/_] unable to get git repo: unable to find git repo
[w.yml/_] unable to get git revision: unable to find git repo
[w.yml/_] unable to get git repo: unable to find git repo
[w.yml/_] unable to get git revision: unable to find git repo
[w.yml/_] unable to get git repo: unable to find git repo
[w.yml/_] unable to get git revision: unable to find git repo
[w.yml/_] ⭐ Run Main env
[w.yml/_] unable to get git repo: unable to find git repo
[w.yml/_] unable to get git revision: unable to find git repo
[w.yml/_] unable to get git repo: unable to find git repo
[w.yml/_] unable to get git revision: unable to find git repo
[w.yml/_] unable to get git repo: unable to find git repo
[w.yml/_] unable to get git revision: unable to find git repo
[w.yml/_]   🐳  docker exec cmd=[sh -e -c /var/run/act/workflow/0.sh] user= workdir=
| GITHUB_TOKEN=
| CI=true
| HOSTNAME=docker-desktop
| GITHUB_ENV=/var/run/act/workflow/envs.txt
| HOME=/root
| RUNNER_TEMP=/tmp
| GITHUB_EVENT_PATH=/var/run/act/workflow/event.json
| ACT=true
| GITHUB_REPOSITORY_OWNER=
| GITHUB_RETENTION_DAYS=0
| GITHUB_HEAD_REF=my-topic-branch
| GITHUB_GRAPHQL_URL=https://api.github.com/graphql
| RUNNER_OS=Linux
| GITHUB_API_URL=https://api.github.com
| GITHUB_WORKFLOW=w.yml
| TERM=xterm
| GITHUB_RUN_ID=1
| GITHUB_REF_TYPE=
| ImageOS=ubuntu20
| GITHUB_BASE_REF=main
| GITHUB_ACTION_REPOSITORY=
| PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin":/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
| RUNNER_TOOL_CACHE=/opt/hostedtoolcache
| RUNNER_TRACKING_ID=
| RUNNER_ARCH=X64
| GITHUB_RUN_NUMBER=1
| GITHUB_ACTION=0
| GITHUB_REF_NAME=
| GITHUB_REPOSITORY=
| GITHUB_ACTION_REF=
| GITHUB_ACTIONS=true
| GITHUB_ACTION_PATH=
| GITHUB_JOB=_
| GITHUB_WORKSPACE=/mnt/c/Users/User/Downloads/act_Windows_x86_64 (1)
| RUNNER_PERFLOG=/dev/null
| GITHUB_SHA=
| GITHUB_ACTOR=nektos/act
| GITHUB_REF=refs/pull/%!f(<nil>)/merge
| GITHUB_PATH=/var/run/act/workflow/paths.txt
| PWD=/mnt/c/Users/User/Downloads/act_Windows_x86_64 (1)
| GITHUB_SERVER_URL=https://github.com
| GITHUB_EVENT_NAME=pull_request
[w.yml/_]   ✅  Success - Main env
[w.yml/_] unable to get git repo: unable to find git repo
[w.yml/_] unable to get git revision: unable to find git repo
[w.yml/_] unable to get git repo: unable to find git repo
[w.yml/_] unable to get git revision: unable to find git repo
[w.yml/_] unable to get git repo: unable to find git repo
[w.yml/_] unable to get git revision: unable to find git repo
[w.yml/_] ⭐ Run Main echo ${{ github.head_ref }}
[w.yml/_] unable to get git repo: unable to find git repo
[w.yml/_] unable to get git revision: unable to find git repo
[w.yml/_] unable to get git repo: unable to find git repo
[w.yml/_] unable to get git revision: unable to find git repo
[w.yml/_] unable to get git repo: unable to find git repo
[w.yml/_] unable to get git revision: unable to find git repo
[w.yml/_]   🐳  docker exec cmd=[sh -e -c /var/run/act/workflow/1.sh] user= workdir=
| my-topic-branch
[w.yml/_]   ✅  Success - Main echo ${{ github.head_ref }}
[w.yml/_] 🏁  Job succeeded
[w.yml/_] unable to get git repo: unable to find git repo
[w.yml/_] unable to get git revision: unable to find git repo
```

workflow:
```yaml
on:
  pull_request:
  push:
jobs:
  _:
    runs-on: ubuntu-latest
    container: ubuntu:latest
    steps:
    - run: env
    - run: echo ${{ github.head_ref }}
```

_ev.json is your json file_
@ChristopherHX I really appreciate you taking the time to see into this.

I think I managed now to reproduce why I can't access the `github.head_ref` and `github.head_ref` variables.

The workflow and action I was testing with were quite lengthy and complex, so I tried to reproduce the issue with a more simplified version of it. In my case, I have reusable workflows and actions called from within the main workflow. Since Act explicitly doesn't support reusable workflows, the new attempt to reproduce the issue includes only actions called from within a workflow.

## Issue

It looks like Act does not propagate these values from a workflow to an action used within it.

## How to reproduce

- Command:

```js
act \
	pull_request \
	--workflows=./.github/workflows/main-workflow.yml \
	--platform=self-hosted=node:16-buster-slim \
	--container-architecture=linux/amd64 \
	--eventpath=./event.json \
```

- Workflow file:

```yml
name: "Main Workflow"

on:
  pull_request:
  push:
jobs:
  _1:
    runs-on: ubuntu-latest
    container: ubuntu:latest
    steps:
      - uses: actions/checkout@v2

      - name: "Step 1: log env vars"
        run: env | grep "GITHUB_HEAD_REF\|GITHUB_BASE_REF"

      - name: "Step 2: log GH HEAD Ref"
        run: echo "github.head_ref -> ${{ github.head_ref }}"

      - name: "Step 3: log GH BASE Ref"
        run: echo "github.base_ref -> ${{ github.base_ref }}"

      - name: "Step 4: run test action"
        uses: ./.github/actions/test-action
```

- Action file:

```yml
name: "Test Action"
description: "A test action..."

runs:
  using: composite

  steps:
    - name: "Step 1: log env vars"
      shell: bash
      run: env | grep "GITHUB_HEAD_REF\|GITHUB_BASE_REF"

    - name: "Step 2: log GH HEAD Ref"
      shell: bash
      run: echo "github.head_ref -> ${{ github.head_ref }}"

    - name: "Step 3: log GH BASE Ref"
      shell: bash
      run: echo "github.base_ref -> ${{ github.base_ref }}"
```

- Event file:

```json
{
  "pull_request": {
    "head": {
      "ref": "my-topic-branch"
    },
    "base": {
      "ref": "main"
    }
  }
}
```

- Output:

```
[Main Workflow/_1] 🚀  Start image=ubuntu:latest
[Main Workflow/_1]   🐳  docker pull image=ubuntu:latest platform=linux/amd64 username= forcePull=false
[Main Workflow/_1]   🐳  docker create image=ubuntu:latest platform=linux/amd64 entrypoint=["/usr/bin/tail" "-f" "/dev/null"] cmd=[]
[Main Workflow/_1]   🐳  docker run image=ubuntu:latest platform=linux/amd64 entrypoint=["/usr/bin/tail" "-f" "/dev/null"] cmd=[]
[Main Workflow/_1] ⭐ Run Main actions/checkout@v2
[Main Workflow/_1]   🐳  docker cp src=/Users/cwr/Development/act-test/. dst=/Users/cwr/Development/act-test
[Main Workflow/_1]   ✅  Success - Main actions/checkout@v2
[Main Workflow/_1] ⭐ Run Main Step 1: log env vars
[Main Workflow/_1]   🐳  docker exec cmd=[sh -e -c /var/run/act/workflow/1.sh] user= workdir=
| GITHUB_BASE_REF=main
| GITHUB_HEAD_REF=my-topic-branch
[Main Workflow/_1]   ✅  Success - Main Step 1: log env vars
[Main Workflow/_1] ⭐ Run Main Step 2: log GH HEAD Ref
[Main Workflow/_1]   🐳  docker exec cmd=[sh -e -c /var/run/act/workflow/2.sh] user= workdir=
| github.head_ref -> my-topic-branch
[Main Workflow/_1]   ✅  Success - Main Step 2: log GH HEAD Ref
[Main Workflow/_1] ⭐ Run Main Step 3: log GH BASE Ref
[Main Workflow/_1]   🐳  docker exec cmd=[sh -e -c /var/run/act/workflow/3.sh] user= workdir=
| github.base_ref -> main
[Main Workflow/_1]   ✅  Success - Main Step 3: log GH BASE Ref
[Main Workflow/_1] ⭐ Run Main Step 4: run test action
[Main Workflow/_1] ⭐ Run Main Step 1: log env vars
[Main Workflow/_1]   🐳  docker exec cmd=[bash --noprofile --norc -e -o pipefail /var/run/act/workflow/4-composite-0.sh] user= workdir=
| GITHUB_HEAD_REF=
| GITHUB_BASE_REF=
[Main Workflow/_1]   ✅  Success - Main Step 1: log env vars
[Main Workflow/_1] ⭐ Run Main Step 2: log GH HEAD Ref
[Main Workflow/_1]   🐳  docker exec cmd=[bash --noprofile --norc -e -o pipefail /var/run/act/workflow/4-composite-1.sh] user= workdir=
| github.head_ref -> 
[Main Workflow/_1]   ✅  Success - Main Step 2: log GH HEAD Ref
[Main Workflow/_1] ⭐ Run Main Step 3: log GH BASE Ref
[Main Workflow/_1]   🐳  docker exec cmd=[bash --noprofile --norc -e -o pipefail /var/run/act/workflow/4-composite-2.sh] user= workdir=
| github.base_ref -> 
[Main Workflow/_1]   ✅  Success - Main Step 3: log GH BASE Ref
[Main Workflow/_1]   ✅  Success - Main Step 4: run test action
[Main Workflow/_1] ⭐ Run Post Step 4: run test action
[Main Workflow/_1]   ✅  Success - Post Step 4: run test action
[Main Workflow/_1] 🏁  Job succeeded
```

You can see in the output that the action invoked from the workflow cannot access the `github.head_ref` and `github.head_ref` variables.
I guess the whole github.event is blank in the composite actions?

EventJSON is discarded here:
https://github.com/nektos/act/blob/bc1737101793f711ee3f4af48304a071603dad7f/pkg/runner/action_composite.go#L52-L72

I didn't refactor composite actions, it is possible that this bug is not present act v0.2.25.
Thank you very much again @ChristopherHX!

Fix patch:
diff --git a/pkg/runner/action_composite.go b/pkg/runner/action_composite.go
index 9001b489505..a7b41438782 100644
--- a/pkg/runner/action_composite.go
+++ b/pkg/runner/action_composite.go
@@ -69,6 +69,7 @@ func newCompositeRunContext(ctx context.Context, parent *RunContext, step action
 		Masks:        parent.Masks,
 		ExtraPath:    parent.ExtraPath,
 		Parent:       parent,
+		EventJSON:    parent.EventJSON,
 	}
 
 	return compositerc


Test patch:
diff --git a/pkg/runner/testdata/pull-request/main.yaml b/pkg/runner/testdata/pull-request/main.yaml
index b9946b94e11..eb81939b4db 100644
--- a/pkg/runner/testdata/pull-request/main.yaml
+++ b/pkg/runner/testdata/pull-request/main.yaml
@@ -5,6 +5,22 @@ jobs:
   build:
     runs-on: ubuntu-latest
     steps:
-      - run: echo '${{github.ref}}' 
+      # test refs from event.json
+      - run: echo '${{github.ref}}'
       - run: echo '${{github.head_ref}}' | grep sample-head-ref
       - run: echo '${{github.base_ref}}' | grep sample-base-ref
+      # test main/composite context equality with data from event.json
+      - run: |
+          runs:
+            using: composite
+            steps:
+            - run: |
+                echo WORKFLOW_GITHUB_CONTEXT="$WORKFLOW_GITHUB_CONTEXT"
+                echo COMPOSITE_GITHUB_CONTEXT="$COMPOSITE_GITHUB_CONTEXT"
+                [[ "$WORKFLOW_GITHUB_CONTEXT" = "$COMPOSITE_GITHUB_CONTEXT" ]]
+              env:
+                WORKFLOW_GITHUB_CONTEXT: ${{ tojson(tojson(github.event)) }}
+                COMPOSITE_GITHUB_CONTEXT: ${{ '${{tojson(github.event)}}' }}
+              shell: bash
+        shell: cp {0} action.yml
+      - uses: ./