Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 11 additions & 2 deletions pkg/ci/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package ci

import (
"path"
"path/filepath"
"strings"
)
Expand All @@ -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"`
}

Expand All @@ -45,11 +47,18 @@ func (c *Github) BranchReplaceSlash() string {
}

func (c *Github) BuildName() string {
// GITHUB_REPOSITORY is "<owner>/<repository>" 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/<repository>, while Gitea Actions
// uses /workspace/<owner>/<repository> 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 {
Expand Down
16 changes: 15 additions & 1 deletion pkg/ci/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/<owner>/<repository> 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())
}
Expand Down
Loading