Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actions support workflow dispatch event #28163

Open
wants to merge 59 commits into
base: main
Choose a base branch
from

Conversation

pangliang
Copy link

@pangliang pangliang commented Nov 22, 2023

fix #23668

My plan:

  • In the actions.list method, if workflow is selected and IsAdmin, check whether the on event contains workflow_dispatch. If so, display a Run workflow button to allow the user to manually trigger the run.
  • Providing a form that allows users to select target brach or tag, and these parameters can be configured in yaml
  • Simple form validation, required input cannot be empty
  • Add a route /actions/run, and an actions.Run method to handle
  • Add WorkflowDispatchPayload struct to pass the Webhook event payload to the runner when triggered, this payload carries the inputs values and other fields, doc: workflow_dispatch payload

Other PRs

  • the Workflow.WorkflowDispatchConfig() method still return non-nil when workflow_dispatch is not defined. I submitted a PR https://gitea.com/gitea/act/pulls/85 to fix it. Still waiting for them to process.

Behavior should be same with github, but may cause confusion. Here's a quick reminder.

  • Doc Said: This event will only trigger a workflow run if the workflow file is on the default branch.
    • If the workflow yaml file only exists in a non-default branch, it cannot be triggered. (It will not even show up in the workflow list)
    • If the same workflow yaml file exists in each branch at the same time, the version of the default branch is used. Even if Use workflow from selects another branch

image

name: Docker Image CI

on:
  workflow_dispatch:
    inputs:
      logLevel:
        description: 'Log level'
        required: true
        default: 'warning'
        type: choice
        options:
        - info
        - warning
        - debug
      tags:
        description: 'Test scenario tags'
        required: false
        type: boolean
      boolean_default_true:
        description: 'Test scenario tags'
        required: true
        type: boolean
        default: true
      boolean_default_false:
        description: 'Test scenario tags'
        required: false
        type: boolean
        default: false
      environment:
        description: 'Environment to run tests against'
        type: environment
        required: true
        default: 'environment values'
      number_required_1:
        description: 'number '
        type: number
        required: true
        default: '100'
      number_required_2:
        description: 'number'
        type: number
        required: true
        default: '100'
      number_required_3:
        description: 'number'
        type: number
        required: true
        default: '100'
      number_1:
        description: 'number'
        type: number
        required: false
      number_2:
        description: 'number'
        type: number
        required: false
      number_3:
        description: 'number'
        type: number
        required: false

env:
  inputs_logLevel:              ${{ inputs.logLevel }}
  inputs_tags:                  ${{ inputs.tags }}
  inputs_boolean_default_true:  ${{ inputs.boolean_default_true }}
  inputs_boolean_default_false: ${{ inputs.boolean_default_false }}
  inputs_environment:           ${{ inputs.environment }}
  inputs_number_1:              ${{ inputs.number_1  }}
  inputs_number_2:              ${{ inputs.number_2  }}
  inputs_number_3:              ${{ inputs.number_3  }}
  inputs_number_required_1:     ${{ inputs.number_required_1  }}
  inputs_number_required_2:     ${{ inputs.number_required_2  }}
  inputs_number_required_3:     ${{ inputs.number_required_3  }}

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: ls -la
      - run: env | grep inputs
      - run: echo ${{ inputs.logLevel }}
      - run: echo ${{ inputs.boolean_default_false }}

image
image

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Nov 22, 2023
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 22, 2023
@wizpresso-steve-cy-fan
Copy link

Well, the problem is, you can specify some user-supplied fields for workflow_dispatch, have you handled that case as well?

@lunny lunny added the topic/gitea-actions related to the actions of Gitea label Nov 23, 2023
@pangliang
Copy link
Author

Well, the problem is, you can specify some user-supplied fields for workflow_dispatch, have you handled that case as well?

I plan to implement a version similar to github, providing a form that allows users to fill in the env parameters of the workflow run, and these parameters can be configured in yaml. Hopefully I can complete it

@pangliang pangliang force-pushed the actions_support_workflow_dispatch_event branch from 170e929 to b3b5544 Compare November 29, 2023 16:43
@pangliang pangliang changed the title WIP: Actions support workflow dispatch event Actions support workflow dispatch event Nov 29, 2023
@yp05327
Copy link
Contributor

yp05327 commented Nov 30, 2023

https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_dispatch
image
Should we also follow this note?

@yp05327
Copy link
Contributor

yp05327 commented Nov 30, 2023

In the actions.list method, if workflow is selected and IsAdmin, check whether the on event contains workflow_dispatch. If so, display a Run workflow button to allow the user to manually trigger the run.

IMO, this notification takes too many spaces, and we don't need to always display it at there?
How about put it into the dropdown menu?
image

Not a block, feel free to discus.

@pangliang pangliang changed the title Actions support workflow dispatch event WIP: Actions support workflow dispatch event Nov 30, 2023
@pull-request-size pull-request-size bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Dec 5, 2023
@pangliang pangliang force-pushed the actions_support_workflow_dispatch_event branch from 2f14a4a to 3bec7d1 Compare December 5, 2023 05:24
@pull-request-size pull-request-size bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Dec 5, 2023
@pangliang
Copy link
Author

https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_dispatch image Should we also follow this note?

I think it should be following now.

  • When list workflow, DefaultBranch is used
  • When running workflow_dispatch workflow, the target ref is selected and specified, but the workflow configuration still uses DefaultBranch

@pangliang
Copy link
Author

In the actions.list method, if workflow is selected and IsAdmin, check whether the on event contains workflow_dispatch. If so, display a Run workflow button to allow the user to manually trigger the run.

IMO, this notification takes too many spaces, and we don't need to always display it at there? How about put it into the dropdown menu? image

Not a block, feel free to discus.

At first I also tried to put it in the dropdown menu, but there was a confusing problem. When no child elements exist, the dropdown menu will not hide automatically. Then it is more difficult to deal with when to display the button

if a or b or c or .... then
   <dropdown>
          if a then
              <menu a>
          end
          if b then
              <menu b>
          end
          ...
   </dropdown>
end

I don't know, I feel a little OCD here

Maybe I'm using it the wrong way, so I copied the design from github
If you have a good solution please tell me, Or I can put it in dropdown. After all, there are only two if conditions now.

@pangliang pangliang changed the title WIP: Actions support workflow dispatch event Actions support workflow dispatch event Dec 5, 2023
go.mod Outdated Show resolved Hide resolved
@yp05327
Copy link
Contributor

yp05327 commented Dec 7, 2023

image
It is a required input, but it is empty.

go.mod Outdated Show resolved Hide resolved
routers/web/repo/actions/actions.go Outdated Show resolved Hide resolved
@yp05327
Copy link
Contributor

yp05327 commented Dec 7, 2023

  • environment: gitea doesn't support the environment feature yet, right? Maybe act_runner uses environment as a keyword, so it can't get the value of inputs.environment

gitea doesn't support the environment feature yet, right? Maybe act_runner uses environment as a keyword, so it can't get the value of inputs.environment

If I set a default value, it works. Maybe it is caused by something else.
image
image

And if I change the default value, it will still be the default value.
So it seems that it will only display the default value, but not the value in the form.

@silverwind
Copy link
Member

I will give this UI another test. There are some minor tweaks left to do.

@denyskon denyskon mentioned this pull request Apr 5, 2024
13 tasks
@kirill-shtrykov
Copy link

kirill-shtrykov commented Apr 6, 2024

Is it possible to include workflows from all branches in the list?
Additionally, after specifying which branch to launch the workflow from, the inputs and the entire workflow aren't pulled from this branch.

@yp05327
Copy link
Contributor

yp05327 commented Apr 8, 2024

@pangliang
can you link this PR to 23668 correctly?
use fix or other keywords instead of related

@lunny lunny added this to the 1.23.0 milestone Apr 11, 2024
@Renerick
Copy link

Is API endpoint for workflow_dispatch planned?

@0xa1d0
Copy link

0xa1d0 commented Apr 13, 2024

Is this coming in any time soon? 😁

@pandatix
Copy link

Is this coming in any time soon? 😁

Planned for 1.23 given the milestones, still waiting for 1.22 so not tomorrow but I hope not too long too !

type WorkflowDispatchPayload struct {
Workflow string `json:"workflow"`
Ref string `json:"ref"`
Inputs map[string]any `json:"inputs"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to keep the sequences?

Copy link
Author

@pangliang pangliang Apr 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Workflow Dispatch Payload is to transmit data to act_runner, and the WorkflowDispatch type in nektos/act uses the kv structure. workflow.go#L77

To maintain order when displaying the inputs form, need to create a separate new same type for the view and use an array to store the Inputs. Because I think it will not be accepted to directly modify the kv of nektos/act into an array.
Previously discussed here: (issuecomment-1859484485)[https://github.com//pull/28163#issuecomment-1859484485]

in short:

  • New WorkflowDispatchInput WorkflowDispatch and workflowDispatchConfig in gitea/routes/web/repo/actions/view.go, to maintain the order of inputs in forms
  • Keep the kv of WorkflowDispatchPayload unchanged to adapt to nektos/act

What do you think? If you think it is feasible, I will modify it like this.

return
}
// always put default branch on the top if it exists
if slices.Contains(branches, ctx.Repo.Repository.DefaultBranch) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the first branch is not default branch.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic seems to be to ensure that the default branch is first; reference from: repo.go#L687

…low_dispatch_event

* origin/main: (62 commits)
  Add test for go-gitea#30674 (go-gitea#30679)
  Fix border-radius of header+segment boxes (go-gitea#30667)
  Fix a panic bug when head repository deleting (go-gitea#30674)
  Fix some bug on migrations (go-gitea#30647)
  Fix checkbox field markup (go-gitea#30666)
  Avoid doubled border for the PR info segment (go-gitea#30663)
  Interpolate runs-on with variables when scheduling tasks (go-gitea#30640)
  Initial support for colorblindness-friendly themes (go-gitea#30625)
  Fix flash message for flex-container (go-gitea#30657)
  Perform Newest sort type correctly when sorting issues (go-gitea#30644)
  Fix project name wrapping, remove horizontal margin on header (go-gitea#30631)
  Add a db consistency check to remove runners that do not belong to a repository (go-gitea#30614)
  Fix wrong table name (go-gitea#30557)
  Fix compare api swagger (go-gitea#30648)
  [skip ci] Updated translations via Crowdin
  Fix queue test (go-gitea#30646)
  Enable jquery-related eslint rules that have no violations (go-gitea#30632)
  Enable more `revive` linter rules (go-gitea#30608)
  Remove obsolete CSS text classes (go-gitea#30576)
  Hide diff stats on empty PRs (go-gitea#30629)
  ...
@silverwind
Copy link
Member

silverwind commented Apr 24, 2024

The branch selector in this PR was very broken, I patched it up now to be similar to the one in the issue list. It did require a few JS changes unfortunately because the one in the issue list was written in a very non-reusable fashion.

Screenshot 2024-04-25 at 01 04 51

There is more to do on the form fields, I will check later.

@yp05327
Copy link
Contributor

yp05327 commented Apr 25, 2024

Have you tested long branch name?

@silverwind
Copy link
Member

silverwind commented Apr 25, 2024

I will, but the overflow: hidden css is active, so I assume it will work. This CSS has one issue with cutting off g letters thought, likely I'll fix that in another PR.

@silverwind
Copy link
Member

silverwind commented May 1, 2024

Branch selector will need more adjustments after #30803 merges. Sorry to hold this PR off, but it's basically previous contributor's fault for leaving it in such a messy and non-reusable state.

@ghnp5
Copy link

ghnp5 commented May 1, 2024

it's basically previous contributor's fault

No problem for holding the PR, and if things have to be improved, then things have to be improved.

But it's not nice to put a blame like that to someone who is just trying to help and contribute... for free.

Even if it's "technically true", no one is born knowing all the proper design patterns and all codebase's standards, etc...

To every contributor -- thank you for improving Gitea 🧡

DennisRasey pushed a commit to DennisRasey/forgejo that referenced this pull request Jun 28, 2024
Closes #2797

I'm aware of go-gitea/gitea#28163 exists, but since I had it laying around on my drive and collecting dust, I might as well open a PR for it if anyone wants the feature a bit sooner than waiting for upstream to release it or to be a forgejo "native" implementation.

This PR Contains:
- Support for the `workflow_dispatch` trigger
- Inputs: boolean, string, number, choice

Things still to be done:
- [x] API Endpoint `/api/v1/<org>/<repo>/actions/workflows/<workflow id>/dispatches`
- ~~Fixing some UI bugs I had no time figuring out, like why dropdown/choice inputs's menu's behave weirdly~~ Unrelated visual bug with dropdowns inside dropdowns
- [x] Fix bug where opening the branch selection submits the form
- [x] Limit on inputs to render/process

Things not in this PR:
- Inputs: environment (First need support for environments in forgejo)

Things needed to test this:
- A patch for https://code.forgejo.org/forgejo/runner to actually consider the inputs inside the workflow.
  ~~One possible patch can be seen here: https://code.forgejo.org/Mai-Lapyst/runner/src/branch/support-workflow-inputs~~
  [PR](https://code.forgejo.org/forgejo/runner/pulls/199)

![image](/attachments/2db50c9e-898f-41cb-b698-43edeefd2573)

## Testing

- Checkout PR
- Setup new development runner with [this PR](https://code.forgejo.org/forgejo/runner/pulls/199)
- Create a repo with a workflow (see below)
- Go to the actions tab, select the workflow and see the notice as in the screenshot above
- Use the button + dropdown to run the workflow
  - Try also running it via the api using the `` endpoint
- ...
- Profit!

<details>
<summary>Example workflow</summary>

```yaml
on:
  workflow_dispatch:
    inputs:
      logLevel:
        description: 'Log Level'
        required: true
        default: 'warning'
        type: choice
        options:
        - info
        - warning
        - debug
      tags:
        description: 'Test scenario tags'
        required: false
        type: boolean
      boolean_default_true:
        description: 'Test scenario tags'
        required: true
        type: boolean
        default: true
      boolean_default_false:
        description: 'Test scenario tags'
        required: false
        type: boolean
        default: false
      number1_default:
        description: 'Number w. default'
        default: '100'
        type: number
      number2:
        description: 'Number w/o. default'
        type: number
      string1_default:
        description: 'String w. default'
        default: 'Hello world'
        type: string
      string2:
        description: 'String w/o. default'
        required: true
        type: string

jobs:
  test:
    runs-on: docker
    steps:
      - uses: actions/checkout@v3
      - run: whoami
      - run: cat /etc/issue
      - run: uname -a
      - run: date
      - run: echo ${{ inputs.logLevel }}
      - run: echo ${{ inputs.tags }}
      - env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"
      - run: echo "abc"
```
</details>

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/3334
Reviewed-by: Earl Warren <[email protected]>
Co-authored-by: Mai-Lapyst <[email protected]>
Co-committed-by: Mai-Lapyst <[email protected]>
@bencurio
Copy link

bencurio commented Jul 2, 2024

I suggests adding a new API endpoint to facilitate the dispatching of workflows. It empowers developers and teams to automate repetitive tasks and orchestrate complex workflows efficiently.

Create a workflow dispatch event

Payload:

POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches
Content-Type: application/json
Authorization: Bearer <token>

{
  "ref": "main",
  "inputs": {
    "param1": "value1",
    "param2": "value2"
  }
}

Permissions:

  • "Actions" repository permissions (write)

Additional endpoints:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm/need 1 This PR needs approval from one additional maintainer to be merged. modifies/docs modifies/go Pull requests that update Go code modifies/js modifies/templates This PR modifies the template files modifies/translation size/L Denotes a PR that changes 100-499 lines, ignoring generated files. topic/gitea-actions related to the actions of Gitea type/feature Completely new functionality. Can only be merged if feature freeze is not active.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Actions - Manually trigger a workflow/action