feat: specify matrix on command line (#1675)
* added matrix option * select the correct subset of matrix configuration after producing all the matrix configuration * add tests * update readme * lint fix * remove matrix from readme --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
		| @@ -20,40 +20,41 @@ type Runner interface { | ||||
|  | ||||
| // Config contains the config for a new runner | ||||
| type Config struct { | ||||
| 	Actor                              string            // the user that triggered the event | ||||
| 	Workdir                            string            // path to working directory | ||||
| 	BindWorkdir                        bool              // bind the workdir to the job container | ||||
| 	EventName                          string            // name of event to run | ||||
| 	EventPath                          string            // path to JSON file to use for event.json in containers | ||||
| 	DefaultBranch                      string            // name of the main branch for this repository | ||||
| 	ReuseContainers                    bool              // reuse containers to maintain state | ||||
| 	ForcePull                          bool              // force pulling of the image, even if already present | ||||
| 	ForceRebuild                       bool              // force rebuilding local docker image action | ||||
| 	LogOutput                          bool              // log the output from docker run | ||||
| 	JSONLogger                         bool              // use json or text logger | ||||
| 	Env                                map[string]string // env for containers | ||||
| 	Inputs                             map[string]string // manually passed action inputs | ||||
| 	Secrets                            map[string]string // list of secrets | ||||
| 	Token                              string            // GitHub token | ||||
| 	InsecureSecrets                    bool              // switch hiding output when printing to terminal | ||||
| 	Platforms                          map[string]string // list of platforms | ||||
| 	Privileged                         bool              // use privileged mode | ||||
| 	UsernsMode                         string            // user namespace to use | ||||
| 	ContainerArchitecture              string            // Desired OS/architecture platform for running containers | ||||
| 	ContainerDaemonSocket              string            // Path to Docker daemon socket | ||||
| 	ContainerOptions                   string            // Options for the job container | ||||
| 	UseGitIgnore                       bool              // controls if paths in .gitignore should not be copied into container, default true | ||||
| 	GitHubInstance                     string            // GitHub instance to use, default "github.com" | ||||
| 	ContainerCapAdd                    []string          // list of kernel capabilities to add to the containers | ||||
| 	ContainerCapDrop                   []string          // list of kernel capabilities to remove from the containers | ||||
| 	AutoRemove                         bool              // controls if the container is automatically removed upon workflow completion | ||||
| 	ArtifactServerPath                 string            // the path where the artifact server stores uploads | ||||
| 	ArtifactServerAddr                 string            // the address the artifact server binds to | ||||
| 	ArtifactServerPort                 string            // the port the artifact server binds to | ||||
| 	NoSkipCheckout                     bool              // do not skip actions/checkout | ||||
| 	RemoteName                         string            // remote name in local git repo config | ||||
| 	ReplaceGheActionWithGithubCom      []string          // Use actions from GitHub Enterprise instance to GitHub | ||||
| 	ReplaceGheActionTokenWithGithubCom string            // Token of private action repo on GitHub. | ||||
| 	Actor                              string                     // the user that triggered the event | ||||
| 	Workdir                            string                     // path to working directory | ||||
| 	BindWorkdir                        bool                       // bind the workdir to the job container | ||||
| 	EventName                          string                     // name of event to run | ||||
| 	EventPath                          string                     // path to JSON file to use for event.json in containers | ||||
| 	DefaultBranch                      string                     // name of the main branch for this repository | ||||
| 	ReuseContainers                    bool                       // reuse containers to maintain state | ||||
| 	ForcePull                          bool                       // force pulling of the image, even if already present | ||||
| 	ForceRebuild                       bool                       // force rebuilding local docker image action | ||||
| 	LogOutput                          bool                       // log the output from docker run | ||||
| 	JSONLogger                         bool                       // use json or text logger | ||||
| 	Env                                map[string]string          // env for containers | ||||
| 	Inputs                             map[string]string          // manually passed action inputs | ||||
| 	Secrets                            map[string]string          // list of secrets | ||||
| 	Token                              string                     // GitHub token | ||||
| 	InsecureSecrets                    bool                       // switch hiding output when printing to terminal | ||||
| 	Platforms                          map[string]string          // list of platforms | ||||
| 	Privileged                         bool                       // use privileged mode | ||||
| 	UsernsMode                         string                     // user namespace to use | ||||
| 	ContainerArchitecture              string                     // Desired OS/architecture platform for running containers | ||||
| 	ContainerDaemonSocket              string                     // Path to Docker daemon socket | ||||
| 	ContainerOptions                   string                     // Options for the job container | ||||
| 	UseGitIgnore                       bool                       // controls if paths in .gitignore should not be copied into container, default true | ||||
| 	GitHubInstance                     string                     // GitHub instance to use, default "github.com" | ||||
| 	ContainerCapAdd                    []string                   // list of kernel capabilities to add to the containers | ||||
| 	ContainerCapDrop                   []string                   // list of kernel capabilities to remove from the containers | ||||
| 	AutoRemove                         bool                       // controls if the container is automatically removed upon workflow completion | ||||
| 	ArtifactServerPath                 string                     // the path where the artifact server stores uploads | ||||
| 	ArtifactServerAddr                 string                     // the address the artifact server binds to | ||||
| 	ArtifactServerPort                 string                     // the port the artifact server binds to | ||||
| 	NoSkipCheckout                     bool                       // do not skip actions/checkout | ||||
| 	RemoteName                         string                     // remote name in local git repo config | ||||
| 	ReplaceGheActionWithGithubCom      []string                   // Use actions from GitHub Enterprise instance to GitHub | ||||
| 	ReplaceGheActionTokenWithGithubCom string                     // Token of private action repo on GitHub. | ||||
| 	Matrix                             map[string]map[string]bool // Matrix config to run | ||||
| } | ||||
|  | ||||
| type caller struct { | ||||
| @@ -116,7 +117,10 @@ func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor { | ||||
| 						log.Errorf("Error while evaluating matrix: %v", err) | ||||
| 					} | ||||
| 				} | ||||
| 				matrixes := job.GetMatrixes() | ||||
|  | ||||
| 				matrixes := selectMatrixes(job.GetMatrixes(), runner.config.Matrix) | ||||
| 				log.Debugf("Final matrix after applying user inclusions '%v'", matrixes) | ||||
|  | ||||
| 				maxParallel := 4 | ||||
| 				if job.Strategy != nil { | ||||
| 					maxParallel = job.Strategy.MaxParallel | ||||
| @@ -171,6 +175,25 @@ func handleFailure(plan *model.Plan) common.Executor { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func selectMatrixes(originalMatrixes []map[string]interface{}, targetMatrixValues map[string]map[string]bool) []map[string]interface{} { | ||||
| 	matrixes := make([]map[string]interface{}, 0) | ||||
| 	for _, original := range originalMatrixes { | ||||
| 		flag := true | ||||
| 		for key, val := range original { | ||||
| 			if allowedVals, ok := targetMatrixValues[key]; ok { | ||||
| 				valToString := fmt.Sprintf("%v", val) | ||||
| 				if _, ok := allowedVals[valToString]; !ok { | ||||
| 					flag = false | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 		if flag { | ||||
| 			matrixes = append(matrixes, original) | ||||
| 		} | ||||
| 	} | ||||
| 	return matrixes | ||||
| } | ||||
|  | ||||
| func (runner *runnerImpl) newRunContext(ctx context.Context, run *model.Run, matrix map[string]interface{}) *RunContext { | ||||
| 	rc := &RunContext{ | ||||
| 		Config:      runner.config, | ||||
|   | ||||
| @@ -186,6 +186,7 @@ func (j *TestJobFileInfo) runTest(ctx context.Context, t *testing.T, cfg *Config | ||||
| 		Inputs:                cfg.Inputs, | ||||
| 		GitHubInstance:        "github.com", | ||||
| 		ContainerArchitecture: cfg.ContainerArchitecture, | ||||
| 		Matrix:                cfg.Matrix, | ||||
| 	} | ||||
|  | ||||
| 	runner, err := New(runnerConfig) | ||||
| @@ -584,3 +585,30 @@ func TestRunEventPullRequest(t *testing.T) { | ||||
|  | ||||
| 	tjfi.runTest(context.Background(), t, &Config{EventPath: filepath.Join(workdir, workflowPath, "event.json")}) | ||||
| } | ||||
|  | ||||
| func TestRunMatrixWithUserDefinedInclusions(t *testing.T) { | ||||
| 	if testing.Short() { | ||||
| 		t.Skip("skipping integration test") | ||||
| 	} | ||||
| 	workflowPath := "matrix-with-user-inclusions" | ||||
|  | ||||
| 	tjfi := TestJobFileInfo{ | ||||
| 		workdir:      workdir, | ||||
| 		workflowPath: workflowPath, | ||||
| 		eventName:    "push", | ||||
| 		errorMessage: "", | ||||
| 		platforms:    platforms, | ||||
| 	} | ||||
|  | ||||
| 	matrix := map[string]map[string]bool{ | ||||
| 		"node": { | ||||
| 			"8":   true, | ||||
| 			"8.x": true, | ||||
| 		}, | ||||
| 		"os": { | ||||
| 			"ubuntu-18.04": true, | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	tjfi.runTest(context.Background(), t, &Config{Matrix: matrix}) | ||||
| } | ||||
|   | ||||
							
								
								
									
										34
									
								
								pkg/runner/testdata/matrix-with-user-inclusions/push.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								pkg/runner/testdata/matrix-with-user-inclusions/push.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | ||||
| name: matrix-with-user-inclusions | ||||
| on: push | ||||
|  | ||||
| jobs: | ||||
|   build: | ||||
|     name: PHP ${{ matrix.os }} ${{ matrix.node}} | ||||
|     runs-on: ubuntu-latest | ||||
|     steps: | ||||
|       - run: | | ||||
|           echo ${NODE_VERSION} | grep 8 | ||||
|           echo ${OS_VERSION} | grep ubuntu-18.04 | ||||
|         env: | ||||
|           NODE_VERSION: ${{ matrix.node }} | ||||
|           OS_VERSION: ${{ matrix.os }} | ||||
|     strategy: | ||||
|       matrix: | ||||
|         os: [ubuntu-18.04, macos-latest] | ||||
|         node: [4, 6, 8, 10] | ||||
|         exclude: | ||||
|           - os: macos-latest | ||||
|             node: 4 | ||||
|         include: | ||||
|           - os: ubuntu-16.04 | ||||
|             node: 10 | ||||
|  | ||||
|   test: | ||||
|     runs-on: ubuntu-latest | ||||
|     strategy: | ||||
|       matrix: | ||||
|         node: [8.x, 10.x, 12.x, 13.x] | ||||
|     steps: | ||||
|       - run: echo ${NODE_VERSION} | grep 8.x | ||||
|         env: | ||||
|           NODE_VERSION: ${{ matrix.node }} | ||||
		Reference in New Issue
	
	Block a user