You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

variables.go 3.0KB

Support configuration variables on Gitea Actions (#24724) Co-Author: @silverwind @wxiaoguang Replace: #24404 See: - [defining configuration variables for multiple workflows](https://docs.github.com/en/actions/learn-github-actions/variables#defining-configuration-variables-for-multiple-workflows) - [vars context](https://docs.github.com/en/actions/learn-github-actions/contexts#vars-context) Related to: - [x] protocol: https://gitea.com/gitea/actions-proto-def/pulls/7 - [x] act_runner: https://gitea.com/gitea/act_runner/pulls/157 - [x] act: https://gitea.com/gitea/act/pulls/43 #### Screenshoot Create Variable: ![image](https://user-images.githubusercontent.com/33891828/236758288-032b7f64-44e7-48ea-b07d-de8b8b0e3729.png) ![image](https://user-images.githubusercontent.com/33891828/236758174-5203f64c-1d0e-4737-a5b0-62061dee86f8.png) Workflow: ```yaml test_vars: runs-on: ubuntu-latest steps: - name: Print Custom Variables run: echo "${{ vars.test_key }}" - name: Try to print a non-exist var run: echo "${{ vars.NON_EXIST_VAR }}" ``` Actions Log: ![image](https://user-images.githubusercontent.com/33891828/236759075-af0c5950-368d-4758-a8ac-47a96e43b6e2.png) --- This PR just implement the org / user (depends on the owner of the current repository) and repo level variables, The Environment level variables have not been implemented. Because [Environment](https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment#about-environments) is a module separate from `Actions`. Maybe it would be better to create a new PR to do it. --------- Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Giteabot <teabot@gitea.io>
1 year ago
Support configuration variables on Gitea Actions (#24724) Co-Author: @silverwind @wxiaoguang Replace: #24404 See: - [defining configuration variables for multiple workflows](https://docs.github.com/en/actions/learn-github-actions/variables#defining-configuration-variables-for-multiple-workflows) - [vars context](https://docs.github.com/en/actions/learn-github-actions/contexts#vars-context) Related to: - [x] protocol: https://gitea.com/gitea/actions-proto-def/pulls/7 - [x] act_runner: https://gitea.com/gitea/act_runner/pulls/157 - [x] act: https://gitea.com/gitea/act/pulls/43 #### Screenshoot Create Variable: ![image](https://user-images.githubusercontent.com/33891828/236758288-032b7f64-44e7-48ea-b07d-de8b8b0e3729.png) ![image](https://user-images.githubusercontent.com/33891828/236758174-5203f64c-1d0e-4737-a5b0-62061dee86f8.png) Workflow: ```yaml test_vars: runs-on: ubuntu-latest steps: - name: Print Custom Variables run: echo "${{ vars.test_key }}" - name: Try to print a non-exist var run: echo "${{ vars.NON_EXIST_VAR }}" ``` Actions Log: ![image](https://user-images.githubusercontent.com/33891828/236759075-af0c5950-368d-4758-a8ac-47a96e43b6e2.png) --- This PR just implement the org / user (depends on the owner of the current repository) and repo level variables, The Environment level variables have not been implemented. Because [Environment](https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment#about-environments) is a module separate from `Actions`. Maybe it would be better to create a new PR to do it. --------- Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Giteabot <teabot@gitea.io>
1 year ago
Support configuration variables on Gitea Actions (#24724) Co-Author: @silverwind @wxiaoguang Replace: #24404 See: - [defining configuration variables for multiple workflows](https://docs.github.com/en/actions/learn-github-actions/variables#defining-configuration-variables-for-multiple-workflows) - [vars context](https://docs.github.com/en/actions/learn-github-actions/contexts#vars-context) Related to: - [x] protocol: https://gitea.com/gitea/actions-proto-def/pulls/7 - [x] act_runner: https://gitea.com/gitea/act_runner/pulls/157 - [x] act: https://gitea.com/gitea/act/pulls/43 #### Screenshoot Create Variable: ![image](https://user-images.githubusercontent.com/33891828/236758288-032b7f64-44e7-48ea-b07d-de8b8b0e3729.png) ![image](https://user-images.githubusercontent.com/33891828/236758174-5203f64c-1d0e-4737-a5b0-62061dee86f8.png) Workflow: ```yaml test_vars: runs-on: ubuntu-latest steps: - name: Print Custom Variables run: echo "${{ vars.test_key }}" - name: Try to print a non-exist var run: echo "${{ vars.NON_EXIST_VAR }}" ``` Actions Log: ![image](https://user-images.githubusercontent.com/33891828/236759075-af0c5950-368d-4758-a8ac-47a96e43b6e2.png) --- This PR just implement the org / user (depends on the owner of the current repository) and repo level variables, The Environment level variables have not been implemented. Because [Environment](https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment#about-environments) is a module separate from `Actions`. Maybe it would be better to create a new PR to do it. --------- Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Giteabot <teabot@gitea.io>
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package setting
  4. import (
  5. "errors"
  6. "net/http"
  7. "code.gitea.io/gitea/modules/base"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/setting"
  10. shared "code.gitea.io/gitea/routers/web/shared/actions"
  11. shared_user "code.gitea.io/gitea/routers/web/shared/user"
  12. )
  13. const (
  14. tplRepoVariables base.TplName = "repo/settings/actions"
  15. tplOrgVariables base.TplName = "org/settings/actions"
  16. tplUserVariables base.TplName = "user/settings/actions"
  17. )
  18. type variablesCtx struct {
  19. OwnerID int64
  20. RepoID int64
  21. IsRepo bool
  22. IsOrg bool
  23. IsUser bool
  24. VariablesTemplate base.TplName
  25. RedirectLink string
  26. }
  27. func getVariablesCtx(ctx *context.Context) (*variablesCtx, error) {
  28. if ctx.Data["PageIsRepoSettings"] == true {
  29. return &variablesCtx{
  30. RepoID: ctx.Repo.Repository.ID,
  31. IsRepo: true,
  32. VariablesTemplate: tplRepoVariables,
  33. RedirectLink: ctx.Repo.RepoLink + "/settings/actions/variables",
  34. }, nil
  35. }
  36. if ctx.Data["PageIsOrgSettings"] == true {
  37. err := shared_user.LoadHeaderCount(ctx)
  38. if err != nil {
  39. ctx.ServerError("LoadHeaderCount", err)
  40. return nil, nil
  41. }
  42. return &variablesCtx{
  43. OwnerID: ctx.ContextUser.ID,
  44. IsOrg: true,
  45. VariablesTemplate: tplOrgVariables,
  46. RedirectLink: ctx.Org.OrgLink + "/settings/actions/variables",
  47. }, nil
  48. }
  49. if ctx.Data["PageIsUserSettings"] == true {
  50. return &variablesCtx{
  51. OwnerID: ctx.Doer.ID,
  52. IsUser: true,
  53. VariablesTemplate: tplUserVariables,
  54. RedirectLink: setting.AppSubURL + "/user/settings/actions/variables",
  55. }, nil
  56. }
  57. return nil, errors.New("unable to set Variables context")
  58. }
  59. func Variables(ctx *context.Context) {
  60. ctx.Data["Title"] = ctx.Tr("actions.variables")
  61. ctx.Data["PageType"] = "variables"
  62. ctx.Data["PageIsSharedSettingsVariables"] = true
  63. vCtx, err := getVariablesCtx(ctx)
  64. if err != nil {
  65. ctx.ServerError("getVariablesCtx", err)
  66. return
  67. }
  68. shared.SetVariablesContext(ctx, vCtx.OwnerID, vCtx.RepoID)
  69. if ctx.Written() {
  70. return
  71. }
  72. ctx.HTML(http.StatusOK, vCtx.VariablesTemplate)
  73. }
  74. func VariableCreate(ctx *context.Context) {
  75. vCtx, err := getVariablesCtx(ctx)
  76. if err != nil {
  77. ctx.ServerError("getVariablesCtx", err)
  78. return
  79. }
  80. if ctx.HasError() { // form binding validation error
  81. ctx.JSONError(ctx.GetErrMsg())
  82. return
  83. }
  84. shared.CreateVariable(ctx, vCtx.OwnerID, vCtx.RepoID, vCtx.RedirectLink)
  85. }
  86. func VariableUpdate(ctx *context.Context) {
  87. vCtx, err := getVariablesCtx(ctx)
  88. if err != nil {
  89. ctx.ServerError("getVariablesCtx", err)
  90. return
  91. }
  92. if ctx.HasError() { // form binding validation error
  93. ctx.JSONError(ctx.GetErrMsg())
  94. return
  95. }
  96. shared.UpdateVariable(ctx, vCtx.RedirectLink)
  97. }
  98. func VariableDelete(ctx *context.Context) {
  99. vCtx, err := getVariablesCtx(ctx)
  100. if err != nil {
  101. ctx.ServerError("getVariablesCtx", err)
  102. return
  103. }
  104. shared.DeleteVariable(ctx, vCtx.RedirectLink)
  105. }