Skip to content

Commit

Permalink
Merge branch 'master' into fix-reusable-workflow-if
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherHX committed Nov 12, 2023
2 parents 873acb4 + 04011b6 commit 7fe5e12
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
4 changes: 3 additions & 1 deletion pkg/container/docker_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,9 @@ func (cr *containerReference) create(capAdd []string, capDrop []string) common.E

var networkingConfig *network.NetworkingConfig
logger.Debugf("input.NetworkAliases ==> %v", input.NetworkAliases)
if hostConfig.NetworkMode.IsUserDefined() && len(input.NetworkAliases) > 0 {
n := hostConfig.NetworkMode
// TODO: use IsUserDefined() once it's windows implementation matches the unix one
if !n.IsDefault() && !n.IsBridge() && !n.IsHost() && !n.IsNone() && !n.IsContainer() && len(input.NetworkAliases) > 0 {
endpointConfig := &network.EndpointSettings{
Aliases: input.NetworkAliases,
}
Expand Down
28 changes: 26 additions & 2 deletions pkg/model/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,39 @@ func (j *Job) Needs() []string {
// RunsOn list for Job
func (j *Job) RunsOn() []string {
switch j.RawRunsOn.Kind {
case yaml.MappingNode:
var val struct {
Group string
Labels yaml.Node
}

if !decodeNode(j.RawRunsOn, &val) {
return nil
}

Check warning on line 286 in pkg/model/workflow.go

View check run for this annotation

Codecov / codecov/patch

pkg/model/workflow.go#L285-L286

Added lines #L285 - L286 were not covered by tests

labels := nodeAsStringSlice(val.Labels)

if val.Group != "" {
labels = append(labels, val.Group)
}

return labels
default:
return nodeAsStringSlice(j.RawRunsOn)

Check warning on line 296 in pkg/model/workflow.go

View check run for this annotation

Codecov / codecov/patch

pkg/model/workflow.go#L295-L296

Added lines #L295 - L296 were not covered by tests
}
}

func nodeAsStringSlice(node yaml.Node) []string {
switch node.Kind {
case yaml.ScalarNode:
var val string
if !decodeNode(j.RawRunsOn, &val) {
if !decodeNode(node, &val) {
return nil

Check warning on line 305 in pkg/model/workflow.go

View check run for this annotation

Codecov / codecov/patch

pkg/model/workflow.go#L305

Added line #L305 was not covered by tests
}
return []string{val}
case yaml.SequenceNode:
var val []string
if !decodeNode(j.RawRunsOn, &val) {
if !decodeNode(node, &val) {
return nil

Check warning on line 311 in pkg/model/workflow.go

View check run for this annotation

Codecov / codecov/patch

pkg/model/workflow.go#L311

Added line #L311 was not covered by tests
}
return val
Expand Down
35 changes: 35 additions & 0 deletions pkg/model/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,41 @@ jobs:
assert.Contains(t, workflow.On(), "pull_request")
}

func TestReadWorkflow_RunsOnLabels(t *testing.T) {
yaml := `
name: local-action-docker-url
jobs:
test:
container: nginx:latest
runs-on:
labels: ubuntu-latest
steps:
- uses: ./actions/docker-url`

workflow, err := ReadWorkflow(strings.NewReader(yaml))
assert.NoError(t, err, "read workflow should succeed")
assert.Equal(t, workflow.Jobs["test"].RunsOn(), []string{"ubuntu-latest"})
}

func TestReadWorkflow_RunsOnLabelsWithGroup(t *testing.T) {
yaml := `
name: local-action-docker-url
jobs:
test:
container: nginx:latest
runs-on:
labels: [ubuntu-latest]
group: linux
steps:
- uses: ./actions/docker-url`

workflow, err := ReadWorkflow(strings.NewReader(yaml))
assert.NoError(t, err, "read workflow should succeed")
assert.Equal(t, workflow.Jobs["test"].RunsOn(), []string{"ubuntu-latest", "linux"})
}

func TestReadWorkflow_StringContainer(t *testing.T) {
yaml := `
name: local-action-docker-url
Expand Down
17 changes: 13 additions & 4 deletions pkg/runner/run_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,18 @@ func (rc *RunContext) startJobContainer() common.Executor {
if err != nil {
return fmt.Errorf("failed to handle service %s credentials: %w", serviceID, err)
}

Check warning on line 295 in pkg/runner/run_context.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L294-L295

Added lines #L294 - L295 were not covered by tests
serviceBinds, serviceMounts := rc.GetServiceBindsAndMounts(spec.Volumes)

exposedPorts, portBindings, err := nat.ParsePortSpecs(spec.Ports)
interpolatedVolumes := make([]string, 0, len(spec.Volumes))
for _, volume := range spec.Volumes {
interpolatedVolumes = append(interpolatedVolumes, rc.ExprEval.Interpolate(ctx, volume))
}

Check warning on line 300 in pkg/runner/run_context.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L299-L300

Added lines #L299 - L300 were not covered by tests
serviceBinds, serviceMounts := rc.GetServiceBindsAndMounts(interpolatedVolumes)

interpolatedPorts := make([]string, 0, len(spec.Ports))
for _, port := range spec.Ports {
interpolatedPorts = append(interpolatedPorts, rc.ExprEval.Interpolate(ctx, port))
}
exposedPorts, portBindings, err := nat.ParsePortSpecs(interpolatedPorts)
if err != nil {
return fmt.Errorf("failed to parse service %s ports: %w", serviceID, err)
}

Check warning on line 310 in pkg/runner/run_context.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L309-L310

Added lines #L309 - L310 were not covered by tests
Expand All @@ -304,7 +313,7 @@ func (rc *RunContext) startJobContainer() common.Executor {
c := container.NewContainer(&container.NewContainerInput{
Name: serviceContainerName,
WorkingDir: ext.ToContainerPath(rc.Config.Workdir),
Image: spec.Image,
Image: rc.ExprEval.Interpolate(ctx, spec.Image),
Username: username,
Password: password,
Env: envs,
Expand All @@ -315,7 +324,7 @@ func (rc *RunContext) startJobContainer() common.Executor {
Privileged: rc.Config.Privileged,
UsernsMode: rc.Config.UsernsMode,
Platform: rc.Config.ContainerArchitecture,
Options: spec.Options,
Options: rc.ExprEval.Interpolate(ctx, spec.Options),
NetworkMode: networkName,
NetworkAliases: []string{serviceID},
ExposedPorts: exposedPorts,
Expand Down

0 comments on commit 7fe5e12

Please sign in to comment.