Continuously Build and Test Your Code With Travis CI
Learn how to achieve continuous integration by continuously building and testing your GitHub code with Travis CI.
Join the DZone community and get the full member experience.Join For Free
Looking for a tool which can continuously build and test your GitHub code with zero cost? Travis CI could be a perfect fit for your purpose.
In this post, I will share how to continuously build and test your code stored on GitHub using Travis CI. Travis CI is free for open-source GitHub projects, so perfect for beginners and constant learners to Integrate and test there software continuously and effectively.
- GitHub account with a public repository: GitHub is open-source, web-based version control. Once you sign up on GitHub, you can create public repositories and push your code to them. If you are not familiar with using GitHub, learn how here.
- .NET Core SDK: We are using .NET Core to create a console application, built continously using Travis CI. If you are not familiar with using .NET Core, check out this link.
Let's get started – check out the source code for this session on GitHub.
Creating a Public Repository on GitHub
Sign into your GitHub account, navigate to Repositories, and create a new repository by clicking the New button.
Now create a new repository with a unique name, provide a description, and click the Create Repository button to create a new repository. Don’t forget to check the checkbox to create a Readme file.
Cloning the GitHub Repository
We will be creating a .NET Core console application to demonstrate continuous integration using Travis CI. Go to GitHub and copy the clone URL. Inside the Repository page, click the "clone or download" button and copy the clone.
To clone the repository on your machine, navigate to the folder path where you want to set up your application. Open the command prompt and type the command
git clone (clone url)
Creating a HelloWorld .NET Core Console Application
To create console application, navigate to the folder created by the clone command. Now open the command prompt and type the command
dotnet new console. This will create a console application in the folder path selected.
Once the application is created, we can build and run the application to confirm everything is fine. In the command prompt, type the command
dotnet build to build the application. After the successful build, run the command
dotnet run to run the application and print Hello World.
Pushing the Code to GitHub
Now that we have created the application, we can push the code to our GitHub repository.
Open the command prompt and type the command
git add *. This will add your changes locally.
Now run the command git commit -m “commit message” to commit the changes locally. Now we can type the command
git push origin master to push all the changes to GitHub.
Now refresh the GitHub project and see that the changes been checked in.
Setting Up Travis CI With GitHub
To sign in with GitHub, open the Travis CI webpage using the URL and click on the "Sign in with GitHub" option to link your GitHub account to Travis.
To sync your GitHub repositories with Travis CI, go to the Travis homepage and click the "Settings" option.
Syncing the Repositories
In the settings page, click the "sync" button. This will display all the GitHub repositories for the GitHub account. Now we can integrate the repositories with Travis CI by selecting it in the "Legacy Services Integration" section.
Continuous Integration of Your Code Using Travis CI
Now that all our setup is completed, we can do the continuous integration of our application using Travis CI.
Create the travis.yml file by navigating to the base folder of your application and creating a .travis.yml file. Include the below commands in the file. The commands specified in the script section of the file will be executed to ensure the CI process.
File Name: .travis.yml
Sample file contents:
language: csharp mono: none dotnet: 2.1.403 script: dotnet build dotnet run
Continuous Integration in Action
Once the file is created, use the above git commands to push the changes to the GitHub project. Now go back to the Travis CI web portal and see the build happening. Once the build is completed, the run command will be executed and the Hello World message will be printed.
The build will run and succeed and after 15-20 seconds (or refresh manually). The build status against the build will be displayed as "Build Passing."
We can also display our build status in the GitHub. You can check my previous post to learn how to do this.
Published at DZone with permission of Automation Curry Puff. See the original article here.
Opinions expressed by DZone contributors are their own.