How to setup automatic deployment from Gitlab to ubuntu?
--
CI/CD may not be option for all the scenarios, most of the Webapp will still be happy with simple pull new code from gitlab. Learn how to Setup fully automatic Deployment from your gitlab to AWS/EC2 or any ubuntu server.
Yes, it took me long time to get this in much simpler way and i will try to make it easiest possible for everyone. Let’s get started.
We are not covering docker, kubernetes, complex test and build process and so on.
Assumption
You have following.
- Ubuntu server/AWS Ec2/Lightsail (where you want to deploy your code, say /var/www/html/ for instance)
- Gitlab private repository which you own or have management permission of
- Basic knowledge of Linux/Ubuntu OS.
thats it.
How it will work
- There will be a Runner(Gitlab runner), installed on your ubuntu system
- Runner will get connected to your gitlab repo using configuration
- Your Gitlab will create job to run on your server when new changes detected on your default branch(Say master)
- Your runner installed on system will run required command on server upon invoking from gitlab.
Concept is as simple as this. You can read entire process in detail on gitlab documentation.
Let’s get started.
Install Gitlab runner on your ubuntu server
First you need to add official gitlab repository to your ubuntu server. for which you can run following command.
# For Debian/Ubuntu/Mint
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
This will install repository and re-run your bash.
Now you are ready to install gitlab runner on your system.
To install gitlab runner, run following command
# For Debian/Ubuntu/Mintexport GITLAB_RUNNER_DISABLE_SKEL=true; sudo -E apt-get install gitlab-runner
Configure and register your runner
To configure your runner, first open gitlab and open repository you want to setup deployment for.
- Go to Project’s settings > CI/CD and expand runner section
- You will see two informations, which are GitLab instance URL & token
- Now on your Ubuntu server, run
sudo gitlab-runner register
Add require information
- Enter Gitlab instance URL.
- Enter gitlab token provided in step 2
- Enter description of your runner. You can update this from gitlab later on
- Enter tags for this runner separated by commas(Learn more about tags)
- On next, provide runner executor. for our case it will be shell. (Learn more about runner executor)
- and you are done here.
Add and configure Gitlab-ci.yml
Next step is, telling your system what to do through gitlab-ci.yml file.
Here is file sample below
Understanding yml file above
We’ve specified job, followed by its stage name, followed by scripts in list which needs to be executed upon each job while running.
For our purpose, we will keep it to minimal so for build and test, we have kept it nearly empty, for deploy we have added just.
- Go to $APP_PATH directory
- Run git pull command from branch we specified in $CI_DEPLOY_BRANCH or you can simply hardcode it here.
About these variables, you can add these variables in
Project > Settings > CI/CD > Variable sections.
Click on add Variable and provide value which we have specified in yml file.
Also to make transition more smooth and prevent any permission issues while deployment, you can do following.
- Provide gitlab-runner permission to perform any read/write operation in your project directory.
- configure your gitlab remote url with username/password(e.g. https://username:password@gitlab.com/username/repo-url.git) or save password on your ubuntu server.
Additionally you can add further command as well after git pull in your yml file such as to clean/clear cache or perform any project specific operation.
Lets test
Now as soon as you update file on your master branch, your job will be triggered and you can see the same in CI/CD > pipeline section of your project.
Upon clicking, you can view status of deployment.
You can click on any of the stage and see output of the command and inspect failure reason if not succeed.
This is simplest explanation i could come up with, we can add more and more steps and sky is the limit.
Let me know if you find it useful. Please share, Tweet, tag your dev buddies. I will be more than happy to discuss further on comments. Reach me out on Twitter/telegram with handle @thedijje if you have any further doubts.