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

Make runs-on support variable expression #29468

Merged
merged 8 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions modules/actions/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package actions

import (
"context"

actions_model "code.gitea.io/gitea/models/actions"
"code.gitea.io/gitea/models/db"
secret_model "code.gitea.io/gitea/models/secret"
sillyguodong marked this conversation as resolved.
Show resolved Hide resolved
"code.gitea.io/gitea/modules/log"
secret_module "code.gitea.io/gitea/modules/secret"
"code.gitea.io/gitea/modules/setting"
)

func GetSecretsOfTask(ctx context.Context, task *actions_model.ActionTask) map[string]string {
secrets := map[string]string{}

secrets["GITHUB_TOKEN"] = task.Token
secrets["GITEA_TOKEN"] = task.Token

if task.Job.Run.IsForkPullRequest && task.Job.Run.TriggerEvent != GithubEventPullRequestTarget {
// ignore secrets for fork pull request, except GITHUB_TOKEN and GITEA_TOKEN which are automatically generated.
// for the tasks triggered by pull_request_target event, they could access the secrets because they will run in the context of the base branch
// see the documentation: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target
return secrets
}

ownerSecrets, err := db.Find[secret_model.Secret](ctx, secret_model.FindSecretsOptions{OwnerID: task.Job.Run.Repo.OwnerID})
if err != nil {
log.Error("find secrets of owner %v: %v", task.Job.Run.Repo.OwnerID, err)
// go on
}
repoSecrets, err := db.Find[secret_model.Secret](ctx, secret_model.FindSecretsOptions{RepoID: task.Job.Run.RepoID})
if err != nil {
log.Error("find secrets of repo %v: %v", task.Job.Run.RepoID, err)
// go on
}

for _, secret := range append(ownerSecrets, repoSecrets...) {
if v, err := secret_module.DecryptSecret(setting.SecretKey, secret.Data); err != nil {
log.Error("decrypt secret %v %q: %v", secret.ID, secret.Name, err)
// go on
} else {
secrets[secret.Name] = v
}
}

return secrets
}

func GetVariablesOfRun(ctx context.Context, run *actions_model.ActionRun) map[string]string {
variables := map[string]string{}

// Global
globalVariables, err := db.Find[actions_model.ActionVariable](ctx, actions_model.FindVariablesOpts{})
if err != nil {
log.Error("find global variables: %v", err)
}

// Org / User level
ownerVariables, err := db.Find[actions_model.ActionVariable](ctx, actions_model.FindVariablesOpts{OwnerID: run.Repo.OwnerID})
if err != nil {
log.Error("find variables of org: %d, error: %v", run.Repo.OwnerID, err)
}

// Repo level
repoVariables, err := db.Find[actions_model.ActionVariable](ctx, actions_model.FindVariablesOpts{RepoID: run.RepoID})
if err != nil {
log.Error("find variables of repo: %d, error: %v", run.RepoID, err)
}

// Level precedence: Repo > Org / User > Global
for _, v := range append(globalVariables, append(ownerVariables, repoVariables...)...) {
variables[v.Name] = v.Data
}

return variables
}
71 changes: 2 additions & 69 deletions routers/api/actions/runner/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import (

actions_model "code.gitea.io/gitea/models/actions"
"code.gitea.io/gitea/models/db"
secret_model "code.gitea.io/gitea/models/secret"
actions_module "code.gitea.io/gitea/modules/actions"
"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
secret_module "code.gitea.io/gitea/modules/secret"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/actions"

Expand All @@ -38,8 +36,8 @@ func pickTask(ctx context.Context, runner *actions_model.ActionRunner) (*runnerv
Id: t.ID,
WorkflowPayload: t.Job.WorkflowPayload,
Context: generateTaskContext(t),
Secrets: getSecretsOfTask(ctx, t),
Vars: getVariablesOfTask(ctx, t),
Secrets: actions_module.GetSecretsOfTask(ctx, t),
Vars: actions_module.GetVariablesOfRun(ctx, t.Job.Run),
}

if needs, err := findTaskNeeds(ctx, t); err != nil {
Expand All @@ -55,71 +53,6 @@ func pickTask(ctx context.Context, runner *actions_model.ActionRunner) (*runnerv
return task, true, nil
}

func getSecretsOfTask(ctx context.Context, task *actions_model.ActionTask) map[string]string {
secrets := map[string]string{}

secrets["GITHUB_TOKEN"] = task.Token
secrets["GITEA_TOKEN"] = task.Token

if task.Job.Run.IsForkPullRequest && task.Job.Run.TriggerEvent != actions_module.GithubEventPullRequestTarget {
// ignore secrets for fork pull request, except GITHUB_TOKEN and GITEA_TOKEN which are automatically generated.
// for the tasks triggered by pull_request_target event, they could access the secrets because they will run in the context of the base branch
// see the documentation: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target
return secrets
}

ownerSecrets, err := db.Find[secret_model.Secret](ctx, secret_model.FindSecretsOptions{OwnerID: task.Job.Run.Repo.OwnerID})
if err != nil {
log.Error("find secrets of owner %v: %v", task.Job.Run.Repo.OwnerID, err)
// go on
}
repoSecrets, err := db.Find[secret_model.Secret](ctx, secret_model.FindSecretsOptions{RepoID: task.Job.Run.RepoID})
if err != nil {
log.Error("find secrets of repo %v: %v", task.Job.Run.RepoID, err)
// go on
}

for _, secret := range append(ownerSecrets, repoSecrets...) {
if v, err := secret_module.DecryptSecret(setting.SecretKey, secret.Data); err != nil {
log.Error("decrypt secret %v %q: %v", secret.ID, secret.Name, err)
// go on
} else {
secrets[secret.Name] = v
}
}

return secrets
}

func getVariablesOfTask(ctx context.Context, task *actions_model.ActionTask) map[string]string {
variables := map[string]string{}

// Global
globalVariables, err := db.Find[actions_model.ActionVariable](ctx, actions_model.FindVariablesOpts{})
if err != nil {
log.Error("find global variables: %v", err)
}

// Org / User level
ownerVariables, err := db.Find[actions_model.ActionVariable](ctx, actions_model.FindVariablesOpts{OwnerID: task.Job.Run.Repo.OwnerID})
if err != nil {
log.Error("find variables of org: %d, error: %v", task.Job.Run.Repo.OwnerID, err)
}

// Repo level
repoVariables, err := db.Find[actions_model.ActionVariable](ctx, actions_model.FindVariablesOpts{RepoID: task.Job.Run.RepoID})
if err != nil {
log.Error("find variables of repo: %d, error: %v", task.Job.Run.RepoID, err)
}

// Level precedence: Repo > Org / User > Global
for _, v := range append(globalVariables, append(ownerVariables, repoVariables...)...) {
variables[v.Name] = v.Data
}

return variables
}

func generateTaskContext(t *actions_model.ActionTask) *structpb.Struct {
event := map[string]any{}
_ = json.Unmarshal([]byte(t.Job.Run.EventPayload), &event)
Expand Down
8 changes: 7 additions & 1 deletion services/actions/notifier_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
repo_model "code.gitea.io/gitea/models/repo"
unit_model "code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/actions"
actions_module "code.gitea.io/gitea/modules/actions"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
Expand Down Expand Up @@ -296,7 +297,12 @@
run.NeedApproval = need
}

jobs, err := jobparser.Parse(dwf.Content)
if err := run.LoadAttributes(ctx); err != nil {
log.Error("LoadAttributes %v", err)
continue
}

jobs, err := jobparser.Parse(dwf.Content, jobparser.WithVars(actions.GetVariablesOfRun(ctx, run)))

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / test-pgsql

undefined: jobparser.WithVars

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / test-mssql

undefined: jobparser.WithVars

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / test-mysql

undefined: jobparser.WithVars

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / test-sqlite

undefined: jobparser.WithVars

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / test-unit

undefined: jobparser.WithVars

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / lint-backend

undefined: jobparser.WithVars) (typecheck)

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / lint-backend

undefined: jobparser.WithVars) (typecheck)

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / lint-backend

undefined: jobparser.WithVars) (typecheck)

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / lint-backend

undefined: jobparser.WithVars) (typecheck)

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / lint-backend

undefined: jobparser.WithVars (typecheck)

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / backend

undefined: jobparser.WithVars

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / checks-backend

undefined: jobparser.WithVars

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / test-e2e

undefined: jobparser.WithVars

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

undefined: jobparser.WithVars) (typecheck)

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

undefined: jobparser.WithVars) (typecheck)

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

undefined: jobparser.WithVars) (typecheck)

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

undefined: jobparser.WithVars) (typecheck)

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

undefined: jobparser.WithVars (typecheck)

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

undefined: jobparser.WithVars) (typecheck)

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

undefined: jobparser.WithVars) (typecheck)

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

undefined: jobparser.WithVars) (typecheck)

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

undefined: jobparser.WithVars) (typecheck)

Check failure on line 305 in services/actions/notifier_helper.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

undefined: jobparser.WithVars (typecheck)
if err != nil {
log.Error("jobparser.Parse: %v", err)
continue
Expand Down
Loading