Hands-On Docker for Microservices with Python
上QQ阅读APP看书,第一时间看更新

Leveraging the pipeline concept

CI tools help to clarify how a build should proceed and work around the concept of a pipeline. A pipeline is a collection of stages. If any of them are not successful, the pipeline stops.

Each stage in the pipeline can produce elements that could be used at later stages or are available as the final product of the full build. These final elements are known as artifacts.

Let's look at an example of a pipeline:

The first stage pulls the latest commit from the source control system. Then, we build all the containers and run both tests and the static analysis. If all has been successful, we tag the resulting server container and push it to the registry.

The order in which these stages run should be oriented at detecting problems as quickly as possible to give quick feedback. For example, if the  static-analysis stage is much faster than the test stage, putting the analysis stage first will make a failing build finish earlier. Be aware of which parts can be executed earlier to reduce the feedback time.

CI tools normally allow great configuration in pipelines, including the possibility of running different stages in parallel. To be able to run stages in parallel, they need to be able to be parallelizable, meaning that they should not change the same elements.

If the chosen CI tool allows running stages in parallel, the pipeline could be defined as follows:

Note that we build the database and the test images in parallel. The next stage builds the rest of the elements, which are already available in the cache, so it will be very quick. Both the tests and the static analysis can run in parallel, in two different containers.

This may speed up complex builds.

Be sure to validate that the amount of time taken reduces. There are cases where the time taken will be very similar. For example, static analysis could be very fast or the hardware you run it on may be not powerful enough to build things in parallel, making the time taken to build in parallel and sequentially very similar. So, always validate your assumptions.

The pipeline is described in a script specific to the Travis CI tool. We'll look at an example with Travis CI later.