diff --git a/pkg/ci/github.go b/pkg/ci/github.go index f7eabd66..5104db40 100644 --- a/pkg/ci/github.go +++ b/pkg/ci/github.go @@ -23,6 +23,7 @@ package ci import ( + "path" "path/filepath" "strings" ) @@ -31,6 +32,7 @@ type Github struct { *Common CICommit string `env:"GITHUB_SHA"` CIBuildName string `env:"RUNNER_WORKSPACE"` + CIRepository string `env:"GITHUB_REPOSITORY"` CIBranchName string `env:"GITHUB_REF"` } @@ -45,11 +47,18 @@ func (c *Github) BranchReplaceSlash() string { } func (c *Github) BuildName() string { + // GITHUB_REPOSITORY is "/" and names the repository directly. + // Prefer it over deriving the name from RUNNER_WORKSPACE, whose layout differs per + // platform: GitHub Actions uses /home/runner/work/, while Gitea Actions + // uses /workspace// and exports its parent, so the last path + // element there is the owner rather than the repository. + if c.CIRepository != "" { + return c.Common.BuildName(path.Base(c.CIRepository)) + } if c.CIBuildName != "" { return c.Common.BuildName(filepath.Base(c.CIBuildName)) - } else { - return c.Common.BuildName(c.CIBuildName) } + return c.Common.BuildName(c.CIBuildName) } func (c *Github) Branch() string { diff --git a/pkg/ci/github_test.go b/pkg/ci/github_test.go index fac5694c..7204a788 100644 --- a/pkg/ci/github_test.go +++ b/pkg/ci/github_test.go @@ -43,8 +43,22 @@ func TestGithub_BuildName(t *testing.T) { assert.Equal(t, "name", ci.BuildName()) } +func TestGithub_BuildName_FromRepository(t *testing.T) { + ci := &Github{Common: &Common{}, CIRepository: "owner/name", CIBuildName: "/home/runner/work/name"} + + assert.Equal(t, "name", ci.BuildName()) +} + +// Gitea Actions checks out to /workspace// and exports the parent of that +// as RUNNER_WORKSPACE, so its last path element is the owner. GITHUB_REPOSITORY must win. +func TestGithub_BuildName_FromRepository_GiteaWorkspaceLayout(t *testing.T) { + ci := &Github{Common: &Common{}, CIRepository: "owner/name", CIBuildName: "/workspace/owner"} + + assert.Equal(t, "name", ci.BuildName()) +} + func TestGithub_Override_ImageName(t *testing.T) { - ci := &Github{Common: &Common{}, CIBuildName: "/home/runner/work/name"} + ci := &Github{Common: &Common{}, CIRepository: "owner/name", CIBuildName: "/home/runner/work/name"} ci.SetImageName("override") assert.Equal(t, "override", ci.BuildName()) }