Customizing the pipeline using .travis.yml

While Travis CI is now tracking our Git repository, it is not yet smart enough to know what to do with it when an event occurs. To tell Travis CI what to do, we need to create a special file within the root of the repository. Whenever a Git event happens (for example, a Git pull request), the .travis.yml file will be processed and used to orchestrate the pipeline execution.

In the case of our smart contract, we have the following .travis.yml in the root of our Git repository:

sudo: required
services:
- docker
dist: trusty
cache:
directories:
- node_modules

script:
- make build
- make test

Since our Makefile is making use of Docker container to make the build independent of the environment in which it is run, we need to let Travis know about this.  Hence, the first three lines of the file provide an indication that the build process will make use of Docker.  The dist: trusty is fixing the Linux distribution to ensure consistency of the system behaviour.

The important lines represent the two major steps of the process:

  • Cache: This is an optimization of of the build and ensures that the node_modules is not always re-loaded every time the build runs.
  • Script: This is where the build commands are provided. In this case, the step includes the following:
    • make build: Builds the chaincode and the composer BNA
    • make test: Unit test execution

The details of the tasks for chaincode was covered in a previous chapter so we won't cover those details again.  However we will focus on the Composer build and explore the stanza of the package.json file:

[...]
"scripts": {
"prepare": "mkdirp ../dist && composer archive create --sourceType dir --sourceName . -a ../dist/trade-finance-logistics.bna",
"pretest": "npm run lint",
"lint": "eslint .",
"test": "nyc mocha -t 0 test/*.js && cucumber-js",
"coverage": "nyc check-coverage",
"posttest": "npm run coverage"
},
[...]
You will find the package.json under the trade-finance-logistics repository in the composer folder.

Lets quickly review each of the default commands generated when the composer project was generated:

  • prepare: This command will package our project into a BNA file. This script runs before the install and will use the Hyperledger composer command-line interface to create the archive.  The only modification we have done to this task was to add the sub-directory  .. to the creation of the dist directoring and output of the BNA file.
  • lint: Runs the eslint tool, which is a tool we use to analyse the code while searching patterns. The rules applied by this tool can be adjusted through the .eslintrc.yml file.
  • test: The mocha unit test framework will run the tests that are located in the project test directory and will be invoked by the nyc tool. The nyc tool is used to measure the coverage of the mocha tests.

You will then need to add these two tasks to the package.json:

  • posttest: This task is a trigger that gets activated once the test have run.  In this case it will call the coverage task.
  • coverage: Runs the nyc tool in reporting mode. This task will assess whether there are sufficient unit tests to cover the code. This task fails the build if the minimums defined in the nyc stanza of the package.json are not met. The following is a sample of this config:
        "nyc": {
"lines": 99,
"statements": 99,
"functions": 99,
"branches": 99
},

By modifying the package.json we now have "gates" that run the verification of the test coverage and the code quality and fails if the minimum is not met.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.244.228