go-gitea/gitea

Missing github.event.schedule when schedule event is triggered

Opened this issue · 1 comments

Description

github.event.schedule is not populated when a schedule event is triggered.

Here is an example workflow :

name: Gitea Actions Demo
on:
  schedule:
    - cron: "*/5 * * * *"
    - cron: "37 12 5 1 2"
  workflow_dispatch:
jobs:
  debug:
    runs-on: ubuntu-latest
    steps:
      - name: Context
        run: |
         cat <<EOF
         github: ${{ toJSON(github) }}
         EOF
  job1:
    runs-on: ubuntu-latest
    if: ${{ github.event.schedule == '*/5 * * * *' }}
    steps:
      - run: echo "This is job1"

When the workflow is run by a schedule event, here is the content on github context:

github: {
  "event": {
    "after": "xxx",
    "before": "xxx",
    "commits": [
      ...
    ],
    "compare_url": "xxx",
    "head_commit": {
      ...
    },
    "pusher": {
      ...
    },
    "ref": "refs/heads/main",
    "repository": {
      ...
    },
    "sender": {
      ...
    },
    "total_commits": 0
  },
  "event_path": "/var/run/act/workflow/event.json",
  "workflow": "Gitea Actions Demo",
  "run_id": "712",
  "run_number": "373",
  "actor": "gitea-actions",
  "repository": "xxx/xxx",
  "event_name": "schedule",
  "sha": "e9b971279dabae3a80d26b603209d330cc979274",
  "ref": "refs/heads/main",
  "ref_name": "main",
  "ref_type": "branch",
  "head_ref": "",
  "base_ref": "",
  "token": "***",
  "workspace": "/workspace/xxx/action-test",
  "action": "0",
  "action_path": "",
  "action_ref": "",
  "action_repository": "",
  "job": "debug",
  "job_name": "",
  "repository_owner": "xxx",
  "retention_days": "",
  "runner_perflog": "/dev/null",
  "runner_tracking_id": "",
  "server_url": "xxx",
  "api_url": "xxx",
  "graphql_url": ""
}

I expect to have a github.event.schedule entry (https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#schedule). This allow condition on cron entry like:

  job1:
    runs-on: ubuntu-latest
    if: ${{ github.event.schedule == '*/5 * * * *' }}
    steps:
      - run: echo "This is job1"

Currently we can't use such condition.

Gitea Version

1.24.5

Can you reproduce the bug on the Gitea demo site?

No

Log Gist

No response

Screenshots

No response

Git Version

No response

Operating System

Debian 12

How are you running Gitea?

Gitea installed on Debian 12 using official release (https://dl.gitea.com/gitea/1.24.5/gitea-1.24.5-linux-amd64).

Act runner installed on Debian 12 using official release (https://dl.gitea.com/act_runner/0.2.12/act_runner-0.2.12-linux-amd64). Docker in rootless mode.

Database

MySQL/MariaDB

Hi,

I think some code must be added services/actions/schedule_tasks.go.

The CreateScheduleTask function accept a ActionSchedule object as argument which contains the payload that will be used to build the event entry of github context. But this object does not has the cron specification which triggered the execution of the workflow.

The startTasks function has the cron specification (a ActionScheduleSpec object) and call CreateScheduleTask which providing it.

To fix this issue, we can replace the line 87 with:

			if err := CreateScheduleTask(ctx, row); err != nil {

And change the beginning os CreateScheduleTask as such:

func CreateScheduleTask(ctx context.Context, spec *actions_model.ActionScheduleSpec) error {
	cron := spec.Schedule
	payload := cron.EventPayload
	//  TODO Change `payload` to add the `schedule` property using `spec.spec` as value

	// Create a new action run based on the schedule
	run := &actions_model.ActionRun{
		Title:         cron.Title,
		RepoID:        cron.RepoID,
		OwnerID:       cron.OwnerID,
		WorkflowID:    cron.WorkflowID,
		TriggerUserID: cron.TriggerUserID,
		Ref:           cron.Ref,
		CommitSHA:     cron.CommitSHA,
		Event:         cron.Event,
		EventPayload:  payload,
		TriggerEvent:  string(webhook_module.HookEventSchedule),
		ScheduleID:    cron.ID,
		Status:        actions_model.StatusWaiting,
	}

	...

I don't know the right way to change the payload string so there's still some code to write.