mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-20 14:53:42 +00:00
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:
1 parent
bcc0b715cd
commit
ff4f626871
2 files changed
+27
-72
No files matched your search
@@ -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 != "" {
|
if set || imageOS != "" {
|
||||||
rc.defaultEnv["ImageOS"] = imageOS
|
rc.defaultEnv["ImageOS"] = imageOS
|
||||||
@@ -1889,32 +1889,10 @@ func parentDir(p string) string {
|
|||||||
return ""
|
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
|
// 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
|
// 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
|
// ("ubuntu-latest", "app:22.04", "catthehacker/ubuntu:act-22.04", or a registry port)
|
||||||
// left to the runs-on label rather than turned into a bogus OS.
|
// yields no ImageOS rather than a bogus OS.
|
||||||
var imageOSTag = regexp.MustCompile(`:(ubuntu|win|macos)-?([0-9]+)[^/]*$`)
|
var imageOSTag = regexp.MustCompile(`:(ubuntu|win|macos)-?([0-9]+)[^/]*$`)
|
||||||
|
|
||||||
// imageOSFromImage derives ImageOS from an image reference, e.g.
|
// imageOSFromImage derives ImageOS from an image reference, e.g.
|
||||||
|
|||||||
@@ -1757,53 +1757,30 @@ func TestRunContextContainerImageCannotSelectBackend(t *testing.T) {
|
|||||||
|
|
||||||
func TestRunContextImageOS(t *testing.T) {
|
func TestRunContextImageOS(t *testing.T) {
|
||||||
ctx := t.Context()
|
ctx := t.Context()
|
||||||
|
ubuntu := tarArchive(t, tarEntry{name: "os-release", body: "ID=ubuntu\nVERSION_ID=\"22.04\"\n"})
|
||||||
t.Run("keeps an ImageOS from the image, the job env or the job's Ubuntu release", func(t *testing.T) {
|
debian := tarArchive(t, tarEntry{name: "os-release", body: "ID=debian\nVERSION_ID=\"12\"\n"})
|
||||||
ubuntu := tarArchive(t, tarEntry{name: "os-release", body: "ID=ubuntu\nVERSION_ID=\"22.04\"\n"})
|
for _, tc := range []struct {
|
||||||
for _, tc := range []struct {
|
image string
|
||||||
imageEnv map[string]string
|
imageEnv map[string]string
|
||||||
jobEnv string
|
jobEnv string
|
||||||
osRelease []byte
|
osRelease []byte
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{imageEnv: map[string]string{"ImageOS": "ubuntu24-arm64"}, osRelease: ubuntu, want: "ubuntu24-arm64"},
|
{imageEnv: map[string]string{"ImageOS": "ubuntu24-arm64"}, osRelease: ubuntu, want: "ubuntu24-arm64"},
|
||||||
{imageEnv: map[string]string{"ImageOS": "ubuntu24-arm64"}, jobEnv: "ImageOS: ubuntu20", want: "ubuntu20"},
|
{imageEnv: map[string]string{"ImageOS": "ubuntu24-arm64"}, jobEnv: "ImageOS: ubuntu20", want: "ubuntu20"},
|
||||||
{osRelease: ubuntu, want: "ubuntu22"},
|
{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 := createRunsOnRunContext(t, "ubuntu-latest\nenv: {"+tc.jobEnv+"}")
|
||||||
rc.JobContainer = imageEnvContainer{&container.HostEnvironment{ActPath: t.TempDir()}, tc.imageEnv, tc.osRelease}
|
rc.platformImage = tc.image
|
||||||
require.NoError(t, rc.copyWorkflowFiles()(ctx))
|
rc.JobContainer = imageEnvContainer{&container.HostEnvironment{ActPath: t.TempDir()}, tc.imageEnv, tc.osRelease}
|
||||||
require.NoError(t, evaluateJobEnvAndDefaults(ctx, rc))
|
require.NoError(t, rc.copyWorkflowFiles()(ctx))
|
||||||
env := map[string]string{}
|
require.NoError(t, evaluateJobEnvAndDefaults(ctx, rc))
|
||||||
require.NoError(t, setupEnv(ctx, &stepRun{RunContext: rc, Step: &model.Step{}, env: env}))
|
env := map[string]string{}
|
||||||
assert.Equal(t, tc.want, env["ImageOS"])
|
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) {
|
func TestRunContextGetRunnerContext(t *testing.T) {
|
||||||
|
|||||||
Reference in new issue
Block a user