gitea源码

actions.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package setting
  4. import (
  5. "fmt"
  6. "strings"
  7. "time"
  8. "code.gitea.io/gitea/modules/log"
  9. )
  10. // Actions settings
  11. var (
  12. Actions = struct {
  13. Enabled bool
  14. LogStorage *Storage // how the created logs should be stored
  15. LogRetentionDays int64 `ini:"LOG_RETENTION_DAYS"`
  16. LogCompression logCompression `ini:"LOG_COMPRESSION"`
  17. ArtifactStorage *Storage // how the created artifacts should be stored
  18. ArtifactRetentionDays int64 `ini:"ARTIFACT_RETENTION_DAYS"`
  19. DefaultActionsURL defaultActionsURL `ini:"DEFAULT_ACTIONS_URL"`
  20. ZombieTaskTimeout time.Duration `ini:"ZOMBIE_TASK_TIMEOUT"`
  21. EndlessTaskTimeout time.Duration `ini:"ENDLESS_TASK_TIMEOUT"`
  22. AbandonedJobTimeout time.Duration `ini:"ABANDONED_JOB_TIMEOUT"`
  23. SkipWorkflowStrings []string `ini:"SKIP_WORKFLOW_STRINGS"`
  24. }{
  25. Enabled: true,
  26. DefaultActionsURL: defaultActionsURLGitHub,
  27. SkipWorkflowStrings: []string{"[skip ci]", "[ci skip]", "[no ci]", "[skip actions]", "[actions skip]"},
  28. }
  29. )
  30. type defaultActionsURL string
  31. func (url defaultActionsURL) URL() string {
  32. switch url {
  33. case defaultActionsURLGitHub:
  34. return "https://github.com"
  35. case defaultActionsURLSelf:
  36. return strings.TrimSuffix(AppURL, "/")
  37. default:
  38. // This should never happen, but just in case, use GitHub as fallback
  39. return "https://github.com"
  40. }
  41. }
  42. const (
  43. defaultActionsURLGitHub = "github" // https://github.com
  44. defaultActionsURLSelf = "self" // the root URL of the self-hosted Gitea instance
  45. // DefaultActionsURL only supports GitHub and the self-hosted Gitea.
  46. // It's intentionally not supported more, so please be cautious before adding more like "gitea" or "gitlab".
  47. // If you get some trouble with `uses: username/action_name@version` in your workflow,
  48. // please consider to use `uses: https://the_url_you_want_to_use/username/action_name@version` instead.
  49. )
  50. type logCompression string
  51. func (c logCompression) IsValid() bool {
  52. return c.IsNone() || c.IsZstd()
  53. }
  54. func (c logCompression) IsNone() bool {
  55. return string(c) == "none"
  56. }
  57. func (c logCompression) IsZstd() bool {
  58. return c == "" || string(c) == "zstd"
  59. }
  60. func loadActionsFrom(rootCfg ConfigProvider) error {
  61. sec := rootCfg.Section("actions")
  62. err := sec.MapTo(&Actions)
  63. if err != nil {
  64. return fmt.Errorf("failed to map Actions settings: %v", err)
  65. }
  66. if urls := string(Actions.DefaultActionsURL); urls != defaultActionsURLGitHub && urls != defaultActionsURLSelf {
  67. url := strings.Split(urls, ",")[0]
  68. if strings.HasPrefix(url, "https://") || strings.HasPrefix(url, "http://") {
  69. log.Error("[actions] DEFAULT_ACTIONS_URL does not support %q as custom URL any longer, fallback to %q",
  70. urls,
  71. defaultActionsURLGitHub,
  72. )
  73. Actions.DefaultActionsURL = defaultActionsURLGitHub
  74. } else {
  75. return fmt.Errorf("unsupported [actions] DEFAULT_ACTIONS_URL: %q", urls)
  76. }
  77. }
  78. // don't support to read configuration from [actions]
  79. Actions.LogStorage, err = getStorage(rootCfg, "actions_log", "", nil)
  80. if err != nil {
  81. return err
  82. }
  83. // default to 1 year
  84. if Actions.LogRetentionDays <= 0 {
  85. Actions.LogRetentionDays = 365
  86. }
  87. actionsSec, _ := rootCfg.GetSection("actions.artifacts")
  88. Actions.ArtifactStorage, err = getStorage(rootCfg, "actions_artifacts", "", actionsSec)
  89. if err != nil {
  90. return err
  91. }
  92. // default to 90 days in Github Actions
  93. if Actions.ArtifactRetentionDays <= 0 {
  94. Actions.ArtifactRetentionDays = 90
  95. }
  96. Actions.ZombieTaskTimeout = sec.Key("ZOMBIE_TASK_TIMEOUT").MustDuration(10 * time.Minute)
  97. Actions.EndlessTaskTimeout = sec.Key("ENDLESS_TASK_TIMEOUT").MustDuration(3 * time.Hour)
  98. Actions.AbandonedJobTimeout = sec.Key("ABANDONED_JOB_TIMEOUT").MustDuration(24 * time.Hour)
  99. if !Actions.LogCompression.IsValid() {
  100. return fmt.Errorf("invalid [actions] LOG_COMPRESSION: %q", Actions.LogCompression)
  101. }
  102. return nil
  103. }