Search⌘ K

Defining a Pipeline

Discover how to structure Go command-line tools by defining a custom pipeline step type with methods to execute external programs. Learn to refactor code for reusability and extend pipelines easily, improving maintainability and automation in CI workflows.

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 ...