Skip to content

feat(actions): add deployment environments for scoped secrets and variables - #38149

Open
alexosumi wants to merge 38 commits into
go-gitea:mainfrom
alexosumi:feat/actions-environments
Open

feat(actions): add deployment environments for scoped secrets and variables#38149
alexosumi wants to merge 38 commits into
go-gitea:mainfrom
alexosumi:feat/actions-environments

Conversation

@alexosumi

@alexosumi alexosumi commented Jun 17, 2026

Copy link
Copy Markdown

Adds deployment environments to Actions.

Secrets and variables are repo- or org-scoped today, so a production deploy token is
readable by any workflow on any branch. An environment is a named scope holding its own
secrets and variables, optionally restricted to matching branches and tags.

A job declaring environment: production receives those values layered over the repo and
org ones. A job that cannot reach the environment it names fails, rather than quietly
deploying with the repository's credentials and no branch policy. An environment named by a
workflow is created on first reference, as on GitHub, except for fork pull requests, whose
workflow comes from the fork.

Managed under Settings → Actions → Environments, and through
/repos/{owner}/{repo}/environments.

Screenshot 2026-08-18 at 21 23 21 image

Fixes: #32090

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jun 17, 2026
@alexosumi
alexosumi force-pushed the feat/actions-environments branch from b985d41 to 94c5b3a Compare June 17, 2026 16:41
@alexosumi alexosumi changed the title Feat/actions environments feat(actions): add deployment environments Jun 17, 2026
@github-actions github-actions Bot added the type/feature Completely new functionality. Can only be merged if feature freeze is not active. label Jun 17, 2026
@alexosumi
alexosumi force-pushed the feat/actions-environments branch 3 times, most recently from f10c32e to 3b7ee2d Compare June 17, 2026 17:36
@alexosumi

Copy link
Copy Markdown
Author

main branch is protected, maybe better open PR to branch 1.26?

@TheFox0x7

TheFox0x7 commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

no, opening to main branch is correct.

@alexosumi

Copy link
Copy Markdown
Author

ok, I also opened a pull request in the runner repository so that the runner can use the environment. https://gitea.com/gitea/runner/pulls/1037

@bircni bircni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

first few findings I found

Comment thread models/actions/environment.go Outdated
Comment thread models/actions/environment.go Outdated
Comment thread models/secret/secret.go Outdated
Comment thread models/actions/variable.go Outdated
Comment thread routers/api/v1/repo/environment.go
Comment thread routers/web/repo/setting/environment.go Outdated
@GiteaBot GiteaBot added lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Jun 17, 2026
@alexosumi
alexosumi force-pushed the feat/actions-environments branch from 3b7ee2d to 48e07bb Compare June 18, 2026 01:49

@alexosumi alexosumi left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

all points were reviewed.

Implements deployment environments similar to GitHub Actions, resolves go-gitea#32090.

Environments allow scoping secrets and variables to a named deployment
target (e.g. production, staging) with optional branch protection rules.

- New DB tables: action_environment, action_environment_secret,
  action_environment_variable (migrations v340, v341 in v1_27)
- ActionRunJob.environment_name populated from workflow YAML environment: key
- Secret/variable resolution overlays env-scoped values over repo/org scope
- GenerateGiteaContext exposes github.environment to runner steps
- REST API: CRUD under /repos/{owner}/{repo}/environments
- Web UI: Environments nested inside Actions settings dropdown
- Generated swagger spec

Closes go-gitea#32090.
@alexosumi
alexosumi force-pushed the feat/actions-environments branch from c750a28 to 9318ed2 Compare June 18, 2026 14:25
@alexosumi
alexosumi requested a review from bircni June 18, 2026 14:33
@bircni

bircni commented Jun 18, 2026

Copy link
Copy Markdown
Member

please dont force push so it is easier to review :-)

Comment thread services/actions/context.go Outdated
Comment thread models/actions/environment.go Outdated
Comment thread routers/api/v1/repo/environment.go
Comment thread services/actions/environment.go Outdated
Comment thread services/actions/environment.go Outdated
Comment thread modules/structs/environment.go Outdated
Comment thread routers/api/v1/api.go Outdated
Comment thread routers/api/v1/api.go Outdated
@bircni
bircni marked this pull request as draft June 18, 2026 16:52
Add ActionEnvironmentVariable, ActionEnvironmentSecret and ActionEnvironment
to deleteBeans so they are removed when the repository is deleted.

Addresses review feedback from bircni.
- MatchesBranch now also handles refs/tags/ prefix so tag-triggered
  deployments respect the branch/tag protection rule
- Clean up environment tables (ActionEnvironment, ActionEnvironmentSecret,
  ActionEnvironmentVariable) on repository deletion via deleteBeans
- Minor API and struct adjustments from review
@alexosumi
alexosumi marked this pull request as ready for review June 18, 2026 19:38
@alexosumi
alexosumi requested a review from bircni June 18, 2026 21:18
Comment thread models/migrations/v1_27/v341.go Outdated
Comment thread models/migrations/v1_27/v340.go Outdated
bircni

This comment was marked as outdated.

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged labels Aug 15, 2026
@bircni
bircni self-requested a review August 15, 2026 16:31
@GiteaBot GiteaBot added lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Aug 15, 2026
@silverwind

Copy link
Copy Markdown
Member

3.6k lines diff seems too big. I would assume this can be cut in half, if not more.

@bircni

bircni commented Aug 18, 2026

Copy link
Copy Markdown
Member

3.6k lines diff seems too big. I would assume this can be cut in half, if not more.

I dont think so...

@bircni

bircni commented Aug 18, 2026

Copy link
Copy Markdown
Member

@silverwind
Broke down the 3.6k lines:
~1.3k is generated swagger JSON (v1-openapi3.generated.json / v1-swagger.generated.json)
~200 is the migration + its test
~390 is tests.

The secret/variable CRUD reuses the existing repo/org/user-scoped services rather than reimplementing them, so there wasn't much duplicated logic left to trim — I did collapse a repeated environment-lookup block in the API handlers into one helper, but that's cosmetic, not a size reduction.

Given that, I don't think this shrinks further without cutting scope, and I'd rather keep it as one PR.

@silverwind

Copy link
Copy Markdown
Member

Will clean this up

A job declaring `environment:` could still run as though it declared none,
with the repository's credentials and no branch policy. The name was dropped
for the first combination of a deferred matrix, an expression resolving to
nothing produced an environment literally named "null", and a name Gitea
cannot create was only logged.

Resolve the environment once when the task is picked and deny the job when it
cannot be reached, creating it on first reference so a runner arriving before
the run finished planning does not fail it instead. Share the fork-trust
predicate, so pull_request_target, which does receive the repository's
secrets, is gated like a push rather than exempted.

Closes go-gitea#32090

Assisted-by: Claude Code:claude-opus-5
@silverwind silverwind changed the title feat(actions): add deployment environments feat(actions): add deployment environments for scoped secrets and variables Aug 18, 2026
@silverwind

Copy link
Copy Markdown
Member

Done, 7 bugs fixed. And you're right, this branch is already as slim as it can get.

xorm's plain Sync drops every index it does not find in the struct it is
given, and a migration adding a single column declares none. Adding
environment_name therefore left action_run_job without any of its indices on
every upgrading instance, with nothing to restore them. The secret and
action_variable column adds survive only because RecreateTable rebuilds those
two a step later, so they are switched over as well.

Assisted-by: Claude Code:claude-opus-5
@silverwind

silverwind commented Aug 18, 2026

Copy link
Copy Markdown
Member

xorm footgun avoided in 88058a4 and AGENTS.md extended for it.

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Aug 18, 2026
…y settings

The danger zone was built from flex-list classes rather than the ones the
repository danger zone uses, so it rendered without its red border and with
the button stacked under the text, and its body showed the confirm prompt
instead of a description.

The list gains a delete button beside the edit one, and shows the creation
date next to the branch policy rather than opposite it, so the row reads as
one line of metadata.

Assisted-by: Claude Code:claude-opus-5
@silverwind

Copy link
Copy Markdown
Member

Also tweaked UI and added screenshots.

@bircni

bircni commented Aug 18, 2026

Copy link
Copy Markdown
Member

I wanna wait for @Zettat123

@silverwind

silverwind commented Aug 18, 2026

Copy link
Copy Markdown
Member

I don't think they'll have anything to complain, Claude worked over an hour on it with a lot of context 😆.

bircni added 2 commits August 18, 2026 21:46
…ents

# Conflicts:
#	modelmigration/migrations.go
#	modelmigration/v28/v349.go
#	modelmigration/v28/v349_test.go
@bircni bircni added the release/highlight Marks a PR as a highlight-worthy change for the release notes. label Aug 29, 2026
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. release/highlight Marks a PR as a highlight-worthy change for the release notes. 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.

Enhanced Environment Variable Management

8 participants