mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-09-15 18:57:01 +00:00
support for container option: --hostname (#809)
This commit is contained in:
parent
cbcc52c438
commit
0e52836daf
4 changed files with 50 additions and 0 deletions
|
@ -52,6 +52,7 @@ type NewContainerInput struct {
|
||||||
Privileged bool
|
Privileged bool
|
||||||
UsernsMode string
|
UsernsMode string
|
||||||
Platform string
|
Platform string
|
||||||
|
Hostname string
|
||||||
}
|
}
|
||||||
|
|
||||||
// FileEntry is a file to copy to a container
|
// FileEntry is a file to copy to a container
|
||||||
|
@ -302,6 +303,7 @@ func (cr *containerReference) create(capAdd []string, capDrop []string) common.E
|
||||||
WorkingDir: input.WorkingDir,
|
WorkingDir: input.WorkingDir,
|
||||||
Env: input.Env,
|
Env: input.Env,
|
||||||
Tty: isTerminal,
|
Tty: isTerminal,
|
||||||
|
Hostname: input.Hostname,
|
||||||
}
|
}
|
||||||
|
|
||||||
mounts := make([]mount.Mount, 0)
|
mounts := make([]mount.Mount, 0)
|
||||||
|
|
|
@ -11,6 +11,9 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/google/shlex"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
|
|
||||||
"github.com/mitchellh/go-homedir"
|
"github.com/mitchellh/go-homedir"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
@ -96,6 +99,7 @@ func (rc *RunContext) GetBindsAndMounts() ([]string, map[string]string) {
|
||||||
|
|
||||||
func (rc *RunContext) startJobContainer() common.Executor {
|
func (rc *RunContext) startJobContainer() common.Executor {
|
||||||
image := rc.platformImage()
|
image := rc.platformImage()
|
||||||
|
hostname := rc.hostname()
|
||||||
|
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
rawLogger := common.Logger(ctx).WithField("raw_output", true)
|
rawLogger := common.Logger(ctx).WithField("raw_output", true)
|
||||||
|
@ -136,6 +140,7 @@ func (rc *RunContext) startJobContainer() common.Executor {
|
||||||
Privileged: rc.Config.Privileged,
|
Privileged: rc.Config.Privileged,
|
||||||
UsernsMode: rc.Config.UsernsMode,
|
UsernsMode: rc.Config.UsernsMode,
|
||||||
Platform: rc.Config.ContainerArchitecture,
|
Platform: rc.Config.ContainerArchitecture,
|
||||||
|
Hostname: hostname,
|
||||||
})
|
})
|
||||||
|
|
||||||
var copyWorkspace bool
|
var copyWorkspace bool
|
||||||
|
@ -304,6 +309,28 @@ func (rc *RunContext) platformImage() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rc *RunContext) hostname() string {
|
||||||
|
job := rc.Run.Job()
|
||||||
|
c := job.Container()
|
||||||
|
if c == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
optionsFlags := pflag.NewFlagSet("container_options", pflag.ContinueOnError)
|
||||||
|
hostname := optionsFlags.StringP("hostname", "h", "", "")
|
||||||
|
optionsArgs, err := shlex.Split(c.Options)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("Cannot parse container options: %s", c.Options)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
err = optionsFlags.Parse(optionsArgs)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("Cannot parse container options: %s", c.Options)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return *hostname
|
||||||
|
}
|
||||||
|
|
||||||
func (rc *RunContext) isEnabled(ctx context.Context) bool {
|
func (rc *RunContext) isEnabled(ctx context.Context) bool {
|
||||||
job := rc.Run.Job()
|
job := rc.Run.Job()
|
||||||
l := common.Logger(ctx)
|
l := common.Logger(ctx)
|
||||||
|
|
|
@ -103,6 +103,7 @@ func TestRunEvent(t *testing.T) {
|
||||||
{"testdata", "shells/sh", "push", "", platforms, ""},
|
{"testdata", "shells/sh", "push", "", platforms, ""},
|
||||||
{"testdata", "job-container", "push", "", platforms, ""},
|
{"testdata", "job-container", "push", "", platforms, ""},
|
||||||
{"testdata", "job-container-non-root", "push", "", platforms, ""},
|
{"testdata", "job-container-non-root", "push", "", platforms, ""},
|
||||||
|
{"testdata", "container-hostname", "push", "", platforms, ""},
|
||||||
{"testdata", "uses-docker-url", "push", "", platforms, ""},
|
{"testdata", "uses-docker-url", "push", "", platforms, ""},
|
||||||
{"testdata", "remote-action-docker", "push", "", platforms, ""},
|
{"testdata", "remote-action-docker", "push", "", platforms, ""},
|
||||||
{"testdata", "remote-action-js", "push", "", platforms, ""},
|
{"testdata", "remote-action-js", "push", "", platforms, ""},
|
||||||
|
|
20
act/runner/testdata/container-hostname/push.yml
vendored
Normal file
20
act/runner/testdata/container-hostname/push.yml
vendored
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
name: container-hostname
|
||||||
|
on: push
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
with-hostname:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: node:12-buster-slim
|
||||||
|
options: "--hostname my.host.local"
|
||||||
|
steps:
|
||||||
|
- run: |
|
||||||
|
[[ $(uname -n) == "my.host.local" ]]
|
||||||
|
|
||||||
|
default-hostname:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: node:12-buster-slim
|
||||||
|
steps:
|
||||||
|
- run: |
|
||||||
|
[[ $(uname -n) ]] && [[ $(uname -n) != "my.host.local" ]]
|
Loading…
Add table
Add a link
Reference in a new issue