Gitlab CI deploy to VPS through SSH

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm gonna show you how you can publish your application from good lumber repository directly to your to our VPS server only way to get lab CI which is a really great tool and what is the best about it with some limitation you can use it for free and like for my personal project I never I never hit this limitation and I'm using it all the time so you need only like empty grid lab repository or your application repository and VPS server at first we need to setup VPS and with only two things at first we need to have to have installed docker on this server and you can do it with my older older script it's really faster how to install docker but you can choose a course or something else everything you need is going to this link it will you can find it in in description under this video and after you connect to your machine you can apply only this command so at first I I'm gonna I'm gonna plug into my empty new server oh wait I already was there before is in its life insulation okay I mean as I said it's empty server so we need to install our docker describe install even supervisor and and docker but we want on the docker about two we can use it all so only pass this this command and wait after our installation is complete we can we can check it our doctor doctor is working with simple command docker - we yes the conversion it's working correctly and now we need to generate SSH key to access our Arbor to our server without user credentials you can find really you can find really great tutorial under this link it's under on on page digital ocean ocean and only with few commands first is this SSH again we generate our key and you can see right now that it's here under - SSH you there is new - new files without without key after that you need to put our public key - authorized keys and you can simply do it with this command can catch public up to power public key and put a 2-under SSH authorized keys okay now we can we can we can copy our private key here and save it to someplace I'm gonna use our empty repository but don't forget to delete it before before commit so save it at first we need to change model of this key to 400 with we we can test it wait SSH SSH command with path to our of our key and with root access to our server come on sometimes these editors consoles have some problems it works ok we are in you can see under VPS this is so what we need to now okay so our server out server is ready so we can go to next step and in its contract containerized our application because the processes that we gonna publish container of this application image of this application to our server through this gate lock pipeline so for this we at first need some application to publish so I'm gonna choose node.js application because there is some step I wanna show it's building process of this image that you you you can in the pipeline build your application before publish it so this is really great feature so we initialized our know we didn't mean it's our new package and we installed their Express server to publish our hello world application in the easy and our application gonna gonna run on our application when I use port 80 and let's test it oh hello world it's working here now we do our packages and we our start script XJS nice and right now we are ready to create our broker file and container containerized our application I'm gonna use the docker file from some of my other project budget so because I'm lazy to write it and okay but here it is we use the image with parental ID node choose working directory copy or content install our depth dependencies and and remove the Deb dependencies but it's in our case there's none and we want to expose our container we're not gonna expose port 80 and of course run our application let's save it for poor testing if everything is all right we create docker compose and because I this is for me it's faster right I'll test it so okay that's ready compose okay server is running let's check it hello world and it tastes really running so everything is okay we can we can delete this test test files and next step is contacts setup github repository okay well for this minute of course go to our developer repository here under under settings you can find and do it yeah you here in settings you can go to under repository link and here is deploy tokens we need to create special group you can learn about this group under under this link here and it's special group which will create you in your pipeline these two variables with access to your container container registry it's like dr. Harper but on gridlock and for this unit only create it's really simple you mean let's create great token deploy token with the name good luck deploy token no Express and read the registry right here we create it and as we go you don't need to save it okay now we need to go to setting and to undersea ICD and we need to set some variables these variables is when you can choose names for name for it what you want but but in our github CI Yama it's gonna be like this master host it's a IP address or you know host to your server it's called master because it's like master and develop server this is our master so as you set it up another is our SSH user and it's root and last is our certificate certificate now certificate SSH K this private key and we we put it we put it here and call it master SSH key but here you need to choose save it as a file what was the difference between variable is that variable is like directly the value of the this like variable is under this and Roman variable but if you choose it as a file in pipeline under this Roman Bible it's gonna be us us file path to file which contains all all this very good and created you can again you can protect it in protected branches by bitter rules for all your developers here I don't wanna complicate it and okay we are ready to go to next step next step is create github CI file let's fund the game here I'm gonna delete this key because we don't we don't need it anymore and let's create our github CI I already have some template for it well stamp right it's ready to ready to work but let's let me describe you how it works at first you need to create stages of this Reedus process it's like build for building this this container and deploy to deploy our container to our server before script before script we said some docker docker variables it's only for identify like how next name our name our new container and this is like nicer nice name in first stage built built we wanna we wanna build our docker containers so we at first need to choose some image which support running doctor in docker and we need to tell get lab that we gonna run docker in docker so this is - this is these lines and after that when when it's triggered manual on on click and first finger then we need to do is to log into our container registry and in our pipeline docker get lab set us set for us these variables with credentials already so we just we only use it we load some previous images if there's any it's not here or true makes that script on file with us nothing exists and we are ready to build our built our docker image this is this you can do it with this command and after that after everything is okay we gonna push it push it to our container to container register and we wanna we wanna test it so that there's nothing easiest maybe we can create some because we don't wanna push all our dependencies and two node modules years and I think it's alright now yes ah okay we undid it technology yes and we push it yes now we check it here on in our project and yes there is everything what it should be and we can go here to our pipelines and we see that there is ready our blood scrip so let's test it okay job succeeds so we can check here in packages container registry if our container was published correctly yes we can see that there is two two containers with same content and one of it is like for this specific commit and second is with the branch name it's a really good practice because sometimes you wanna you're gonna publish like latest branch branch build but sometimes you want like a specific comet comet build and next next step is to deploy this this application for this we use all already prepared prepared coat of mine and it's a deploy stage for image we only need some docker image with SSH client I choose to use this one I have good experience with it and everything works normally as I told you under this environment good lab environment variable what we said is path to our path to our file part of file of a bit our certificate key and we need to set right mod mod on it right rights for it and now fun begins we have here SSH command to connect to our machine with provided credentials you can see like that at first we want to skip we want to skip this checking of hosts because we can ants in pipeline we can answer to it so we can do it with with this and after that we provide path to our to our key and SSH user with IP address of our host host we need to in arab our machine we need to log in log to our container register in our like gitlab - and again again good lab provides us this registry this registry path and these credentials for deploying this for the credentials to this registry for deploying this application only with read read rights and after we login we AG again connect to this SSH it's separated here because I couldn't find the right way how to combine it to one one come one command and because there is lots of like putting anywhere environments of those brisket and I didn't figure it out and this work fine it's it's only easy tutorial you can modify by yourself everyone and if you find the way of course write it to me to comments or as a comma a comment in gist ok second we after we set up this this docker registry we want to remove remove remove old images what is running there even if even if they are running we want to delete them why is that because you don't want to waste space of on your virtual machine and it's better to delete it before we start starting a new well container yes we we delete our container before before we start a new one so there is there is some time when our application is not working but again you can you can edit it and and to like do it without get out of time and the last what we need to do is run our new container and the set it here we want restart it always expose all our parts in the docker file and this is it so our our deploy deploy script is ready - so we can test it push ok let's check it in our pipelines yeah more invalid ok push and it's working now again we first need to build our application because we need to build this specific comment and and after it's done we can run our deploy script and check if it's working ok it seems that our pipeline is succeed and we can come on it's ok and we can check it on server and so hours ago excite 19 minutes oh I'm not connected to my server and we can see that our container is running 42 seconds and you can check it on a browser and I again lost my IP address come on yeah I hope okay hello ward this is our application running on our VPS and everything seems to be fine so this is how to make automatic pipelines thank you for watching and if you have any question just just write it - to comment and I'm gonna try to answer - okay thank you again and bye
Info
Channel: Tony B
Views: 7,145
Rating: undefined out of 5
Keywords: Gitlab, CI, CD, SSH, VPS
Id: pChVWAVY7RU
Channel Id: undefined
Length: 26min 14sec (1574 seconds)
Published: Fri May 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.