Handle sequential execution of scripts in Travis CI
Many projects on Github use Travis to automatically execute certain scripts on every build. Unfortunately, Travis build does not fail fast. What this means is that Travis continues the build even after it encounters errors in the building process causing it to fail.
There are many projects that involve execution of multiple scripts sequentially with each dependent on the proper execution of the previous script. This requires that if one of the scripts fails then the process should stop there and none of the scripts following it should execute. Failing to achieve this can lead to some unprecedented outcomes.
One solution for this would be to handle those statements in all the scripts that could lead to a failure in the build process. Opting this approach could be burdensome as there can be multiple scripts with a huge number of commands. Also, it is hard to realise which command could fail to execute. Instead of opting for this, a favoured approach would be to use ‘Build Stages’ offered in Travis CI.
Using Travis’ build stages
build stages, which is a way to group jobs, and run jobs in each stage in parallel, but run one stage after another sequentially. Put simply,
Build Stages allows us make one job run only if several other, parallel jobs have completed successfully.
These build stages can be used to execute one script at each stage, with Travis exiting at the stage in which the errored script is executed.
jobs: include: - stage: Install Virtual Environment script: - pip install --user virtualenv - stage: Generate Documentation script: - source <(curl -s https://raw.githubusercontent.com/imujjwal96/mydoc/master/generate.sh) - stage: Publish Documentation script: - source <(curl -s https://github.com/imujjwal96/mydoc/master/publish_docs.sh)
Consider the Travis configuration defined above. This configuration describes the three stages that are involved in a real-world project, Yaydoc, which is used to
Automatically Generate and Deploy the Documentation to Github Pages. It is clear from the configuration block that the three critical stages involved in the process of generating documentations using Yaydoc are
- Installing and Setting Up Virtual Environment
- Generation of Documentation
- Publishing Documentation
It would not be wise for the system to publish documentations that are not generated properly. Hence, these three scripts are critical, with the execution of each script dependent on the successful execution of the previous script. Each script is defined in a separate stage and thus a failed ‘Generate documentation’ script stops the build.
If the above scripts were to execute normally, the ‘Publish documentation’ script would have executed even after the 'Generate documentation' script fails.