Defining a Pipeline

Learn how to make the tool more maintainable and easy to extend by adding a pipeline.

Overview

At this point, our tool executes the first step in the CI pipeline: Go Build. In its current state, the information to execute this step is hard-coded into the run() function. While this is a valid approach, adding more steps to this tool using the same approach would cause extensive code repetition. We want a tool that’s maintainable and easy to extend, so let’s make a change to the program’s structure to make the code more reusable.

To do that, we’ll refactor the part of the run() function that executes the external program into its own function. To make it easier to configure, let’s add a custom type step that represents a pipeline step and associate the method execute() with it. We’ll also add a constructor function called newStep() to create a new step. By doing this, when we want to add a new step to the pipeline, we instantiate the step type with the appropriate values.

Creating the step.go file

Let’s create a file called step.go in the goci directory. Include the package definition and the import sections. Use the os/exec package to execute external programs:

Get hands-on with 1400+ tech skills courses.