Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluate if condition when calling a reusable workflow #2087

Merged
merged 3 commits into from
Nov 12, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions pkg/runner/run_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,23 @@
if rc.Config.ContainerNetworkMode == "" {
return "host", false
}
return string(rc.Config.ContainerNetworkMode), false

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L100

Added line #L100 was not covered by tests
}

func getDockerDaemonSocketMountPath(daemonPath string) string {
if protoIndex := strings.Index(daemonPath, "://"); protoIndex != -1 {
scheme := daemonPath[:protoIndex]
if strings.EqualFold(scheme, "npipe") {
// linux container mount on windows, use the default socket path of the VM / wsl2
return "/var/run/docker.sock"
} else if strings.EqualFold(scheme, "unix") {
return daemonPath[protoIndex+3:]
} else if strings.IndexFunc(scheme, func(r rune) bool {
return (r < 'a' || r > 'z') && (r < 'A' || r > 'Z')
}) == -1 {
// unknown protocol use default
return "/var/run/docker.sock"
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L105-L116

Added lines #L105 - L116 were not covered by tests
}
return daemonPath
}
Expand Down Expand Up @@ -291,13 +291,13 @@
}
username, password, err = rc.handleServiceCredentials(ctx, spec.Credentials)
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

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))
Expand All @@ -306,8 +306,8 @@
}
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

serviceContainerName := createContainerName(rc.jobContainerName(), serviceID)
c := container.NewContainer(&container.NewContainerInput{
Expand Down Expand Up @@ -346,8 +346,8 @@
if len(rc.ServiceContainers) > 0 {
logger.Infof("Cleaning up services for job %s", rc.JobName)
if err := rc.stopServiceContainers()(ctx); err != nil {
logger.Errorf("Error while cleaning services: %v", err)
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L349-L350

Added lines #L349 - L350 were not covered by tests
if createAndDeleteNetwork {
// clean network if it has been created by act
// if using service containers
Expand All @@ -355,8 +355,8 @@
// so, we should remove the network at last.
logger.Infof("Cleaning up network for job %s, and network name is: %s", rc.JobName, networkName)
if err := container.NewDockerNetworkRemoveExecutor(networkName)(ctx); err != nil {
logger.Errorf("Error while cleaning network: %v", err)
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L358-L359

Added lines #L358 - L359 were not covered by tests
}
}
return nil
Expand Down Expand Up @@ -427,11 +427,11 @@
if rc.ExtraPath != nil && len(rc.ExtraPath) > 0 {
path := rc.JobContainer.GetPathVariableName()
if rc.JobContainer.IsEnvironmentCaseInsensitive() {
// On windows system Path and PATH could also be in the map
for k := range *env {
if strings.EqualFold(path, k) {
path = k
break

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L430-L434

Added lines #L430 - L434 were not covered by tests
}
}
}
Expand All @@ -444,8 +444,8 @@
}
}
if len(cpath) == 0 {
cpath = rc.JobContainer.DefaultPathVariable()
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L447-L448

Added lines #L447 - L448 were not covered by tests
(*env)[path] = cpath
}
(*env)[path] = rc.JobContainer.JoinPathVariable(append(rc.ExtraPath, (*env)[path])...)
Expand Down Expand Up @@ -526,16 +526,16 @@
// ActionCacheDir is for rc
func (rc *RunContext) ActionCacheDir() string {
if rc.Config.ActionCacheDir != "" {
return rc.Config.ActionCacheDir
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L529-L530

Added lines #L529 - L530 were not covered by tests
var xdgCache string
var ok bool
if xdgCache, ok = os.LookupEnv("XDG_CACHE_HOME"); !ok || xdgCache == "" {
if home, err := os.UserHomeDir(); err == nil {
xdgCache = filepath.Join(home, ".cache")
} else if xdgCache, err = filepath.Abs("."); err != nil {
// It's almost impossible to get here, so the temp dir is a good fallback
xdgCache = os.TempDir()

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L537-L538

Added lines #L537 - L538 were not covered by tests
}
}
return filepath.Join(xdgCache, "act")
Expand Down Expand Up @@ -607,8 +607,8 @@
executor = newLocalReusableWorkflowExecutor(rc)
case model.JobTypeReusableWorkflowRemote:
executor = newRemoteReusableWorkflowExecutor(rc)
case model.JobTypeInvalid:
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L610-L611

Added lines #L610 - L611 were not covered by tests
}

return func(ctx context.Context) error {
Expand Down Expand Up @@ -686,20 +686,22 @@
jobType, jobTypeErr := job.Type()

if runJobErr != nil {
return false, fmt.Errorf(" \u274C Error in if-expression: \"if: %s\" (%s)", job.If.Value, runJobErr)
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L689-L690

Added lines #L689 - L690 were not covered by tests

if jobType == model.JobTypeInvalid {
return false, jobTypeErr

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L693

Added line #L693 was not covered by tests
} else if jobType != model.JobTypeDefault {
return true, nil
}

if !runJob {
l.WithField("jobResult", "skipped").Debugf("Skipping job '%s' due to '%s'", job.Name, job.If.Value)
return false, nil
}

if jobType != model.JobTypeDefault {
return true, nil
}

img := rc.platformImage(ctx)
if img == "" {
for _, platformName := range rc.runsOnPlatformNames(ctx) {
Expand Down Expand Up @@ -846,13 +848,13 @@
}
// allow to be overridden by user
if rc.Config.Env["GITHUB_SERVER_URL"] != "" {
ghc.ServerURL = rc.Config.Env["GITHUB_SERVER_URL"]
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L851-L852

Added lines #L851 - L852 were not covered by tests
if rc.Config.Env["GITHUB_API_URL"] != "" {
ghc.APIURL = rc.Config.Env["GITHUB_API_URL"]
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L854-L855

Added lines #L854 - L855 were not covered by tests
if rc.Config.Env["GITHUB_GRAPHQL_URL"] != "" {
ghc.GraphQLURL = rc.Config.Env["GITHUB_GRAPHQL_URL"]

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L857

Added line #L857 was not covered by tests
}

return ghc
Expand Down Expand Up @@ -953,7 +955,7 @@
func setActionRuntimeVars(rc *RunContext, env map[string]string) {
actionsRuntimeURL := os.Getenv("ACTIONS_RUNTIME_URL")
if actionsRuntimeURL == "" {
actionsRuntimeURL = fmt.Sprintf("http://%s:%s/", rc.Config.ArtifactServerAddr, rc.Config.ArtifactServerPort)

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L958

Added line #L958 was not covered by tests
}
env["ACTIONS_RUNTIME_URL"] = actionsRuntimeURL

Expand All @@ -975,14 +977,14 @@
}

if container.Credentials != nil && len(container.Credentials) != 2 {
err := fmt.Errorf("invalid property count for key 'credentials:'")
return "", "", err

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L980-L981

Added lines #L980 - L981 were not covered by tests
}

ee := rc.NewExpressionEvaluator(ctx)
if username = ee.Interpolate(ctx, container.Credentials["username"]); username == "" {
err := fmt.Errorf("failed to interpolate container.credentials.username")
return "", "", err

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L986-L987

Added lines #L986 - L987 were not covered by tests
}
if password = ee.Interpolate(ctx, container.Credentials["password"]); password == "" {
err := fmt.Errorf("failed to interpolate container.credentials.password")
Expand All @@ -990,41 +992,41 @@
}

if container.Credentials["username"] == "" || container.Credentials["password"] == "" {
err := fmt.Errorf("container.credentials cannot be empty")
return "", "", err
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L995-L997

Added lines #L995 - L997 were not covered by tests

return username, password, nil

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L999

Added line #L999 was not covered by tests
}

func (rc *RunContext) handleServiceCredentials(ctx context.Context, creds map[string]string) (username, password string, err error) {
if creds == nil {
return
}
if len(creds) != 2 {
err = fmt.Errorf("invalid property count for key 'credentials:'")
return
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L1006-L1009

Added lines #L1006 - L1009 were not covered by tests

ee := rc.NewExpressionEvaluator(ctx)
if username = ee.Interpolate(ctx, creds["username"]); username == "" {
err = fmt.Errorf("failed to interpolate credentials.username")
return
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L1011-L1015

Added lines #L1011 - L1015 were not covered by tests

if password = ee.Interpolate(ctx, creds["password"]); password == "" {
err = fmt.Errorf("failed to interpolate credentials.password")

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L1017-L1018

Added lines #L1017 - L1018 were not covered by tests
return
}

return

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L1022

Added line #L1022 was not covered by tests
}

// GetServiceBindsAndMounts returns the binds and mounts for the service container, resolving paths as appopriate
func (rc *RunContext) GetServiceBindsAndMounts(svcVolumes []string) ([]string, map[string]string) {
if rc.Config.ContainerDaemonSocket == "" {
rc.Config.ContainerDaemonSocket = "/var/run/docker.sock"
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L1028-L1029

Added lines #L1028 - L1029 were not covered by tests
binds := []string{}
if rc.Config.ContainerDaemonSocket != "-" {
daemonPath := getDockerDaemonSocketMountPath(rc.Config.ContainerDaemonSocket)
Expand All @@ -1034,14 +1036,14 @@
mounts := map[string]string{}

for _, v := range svcVolumes {
if !strings.Contains(v, ":") || filepath.IsAbs(v) {
// Bind anonymous volume or host file.
binds = append(binds, v)
} else {
// Mount existing volume.
paths := strings.SplitN(v, ":", 2)
mounts[paths[0]] = paths[1]
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L1039-L1046

Added lines #L1039 - L1046 were not covered by tests
}

return binds, mounts
Expand Down
11 changes: 11 additions & 0 deletions pkg/runner/run_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,17 @@ if: always()`, ""),
})
rc.Run.JobID = "job2"
assertObject.True(rc.isEnabled(context.Background()))

rc = createIfTestRunContext(map[string]*model.Job{
"job1": createJob(t, `uses: ./.github/workflows/reusable.yml`, ""),
})
assertObject.True(rc.isEnabled(context.Background()))

rc = createIfTestRunContext(map[string]*model.Job{
"job1": createJob(t, `uses: ./.github/workflows/reusable.yml
if: false`, ""),
})
assertObject.False(rc.isEnabled(context.Background()))
}

func TestRunContextGetEnv(t *testing.T) {
Expand Down