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

Merged
merged 73 commits into from
Aug 19, 2024
Merged

Actions support workflow dispatch event #28163

merged 73 commits into from
Aug 19, 2024

Conversation

pangliang
Copy link
Contributor

@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
@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
Contributor 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 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 and removed size/L labels Dec 5, 2023
@pull-request-size pull-request-size bot added size/L and removed size/XL labels Dec 5, 2023
@pangliang
Copy link
Contributor 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
Contributor 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
@yp05327
Copy link
Contributor

yp05327 commented Dec 7, 2023

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

@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.

@@ -76,6 +76,11 @@
</button>
{{end}}
</div>

{{if .AllowTriggerWorkflowDispatchEvent}}
Copy link
Contributor

@yp05327 yp05327 Dec 7, 2023

Choose a reason for hiding this comment

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

Suggested change
{{if .AllowTriggerWorkflowDispatchEvent}}
{{if .WorkflowDispatchConfig}}

AllowTriggerWorkflowDispatchEvent is unnecessary.
But WorkflowDispatch or WorkflowDispatchConfig should be improved,
return WorkflowDispatch{} when inputs are not defined.
return nil, when there are no workflow_dispatch in on

Copy link
Contributor Author

@pangliang pangliang Dec 7, 2023

Choose a reason for hiding this comment

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

AllowTriggerWorkflowDispatchEvent is used to decide whether to display trigger button, and WorkflowDispatchConfig is used to decide whether to process inputs;
it can also be triggered without inputs.

Copy link
Contributor

@yp05327 yp05327 Dec 7, 2023

Choose a reason for hiding this comment

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

When workflow_dispatch is defined, but inputs are not
→ workflowDispatchConfig = {}
→ workflowDispatchConfig != nil, let's display the notification
→ user click Run Workflow
→ workflowDispatchConfig.Inputs = nil, don't have inputs, don't show them

When workflow_dispatch and inputs are both defined
→ workflowDispatchConfig = nil
→ don't display the notification

But we should do some changes to WorkflowDispatchConfig function.
WorkflowDispatchConfig will return nil when workflow_dispatch is defined, but inputs are not.
And this also causes a bug now, when workflow_dispatch is defined, but inputs are not, the notification will not display😕

e.g.
image
nothing happened here:
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, yes, you are right; before, WorkflowDispatchConfig() always returned non-null, but now the behavior of parse has been modified. Use workflowDispatchConfig to determine whether to display the button or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for such a confusing scenario; I fixed it and the logic now is:

  • workflow_dispatch is not defined, WorkflowDispatchConfig() return nil
  • workflow_dispatch is defined WorkflowDispatchConfig() return non nil , Whether the input is defined or not
  • inputs is not defined WorkflowDispatch.Inputs is nil

so,

  • WorkflowDispatchConfig if exists, display button
  • When .WorkflowDispatchConfig.Inputs is nil, <range > nothing happens

Copy link
Contributor Author

@pangliang pangliang Dec 7, 2023

Choose a reason for hiding this comment

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

ParseRawOn is no longer needed
but, I think some modif still need to be made, workflow_dispatch is not defined, WorkflowDispatchConfig() return nil

var config WorkflowDispatch
node := val["workflow_dispatch"]
if !decodeNode(node, &config) {
    return nil
}

return &config

change to

var config WorkflowDispatch
node, found := val["workflow_dispatch"]
if found && decodeNode(node, &config) {
    return &config
}

return nil

I submitted a simpler PR to fix it: https://gitea.com/gitea/act/pulls/85
The previous PR 84 is no longer needed

Copy link
Contributor

@yp05327 yp05327 Dec 8, 2023

Choose a reason for hiding this comment

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

Thanks for your contribution.

ps: not only workflow_dispatch, other events, e.g. workflow_call, have some similar functions,
we can check them later.

Copy link
Contributor

@yp05327 yp05327 Dec 8, 2023

Choose a reason for hiding this comment

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

We need do more changes in WorkflowDispatchConfig.

if w.RawOn.Kind != yaml.MappingNode {
		return nil
}

This is not correct. e.g. if I define this:

on: [push, workflow_dispatch]

w.RawOn.Kind is yaml.SequenceNode, and will return nil.
If I define this:

on: workflow_dispatch

w.RawOn.Kind is yaml.ScalarNode, and will return nil.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I think I will have some time to work on it next few days; btw, it seems that the On function has the same problem, switch does not handle all types of kind.

Copy link
Contributor Author

@pangliang pangliang Dec 11, 2023

Choose a reason for hiding this comment

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

I pushed a new commit to nektos/act#2123 and added a recursive function to handle nested SequenceNode. It can also support workflow_call. The following cases have been tested.

on: workflow_dispatch

on: [push, workflow_dispatch]

on:
    - push
    - workflow_dispatch

on:
    push:
    pull_request:
    workflow_dispatch:
        inputs:
            logLevel:
                description: 'Log level'
                required: true
                default: 'warning'
                type: choice
                options:
                - info
                - warning
                - debug

@denyskon
Copy link
Member

I changed it to checkbox because it is more common throughout Gitea UI

@denyskon
Copy link
Member

Also, putting the titles of the input fields is equally common in Gitea UI. This long form is just an example and shouldn't normally contain so many fields. We can make it wider, however, just to look better.

@lunny
Copy link
Member

lunny commented Aug 17, 2024

Also, putting the titles of the input fields is equally common in Gitea UI. This long form is just an example and shouldn't normally contain so many fields. We can make it wider, however, just to look better.

Yes, wider is better.

@denyskon
Copy link
Member

grafik

That would be tiny instead of mini. Looks better IMO, normal size would be too wide.

@denyskon
Copy link
Member

grafik
(here with a more realistic example, taken from my production pipeline 😆 )

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged labels Aug 17, 2024
@denyskon denyskon added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Aug 17, 2024
@denyskon denyskon added the docs-update-needed The document needs to be updated synchronously label Aug 17, 2024
@github-actions github-actions bot removed the docs-update-needed The document needs to be updated synchronously label Aug 18, 2024
@yp05327
Copy link
Contributor

yp05327 commented Aug 18, 2024

image
Why removed ???

@techknowlogick
Copy link
Member

Thanks so much @pangliang !!!

@techknowlogick techknowlogick merged commit 36232b6 into go-gitea:main Aug 19, 2024
26 checks passed
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Aug 19, 2024
@lunny lunny added the docs-update-needed The document needs to be updated synchronously label Aug 19, 2024
@lunny
Copy link
Member

lunny commented Aug 19, 2024

Please send a docs PR to https://gitea.com/gitea/docs

@pangliang pangliang deleted the actions_support_workflow_dispatch_event branch August 19, 2024 10:25
zjjhot added a commit to zjjhot/gitea that referenced this pull request Aug 20, 2024
* giteaofficial/main:
  add CfTurnstileSitekey context data to all captcha templates (go-gitea#31874)
  Add tag name in the commits list (go-gitea#31082)
  Fix actions notify bug (go-gitea#31866)
  Actions support workflow dispatch event (go-gitea#28163)
4kProgrammer added a commit to 4kProgrammer/gitea that referenced this pull request Aug 20, 2024
@hoo29 hoo29 mentioned this pull request Sep 4, 2024
@FireGhost
Copy link

Why has this feature been removed from 1.22.2 in the end ?

@Janhouse
Copy link

@FireGhost this is part of 1.23, wait for release.

@go-gitea go-gitea locked and limited conversation to collaborators Sep 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
docs-update-needed The document needs to be updated synchronously lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/go Pull requests that update Go code modifies/templates This PR modifies the template files modifies/translation 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