fix: stop deriving ImageOS from runs-on labels (#1419)

A runs-on label only names whatever image the runner maps it to, so `ubuntu-22.04:docker://node:24` reported `ubuntu22` on Debian, and `ubuntu-latest` needed a hardcoded release that must follow GitHub's move to 26.04. `ImageOS` now comes from the job env, the image env, an Ubuntu `os-release` or an image tag naming a release, else stays unset like on GitHub's self-hosted runners.

Reviewed-on: https://gitea.com/gitea/runner/pulls/1419
Reviewed-by: Lunny Xiao <[email protected]>
Co-authored-by: silverwind <[email protected]>
This commit is contained in:
silverwind authored and silverwind committed 2026-11-20 03:05:08 +00:00
1 parent bcc0b715cd
commit ff4f626871
2 files changed
+8 -53

No files matched your search

+3 -25
View File
@@ -1231,7 +1231,7 @@ func (rc *RunContext) copyWorkflowFiles() common.Executor {
}
}
}
imageOS = cmp.Or(imageOS, rc.imageOS(ctx))
imageOS = cmp.Or(imageOS, imageOSFromImage(rc.platformImage))
}
if set || imageOS != "" {
rc.defaultEnv["ImageOS"] = imageOS
@@ -1889,32 +1889,10 @@ func parentDir(p string) string {
return ""
}
// imageOS returns ImageOS, which setup-* actions use to tell one runner image release
// from another. The resolved image tag is preferred over the runs-on label because it
// still names a release when the label is a rolling one such as ubuntu-latest.
func (rc *RunContext) imageOS(ctx context.Context) string {
if job := rc.Run.Job(); job == nil || job.RunsOn() == nil {
return "" // a composite action runs on a synthetic job without runs-on
}
if imageOS := imageOSFromImage(rc.platformImage); imageOS != "" {
return imageOS
}
for _, platformName := range slices.Backward(rc.runsOnPlatformNames(ctx)) {
if platformName == "ubuntu-latest" {
// Rolling label whose image names no release either, so report GitHub's current ubuntu-latest.
return "ubuntu24"
} else if platformName != "" {
return strings.SplitN(strings.Replace(platformName, `-`, ``, 1), `.`, 2)[0]
}
}
return ""
}
// imageOSTag matches an image reference tagged with an OS family ImageOS can report plus
// its release, such as "docker.gitea.com/runner-images:ubuntu-24.04". Anything else
// ("ubuntu-latest", "app:22.04", "catthehacker/ubuntu:act-22.04", or a registry port) is
// left to the runs-on label rather than turned into a bogus OS.
// ("ubuntu-latest", "app:22.04", "catthehacker/ubuntu:act-22.04", or a registry port)
// yields no ImageOS rather than a bogus OS.
var imageOSTag = regexp.MustCompile(`:(ubuntu|win|macos)-?([0-9]+)[^/]*$`)
// imageOSFromImage derives ImageOS from an image reference, e.g.
+5 -28
View File
@@ -1757,10 +1757,10 @@ func TestRunContextContainerImageCannotSelectBackend(t *testing.T) {
func TestRunContextImageOS(t *testing.T) {
ctx := t.Context()
t.Run("keeps an ImageOS from the image, the job env or the job's Ubuntu release", func(t *testing.T) {
ubuntu := tarArchive(t, tarEntry{name: "os-release", body: "ID=ubuntu\nVERSION_ID=\"22.04\"\n"})
debian := tarArchive(t, tarEntry{name: "os-release", body: "ID=debian\nVERSION_ID=\"12\"\n"})
for _, tc := range []struct {
image string
imageEnv map[string]string
jobEnv string
osRelease []byte
@@ -1769,10 +1769,11 @@ func TestRunContextImageOS(t *testing.T) {
{imageEnv: map[string]string{"ImageOS": "ubuntu24-arm64"}, osRelease: ubuntu, want: "ubuntu24-arm64"},
{imageEnv: map[string]string{"ImageOS": "ubuntu24-arm64"}, jobEnv: "ImageOS: ubuntu20", want: "ubuntu20"},
{osRelease: ubuntu, want: "ubuntu22"},
{osRelease: tarArchive(t, tarEntry{name: "os-release", body: "ID=debian\nVERSION_ID=\"12\"\n"}), want: "ubuntu24"},
{image: "docker.gitea.com/runner-images:ubuntu-24.04", osRelease: debian, want: "ubuntu24"},
{image: "docker.gitea.com/runner-images:ubuntu-latest-slim", osRelease: debian},
} {
rc := createRunsOnRunContext(t, "ubuntu-latest\nenv: {"+tc.jobEnv+"}")
rc.platformImage = "docker.gitea.com/runner-images:ubuntu-latest"
rc.platformImage = tc.image
rc.JobContainer = imageEnvContainer{&container.HostEnvironment{ActPath: t.TempDir()}, tc.imageEnv, tc.osRelease}
require.NoError(t, rc.copyWorkflowFiles()(ctx))
require.NoError(t, evaluateJobEnvAndDefaults(ctx, rc))
@@ -1780,30 +1781,6 @@ func TestRunContextImageOS(t *testing.T) {
require.NoError(t, setupEnv(ctx, &stepRun{RunContext: rc, Step: &model.Step{}, env: env}))
assert.Equal(t, tc.want, env["ImageOS"])
}
})
t.Run("prefers the release in the resolved image tag", func(t *testing.T) {
rc := createRunsOnRunContext(t, "ubuntu-latest")
rc.Config.PlatformPicker = func([]string) string { return "docker.gitea.com/runner-images:ubuntu-24.04" }
require.NoError(t, rc.resolvePlatformImage(ctx))
assert.Equal(t, "ubuntu24", rc.imageOS(ctx))
})
t.Run("falls back to the runs-on label", func(t *testing.T) {
rc := createRunsOnRunContext(t, "ubuntu-22.04")
rc.Config.PlatformPicker = func([]string) string { return "some-image" }
require.NoError(t, rc.resolvePlatformImage(ctx))
assert.Equal(t, "ubuntu22", rc.imageOS(ctx))
})
t.Run("reports the current release for the rolling ubuntu-latest label", func(t *testing.T) {
assert.Equal(t, "ubuntu24", createRunsOnRunContext(t, "ubuntu-latest").imageOS(ctx))
})
t.Run("is empty for the synthetic job of a composite action", func(t *testing.T) {
rc := createIfTestRunContext(map[string]*model.Job{"job1": {}})
assert.Empty(t, rc.imageOS(ctx))
})
}
func TestRunContextGetRunnerContext(t *testing.T) {