Continuous Integration for Rails with Travis & GitHub

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

I posted this on the weekend.

Continuous Integration looks to be complicated (and hard to understand the value of). In this video, I cover some of the basics. Depending on the response I might do an in-depth course on this topic!

👍︎︎ 7 👤︎︎ u/philwrites 📅︎︎ Sep 03 2021 🗫︎ replies
Captions
you've written your code in ruby on rails now you want to deploy it to a server but first maybe you should check to see if it actually works that's what continuous integration is about let's talk about it today [Music] hey guys phil here again with another ruby on rails chat today we're heading off kind of into the land of what they call devops which is some kind of new term that i don't really know what they're talking about but essentially you want to get your code out you know you're not writing this just for yourself so there is something called continuous integration and what is continuous integration you might add it is when you check your code into a remote repository some kind of process goes grabs that code checks to see that it compiles we're using that term very loosely here compiles the test run and you're good to go and you do this so for a couple of reasons one is so you don't screw any other developers working on the project with you you know so that when they check out the code it'll you know run on their machine hopefully and also when you're on your way to uh releasing something you have syntaxically correct correct files you know because one of the things about rails and ruby is that it's not compiled right so we're not pre-compiling anything but often in the development environment we don't run syntax checks you know we don't load up every file that normally in the dev environment that doesn't happen but it definitely happens in production and then you can say oh my god you know i didn't do a brace here everything missed it so that's one of the things that continuous integration can test or check and of course the other thing is tests a lot of developers don't run tests all the time on their own machine so when you check things into the remote repository you can have this other process go out and run the tests for you that is let me just check to see that's pretty much what continuous integration is continuous integration going to come the term comes from back when we had the extreme programming which was this concept in late 90s i think that maybe evolved into agile but basically you just want to have you want to cut down that time at the end now back when i was starting out and yes it's another boring story from the old man is you know i remember uh one of the first serious programming jobs i had in the early 90s um i was working in richmond virginia and we were writing macintosh software and to compile the app so that you could send it off to the testing team and all the rest of that stuff to compile the app sometimes could take two hours and that was back in the heady days of software development so you know in our office we had things like pinball machines and popcorn makers and video games and stuff like that you know because we needed that because you sat around for two hours waiting for your thing to compile continuous integration is kind of an answer to that it is don't worry about doing that so much on your local machine uh you know write it as you think best of course run bits as you can on your machine then check it into the repository and let the central service do it i know a lot of people are going to watch this video and say but that's not how i do continuous integration i don't use this not there's lots of ways to do continuous integration i am going to be doing it in a very simple way to give you the concepts and to give you an example of it actually working and that is i'm going to be using something called travis some people like it some people don't i agree the name is a bit strange but we're going to get travis to do the work for us and so let's jump over and take a look at what we need to do hopefully i can do this video one time there might be some edits uh we'll see how i'm feeling about going back and re-recording it and to cover the inevitable mistakes that i always make all right so let's take a look i've got i'm using the contractometer which is the project that i wrote for the last hot wire um stimulus demo and right now uh what i'm going to do is i'm in the contractometer i'm going to create a branch and that is let's call it for ci for continuous integration and the first thing we really need to do is to get some stuff into our gem file so let's go and take a look this is as you remember from the last time the bog standard uh straight out of the box rails gem file i've added in some stuff at the bottom this is you know the marginalia the memory profiler stimulus rails and that kind of stuff stuff that we talked about last time you can go and take a look at the previous video if you want to see what it is i'm not talking about developing the app here i'm talking about doing the continuous integration now some people like to go through and remove all the comments in this file i don't quite see the point in doing that so i'm not going to one thing that we can do because this branch is let's say specifically for continuous integration i'm going to get rid of this this stuff because it's only for running on windows i'm not running on windows the continuous integration is not running on windows and eventually when i release something to the web that's not going to be on a windows server either so let's get rid of that and let's go down to the bottom and well no we don't need to go down to the bottom at all we can get rid of uh the development block if we wanted i'm not going to do that but you can get rid of it we're going to use a leave by bug in there let's add some of the things that we need so we're going to be doing you know at the heart of doing this is testing so we're going to use cucumber for that not that i'm going to be writing really any sophisticated tests in this but let's pretend that we're doing the real thing so let's uh cucumber rails and then there's something called coveralls which is a code coverage tool and they'll give you a web interface that will integrate with travis and show you your code coverage in all fairness i had some issues getting that working but we're going to leave it in and pretend that it works for the other thing we want to have is database cleaner which is just very nice if you're doing tests and then we want the stuff for that we use for our spec which is factory bot rails and of course [Music] our spec come on r spec rails and then was it shoulda matchers so that's all the stuff just for doing the tests and so on like that um the cucumber adds in the cucumber sport coveralls i said database cleaner will clean their database every time before you do a testing which is very nice factory bot is to give you some factory gem uh r spec rails of course and shoulda and so let's when we do this um what we need to do is add some stuff into application rb really in fairness a lot of this is not strictly to do with setting up the continuous integration is to set up the proper test environment but it's kind of all part and parcel as we say this don't generate system test files we're going to get rid of that we don't need it and we're going to write a config generator to generate generate and what do we want so generate helper false i don't know why i'm i'm just going to paste these in you know what so there's a whole bunch of stuff you want to turn off this is again just to make the test more clear uh i don't know this thing's going to work it's not going to work so let's do that and now okay we've set all that stuff up so let's run bundler so bundle it's going to go off and you'll see it's got r spec rails is in there the shutter matches or matchers are in there database cleaner cucumber all that stuff's been added in and now we can actually run the installers so remember when we generated that i you weren't here at the time i don't think generated this project i didn't actually um create a test and you know i said uh skip tests when i did the rails new so we have no test stuff in here at all so one thing we're gonna need to do is bundle exact rails g and let's let's install all the stuff for our spec and it won't do that now because i made a typo somewhere and that is generators there we go let's do that again so it's going to generate a dot r spec file uh i can't see because my camera's in the way r spec which is just require spec helper it will have created a spec directory with some rails helper and spec helper we're going to edit those in a little bit and while we're here why don't we generate the cucumber install as well off we go against some stuff for the cucumber uh tess i'm probably going to do another video excuse me where i uh talk about writing tests and using cucumber maybe capybara so i know we say caviar and all the rest of that stuff so this is just very cursory we're just kind of getting everything ready that we need to do it and so the other thing we added of course was the um database cleaner so we want to go to our spec no rails helper now in rails helper there will be an r spec blocks there it is r spec configure that will have been added by the well this whole file was added by the generator and what we need to do is inside of this block you can do it anywhere so i'll just do it at the bottom again a lot of people delete the comments i don't comments are helpful so we're going to add in a couple of blocks here which is a before and around and so before each test suite we're going to set up some stuff for database cleaner and we're just going to truncate the tables get rid of all that data and then around each thing we're just going to do a clean and then run the example example in this case is the test and outside of the r spec block uh don't worry i'm going to have a readme on the um the old github there for this project we're going to just set up the uh shoulda matchers here can i make the cursor big no uh should have matchers and then we're good to go so all of our stuff has been configured um i think there's a coverall maybe i'll add the coverall stuff in just for valve you know i did another version of this video when i went back and i watched it and i noticed that i called coverall covergirl i don't know i really don't know about myself and so we need to add some lines at the top of nvarby and spec helper to set the coverall connection and also seems like a lot of files but now what are you going to do we're going to use simple car cover so we kind of add a simple curve and we're just going to say start the symbol cup rails simple and i'm not going to do anything sophisticated with that so let's go over and do and look at something we're going to look at oh i've got it over here travis so travis is this thing that will um connect with your github account now i'm going to put i'll put a thing up so what you need to do is go to travis.ci.com again link's in the description and it'll be in the readme as well so you go to travis you sign yourself up i used my github account to sign up for travis just so maybe i didn't have to do one step i will put the screenshot of what it looks like when you first do that on there and you know when you authorize travis to have access and then you can pull over all of your repos or you know and turn on some of them and turn off others so i've done that and i've got this stimulus turbo thing is already in there because of course i did a version of this before and tested it to make sure that my demo worked for you so what we're going to do so you can kind of ignore what we see here right now branches uh we don't really have any branches again sorry you're gonna have to ignore what i see there but what we do need to do in our project to get travis working it's not enough that you tell travis that the repo is there and all the rest of it you need to create a dot travis dot yml worst place to put the camera right in front of the edge of my thing there um and so we need to create this yaml file and what travis will do is it'll look for branches that have the travis yaml so right now i don't have a branch that has that so i can't rebuild that's because this branch i have the old branch i made yesterday when i tested is gone so i can't build it all because there's no travis so what we need to do is a a file here a yaml file we're going to say that our language is ruby travis uses rvm and we want to set our version now in our gem file at the top it will say what version of ruby we're using on your project it may be different you can put whatever version it is you're using just make sure it's the right one we're going to do a before script and that means before before firing up travis what should we do and we should do a bundle exec rake db create all so let's make all of our databases and then we'll do a bundle exec rake db migrate doesn't take einstein to figure out that and we'll do a script and what we're going to do bundle exec break ci tests now it's completely possible that i did not do the ci tests part so we need to create a rake file this is a thing we're talking about here rake ci tests so what we can do in our lib tasks we want to create a new file here and we're going to call that ci dot rake and what do we do unless we're in production we are going to require our spec core break task and we're going to require a cucumber break task i know it would be a million times better if i copied and pasted this but today i feel like typing require uh we're going to do the cover all covergirls rake task just pull all that stuff in and we'll fire off coveralls and [Music] then we're gonna this is a rake file so we're gonna create a namespace of the other way ci do and what we want to say is let's uh run all our tests and generate coverage report so task tests spec cucumber so it'll go through and run the tests and then do a coveralls push and send that data off to coveralls there we go so we've created that ci file which we have now told um that thing travis to do so let's what else do we need in there oh yeah of course we need to tell travis what services not secretabis services we use and this is pretty simple app and i just use postgresul if i'm not mistaken yes postgresql is my database here so we're gonna do that and then do you want to send me a squillian notifications about the process of this and the truth is no i don't i want those you may want your notifications but it will spam the bejesus out of you so there we go our travis yemel is set up and the coveralls is set up so let's push this so what do we got a git status let's see what we've changed let's make that a little clearer so we modified the gem file which of course will modify the lock file you know we added some stuff into application let's take a quick look and see what we added into application we added the config generators stuff into application i can just do all this here i don't need to um we created the cucumber yaml we changed development um this oh that's uh yeah that's for um cucumber it adds in a couple of annotations things there we change test which i imagine do the exact same thing yes we're gonna add in that stuff there and then we created a whole bunch of new files we created the rspec file the simple coverage file the travis yaml cucumber ammo got created by the uh but the install the generator sorry created a coverage and create some features we create that rake file just there at the end and we created there was a cucumber rake file that was created by the generator and the script and a i don't know what the script is and a spec so there we go we've added all that now there's two things we can do we can do an add minus p i don't know if you know about this if you're using git admins p is nice it'll go through and then show you each file and say to you approve these or not it's very handy if there's one change in a file you don't want to commit you don't have to add all of the changes in a file and to get you know you can you can say okay leave out that change if it comes up you can hear you can say no and you know if you change something that's only specific for you but in this case i'm going to quit that because git add i know i want to keep everything here so i'm just going to do that if i look at git status again everything's waiting so i can say git commit add all the things for travis coveralls and ci and it's called 4ci so we can say good push origin for ci now in this particular case it is on github i don't use github personally for my professional projects like zone master and so on um i use either bitbucket or something else like that but if you're going to do continuous integration the easiest way for sure is if you have a github your code is on github so this project as you well know is on github so we're going to push that up to github i will have to type in my password two times and there we go we've created a new branch called 4ci inside of that stimulus hotwire turbo bootstrap 5 demo project so let's go over and take a look and here we are let's just refresh and there we go so i didn't probably have to refresh but anyway so the branch it's got this cool little spinning icon let's see if i can zoom in oh look at that it's got a little spinning icon the branch of 4ci and there's my commit uh comment can't say that a lot oh look i typed overalls wrong anyway add all the things for travis coveralls and ci is it saying anything interesting over here it started this is build number seven for this uh it tells you what commit it is if you click on this it'll take you over to the commit it'll do the diff it'll tell you the branch tells you who it is and then tells you what kind of uh environment it is that you're running this on and then if you go down travis will show you all the stuff as it builds stuff you know so the worker information is building postgresql what version and postgres is doing which is 10 just that's old isn't it anyway um all the stuff that it needs it does uh rvm install so it's kind of firing up a brand new machine off in the cloud somewhere and it will run your code on that machine now the cool thing about this is that pretty much essentially it's free you know there are versions of travis that are paid there are other continuous integration things that are paid and maybe if you're in a you know you're developing a pro thing or whatever um you know then you can do the paid version but the free version is totally fine for those of us working on smaller projects um and it's going to go off it's going to do all of its stuff and you know it's you don't have to worry about you know it's whatever version i'm using uh rails 6.14 so version is not a problem you don't have any problems here you know it just goes off and it's going to build this thing and it'll show you how long it takes i can't remember how long the building took before so i might speed this up or you might watch me drinking a lot more coffee but um you know some let's just talk for a second about continuous integration a lot of people think that it's well a lot of people think a lot of things are a magic bullet they think continuous integration is some kind of magic bullet and they can people kind of who are uh the flag where flag bearers of continuous integration also do say these kinds of things and oh it's going to reduce your bugs it's going to do it's not going to do any of that and it's going to make things more stable and it's going to make things a little bit more easier when you go to deploy and but it's a big help in running your tests like i say it takes this responsibility from the individual developer and pushes it up to a service it is automation right that's the point and automation is good and oh my god it errored so we can look maybe and see there see i typed i made a typo perfect example here you know it looked fine on my machine i don't even know how i would test this on my machine because it's inside of a rake file that i normally wouldn't run as a developer so i've made a typo there we go that's the benefit of continuous integration i know that this branch is broken so let's go down here and where is it it's in ci rake on line six here we are right there uninitialized constant coverall did you mean coveralls why yes i did you know sometimes people uh take me to task of this but i make too many mistakes and the point is to demonstrate being a normal human being normal human beings make mistakes okay so that failed so let's go let's clean that up and let's do a git add minus p and we can see that in our ci rate file i've changed that to coveralls right task and let's just check one more time line six line six we're good to go stage is hunk stage it get commit fix my inevitable typo and then we can push this up again and it's going to push it and then if we wait a minute i would imagine we will see this start to build again so while i was talking about how this doesn't really help you it showed you how it helps you okay so i i think it's a a great thing to start from the beginning i the next thing of course is um to write some tests and we can write tests in r spec and those tests will be run automatically as well i'm not going to talk about i'm not going to show you writing some tests here this project is so basic i haven't really thought of any good test see now this time says postgres 9.6 so it's going off obviously and firing up different things interesting this output seems to be a little different every time that's fine anyway there you go continuous integration with rails using travis using coveralls and um our spec and getting our spec into your project wow it covered a lot and it's really not been that long only about 30 minutes again the camera's in the worst possible place i hope you like these videos i don't cover every possible permutation of doing everything when i do a video like about this one with tess or the other one about debugging or whatever i'm covering the basics that i use and i think are important to know as a beginner or as a competent professional developer we're not here to do the nuances of the language we're here to write products that make money anyway that's all for now i hope you enjoy this it's going to go off it's going to build but i'm not going to waste your time i'll put a flashy screen at the end that shows that it built correctly all for now like subscribe share it if you want please and we'll see you next time bye for now ah [Music] [Music] [Music] and we're back and so it ran for three minutes and 18 seconds and it's passed as you can see i will zoom that up this number eight passed um i played a little guitar while i waited and got you can see the build history you can see that this one passed that one failed it goes um build history you pull requests i don't have any pull requests on this one and there we go see you around
Info
Channel: Phil Smy
Views: 434
Rating: undefined out of 5
Keywords: ruby on rails, learn ruby on rails, software as a service, saas business, how to create the perfect saas business, tech entrepreneurs, entrepreneur, become an entrepreneur, become an entrepreneur motivational video, be an entrepreneur, continuous integration, travis ci, travis ci with github
Id: YqzUaKbH1UY
Channel Id: undefined
Length: 31min 30sec (1890 seconds)
Published: Sun Aug 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.