Competitive Coding using Travis-CI
We all have used Continuous Integration with our Version Control System but ever wondered we can also host a Competetive Coding Competition on Travis-CI and use it like Hackerearth or Hackerrank!
So How Does it work?
So the Above Flow Gives you a Basic Info of the whole Workflow. But wait this ain’t any informant Blog Post, consider this a step by step guide to deploy your Own Custom Competetive Coding Event on Travis and GitHub!
Step- 1
For Reference, I have already Created a GitHub repository with all the necessary codes and Travis-Config File. Don’t worry at the end of the Blog Post you will be able to Deploy Your own.
- Git Clone this Repository
- You will see .travis.yml file.
- You will see a scripts folder
- These 2 Things are the Heart Here ❤
Prerequisites:
- Create a DropBox App from the Following
https://www.dropbox.com/developers/apps_tk=pilot_lp&_ad=topbar4&_camp=myapps
After you Create your Dropbox App click on Generate Access token and Copy it.
Open The Scripts Folder and open dropbox_uploader.sh file and move to the line number- 1548 and add your access token there.
Open DropBox and Create a Cases folder and upload 6 files
- input.txt, input1.txt, input2.txt
- output.txt, output1.txt, output2.txt
These will be the Test case that you upload on Hackerearth or any other coding platforms (Here I considered 3 test cases you can change that if you want)
I recommend you look at this repository where I got the dropbox_uploader.sh code for reference and Usage.
Once all these setups are done we can write our program of Competetive Coding Coding. Here I have taken a Sample mid-level question — Question can be found in the repository README.md and the solution is in main.c.
Step-2
Lets Head in understanding the Code!!
This is our Travis build file and is quite simple and self-explanatory.
- First I am making our dropbox_uploader.sh file executable using
chmod +x command. - Then I am downloading my Input files with our dropbox uploader
- And compiling our main.c with clang
- And then I am doing this :
./sample_program < input.txt > stored_output.txt
- What it does is it takes input from input.txt and stores output in output.txt It’s that simple 😉
- After that, I am uploading the stored_output.txt to Dropbox
- In the Python section basically, I am downloading output.txt and stored_output.txt files.
- Now before we move forward let’s open our scripts folder and inside it open test_cases_checker.py
- So if you are familiar with python than you will find this quite easy to understand that what I am doing is opening output.txt files and adding it to python list and also adding stored_ouput.txt and adding it to a python list
- Then I am basically checking if each element of both these lists are same or not
- If they are same then I will print
- Test case 0: Passed
Test case 1: Passed
Test case 2: Passed - If any of the Test Cases fail then I will raise an error and hence build will fail on Travis
- And After that in our travis.yml file, I am Basically deleting our stored_output.txt files from Dropbox for non-test case duplicity reasons for future assessments.
- That’s all this is all that is required for our building a Competetive Code Checker System for using it with Travis and GitHub.
Step 3:
- So let’s start using this Project you can either Fork my repository or you can create your own and push all the code to GitHub.
- After that Open Travis CI Website: https://travis-ci.org and sign in with GitHub.
- After that hover on profile Icon and click on settings.
- After that, you will see your GitHub Repository list and find the one you pushed all your code and turn on the switch beside the repository name.
- Click in more option- trigger build and trigger custom build and this is what you will see.
- Now speedily click on cancel job of python because we need to run python file after we compile our main.c program.
- Once The C job of Travis is compiled perfectly then restart the python job and see that if all the test cases are compiled or not?
And done 😎😊that's how you just made your own competitive coding platform and isn't it awesome 🤷♂️ ?
If you loved or liked my small experiment make sure you hit that
👏 Clap Button and also don't forget to follow me on twitter.