TravisCI: Integration and Configuration
Integrating Travis CI to GIT project
There are many CI tools that integrates with Git services. But here is how we can integrate travis with GitHub.
1. Navigate to Travis-ci.com and Sign up with GitHub.
2. Authorize Travis CI with GitHub and then You’ll be redirected to GitHub.
3. Then you will be able to see all the organizations and its repositories on the Travis dashboard
Now Below are the steps to get started with the TravisCI:
Step 1:- There is a search bar on the left side of the travis dashboard. Where you can search for the required builds using repository name
Step 2:- Clicking the + sign below the search bar. After clicking it you will be able to see the list of your existing repositories if you have already connected your GitHub account.
Step 3:- Once the list of your project appears then search for the required project and click the enable switch in front of that repository name. Then travis will be able to trigger the build automatically after making some changes in repository.
Step 4:- After it is enabled, then go to the settings by clicking the gear icon beside the check button. Now you should see different settings. So you can check all the options and change based on the project build requirements.
This is to setup the project in TravisCI now let’s try to execute some of the builds through the travis configuration file.
Travis configuration file
As almost every CI tools has their own configuration file present in their particular repository in the same way Travis CI has a .travis.yml file in the project root.
Sample configuration file in Travis CI:
language: python
python:
- "3.9"
script:
- echo 'CV lint scanning on cp-serviceability'
- echo 'Installing required binaries'
- echo 'CV lint scanning starts'
- chmod 777 ./tests/cv_lint.sh
- source ./tests/cv_lint.sh cp-serviceability
- cv_lint cp-serviceability
|
Let’s go over each block:
1. language — Build is using python as a language
2. python — version specified is 3.9
3. script — These are the set of some commands to execute. In above example, we print some informative comments, installing required binaries and executing the scan by sourcing cv_lint.sh script and calling cv_lint function.
The CI process
The CI of the CICD process mostly focused on the integration part where it is more responsible for scanning and testing the code base and creating the build with no errors or vulnerabilities.
So as we have already gone through the travis configuration file in above section so we’ll be using the same file for our CI process
Travis will trigger the build immediately after you push any changes to the repository. And the triggered build will be visible on travis dashboard as shown in below image
You will also be able see the triggered build on the GitHub as well on the same changes file page. And after clicking in the details button it will redirect you to the same travis build page where you can check the logs.
Also you will be able to see the logs on the same page below the summary section as below
In the case of a pull request, the moment you raise a pull request from your branch to develop or master branch then this execution gets started and it sets as a mandatory check in GitHub. As long as the execution is in-progress you will not be able to merge the PR.
Here the build is in-progress
If the build is failed then you will not be able to merge these changes.
And once the build returns the success status from travis to GitHub then the merge button will be enabled and those changes are considered to be the valid and can be merged with the master branch as shown in the image below
In the above scenario we have only added single stage of scanning the code base but you can add many more things in it as per the requirement.
The CD process
Till now we have gone through the project creation and CI process to scan the code whether the updated code is safer to merge with the main branch.
Now in order to deploy those changes to the server we need to create its build and then deploy the same manually. We need to login to the servers VM and then follow the deployment steps. Which is very time consuming and huge amount of efforts also needed for this process.
We have to integrate CD process in order to automate this deployment.
For this we have to make some change in the same travis configuration file used in previous article in order to deploy this code to the demo server:
language: python
python:
- "3.9"
install:
- pip install -r requirements.txt
script:
- echo 'CV lint scanning on cp-serviceability'
- echo 'Installing required binaries'
- echo 'CV lint scanning starts'
- chmod 777 ./tests/cv_lint.sh
- source ./tests/cv_lint.sh cp-serviceability
- cv_lint cp-serviceability
after_success:
- python manage.py runserver 5000
|
Notice that there are two more stages added to the script:
1. Install: which is used to install required packages before the actual script execution
2. After_success: this is executed only after the script block is executed properly and returns the success status.
Summary
In this article, we learned about CI process integration with Travis CI and GitHub. Also we have learnt that how can we execute the travis build automatically whenever any developer pushes their code to their particular branch or raise a PR with any production branch.
Also we have covered success and failure build cases and how they can be used as a pre-checks for merging.
In CD process we have seen that how to install the packages before execution and how to deploy the code to the server after successful execution of script block.
This is the second blog in the series of the blogs that will be published for TravisCI. More blogs will be published over next few months. If you have any further queries on TravisCI, do feel free to email ashish.sonawane@ibm.com
Author Details: Ashish Sonawane (ashish.sonawane@ibm.com)
Reviewer Details: Ashish Kothekar (ashish.kothekar@in.ibm.com)
Boudhayan Chakrabarty (bochakra@in.ibm.com)