summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Wang <kwangsan@gmail.com>2022-05-08 16:23:19 +0200
committerGitHub <noreply@github.com>2022-05-08 16:23:19 +0200
commit8e216c642e11dfa8dba7aab040bb09274c48b5a6 (patch)
tree7777d33b2d706a92c3572ffa47c704057bb73ed9
parentbuild(deps): bump github.com/moby/buildkit from 0.10.1 to 0.10.2 (#1147) (diff)
downloadforgejo-act-8e216c642e11dfa8dba7aab040bb09274c48b5a6.tar.xz
forgejo-act-8e216c642e11dfa8dba7aab040bb09274c48b5a6.zip
feat: support `GITHUB_REF_NAME` & `GITHUB_REF_TYPE` (#1142)
* feat: support `_REF_NAME` & `_REF_TYPE` * chore: fix `step_test::TestSetupEnv` * fix: logic & test * test: delete `GITHUB_REF_NAME`, `GITHUB_REF_TYPE`
-rw-r--r--pkg/model/github_context.go2
-rw-r--r--pkg/runner/run_context.go11
-rw-r--r--pkg/runner/step_test.go2
3 files changed, 15 insertions, 0 deletions
diff --git a/pkg/model/github_context.go b/pkg/model/github_context.go
index c68aaa1..26e61ae 100644
--- a/pkg/model/github_context.go
+++ b/pkg/model/github_context.go
@@ -18,6 +18,8 @@ type GithubContext struct {
EventName string `json:"event_name"`
Sha string `json:"sha"`
Ref string `json:"ref"`
+ RefName string `json:"ref_name"`
+ RefType string `json:"ref_type"`
HeadRef string `json:"head_ref"`
BaseRef string `json:"base_ref"`
Token string `json:"token"`
diff --git a/pkg/runner/run_context.go b/pkg/runner/run_context.go
index 06c0740..07f62d8 100644
--- a/pkg/runner/run_context.go
+++ b/pkg/runner/run_context.go
@@ -549,6 +549,15 @@ func (rc *RunContext) getGithubContext() *model.GithubContext {
ghc.SetRefAndSha(rc.Config.DefaultBranch, repoPath)
+ // https://docs.github.com/en/actions/learn-github-actions/environment-variables
+ if strings.HasPrefix(ghc.Ref, "refs/tags/") {
+ ghc.RefType = "tag"
+ ghc.RefName = ghc.Ref[len("refs/tags/"):]
+ } else if strings.HasPrefix(ghc.Ref, "refs/heads/") {
+ ghc.RefType = "branch"
+ ghc.RefName = ghc.Ref[len("refs/heads/"):]
+ }
+
return ghc
}
@@ -624,6 +633,8 @@ func (rc *RunContext) withGithubEnv(env map[string]string) map[string]string {
env["GITHUB_WORKSPACE"] = github.Workspace
env["GITHUB_SHA"] = github.Sha
env["GITHUB_REF"] = github.Ref
+ env["GITHUB_REF_NAME"] = github.RefName
+ env["GITHUB_REF_TYPE"] = github.RefType
env["GITHUB_TOKEN"] = github.Token
env["GITHUB_SERVER_URL"] = "https://github.com"
env["GITHUB_API_URL"] = "https://api.github.com"
diff --git a/pkg/runner/step_test.go b/pkg/runner/step_test.go
index c8f3a0c..b87efeb 100644
--- a/pkg/runner/step_test.go
+++ b/pkg/runner/step_test.go
@@ -154,6 +154,8 @@ func TestSetupEnv(t *testing.T) {
// These are commit or system specific
delete((env), "GITHUB_REF")
+ delete((env), "GITHUB_REF_NAME")
+ delete((env), "GITHUB_REF_TYPE")
delete((env), "GITHUB_SHA")
delete((env), "GITHUB_WORKSPACE")
delete((env), "GITHUB_REPOSITORY")