Configure Tools
In this chapter we’ll see how to configure tools for doing backend tasks.
Let’s see how to do it with Vagrant and Jenkins. They’re both independent tools but once we learn how they work will find out they can work together.
Jenkins
Jenkins is an automation server that supports plugins to help in the process of building, deploying and/or automating software projects.
A Jenkins installation can be as complex as we want it to be. Has the main advantage of being free and open source and the disadvantage is that once we install it on a server, we have to take care of everything.
Initial Configuration
Once Jenkins is installed in your system(local, Vagrant, or web server) we need to follow some steps to complete the installation and then proceed to configure it.
In your browser go to http://localhost:8080 and wait for a Jenkins page to appear.
The page will indicate where to find the Jenkins initial admin password.
The file is located in /var/jenkins/secrets, so the command is:
1 $ cat /var/jenkins/secrets/initialAdminPassword
Paste the output in the input and click the Continue button at the end of the form.
Now your Jenkins is ready to be used!
Setup for the Cloud
To access Jenkins instance installed in a EC2 machine, for example, probably you’re going to need to configure a virtual host for the web server your EC2 server uses.
As I’m a fan of Nginx, here’s a config for nginx:
1 upstream app_server {
2 server 127.0.0.1:8080 fail_timeout=0;
3 }
4
5 server {
6 listen 80;
7 listen [::]:80 default ipv6only=on;
8 server_name CI.YOURCOMPANY.COM;
9
10 location / {
11 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
12 proxy_set_header Host $http_host;
13 proxy_redirect off;
14
15 if (!-f $request_filename) {
16 proxy_pass http://app_server;
17 break;
18 }
19 }
20 }
Replace CI.YOURCOMPANY.COM for your server IP address or a valid subdomain.
Checkout other configs in this gist and this other.
Plugins
One of the most popular plugins in Jenkins is the GitHub one. Jenkins alone can’t do much so installing plugins is a most to take the best out of Jenkins.
For projects hosted on GitHub, installed the respective plugin. The docs provide instructions on how to install and configure.
One of the most important benefits of using this plugin is being able to trigger actions in Jenkins whenever a new commit is made to a given branch.
So one could setup a job to do a deployment whenever master branch gets new commits. For this kind of setup you’d be better using a GitHub Personal Token.
Be mindful the token should have the scope
admin:org_hook
For Ruby on Rails
When using Jenkins or any other CI tool to run tests for Rails apps, there’s one thing we need to be aware:
The Jenkins server need everything your Rails app needs to run.
Let’s say your app in development needs the following list of dependencies:
- RVM
- Ruby
- Bundler
- A database: PostgreSQL or MySQL
- NodeJS
Then you’d have to install them all in the Jenkins server. Once they’re available you can run commands for the Rails app in the Jenkins instance:
1 bundle install
2 rails db:migrate
3 rspec spec --fail-fast
These commands would go in a build step. If any of those commands fail, Jenkins will notify you and you should take actions to fix them.
This article shows how to do a setup that run tests and deploys using Capistrano. It’s a bit complex but can serve as an example.
Tips and Tricks
- Restart your Jenkins server from the a web browser with
JENKINSURL/safeRestart - Force restart making running jobs to be lost with
JENKINSURL/restart - In the server you can restart it with
sudo service jenkins restart - Jenkins execution logs normally should live in
/var/log/jenkins
Docs
Vagrant
Vagrant is a tool for creating development environments with ease. Leveraging VirtualBox, you can use Vagrant to create unique and isolated development environments for almost everyting(I’ve tried PHP and RoR development environments).
What’s best about Vagrant is that you can make these environments repeatable and portable, meaning you configure it once, use it many more times.
In a simplified way, what Vagrant does is create a virtual machine(VM) with OS you indicate, sync the VM filesystem with its host filesystem, so that you can work as usual in your OS, i.e, Linux Mint, and run your application inside the virtual machine. This way, you can also keep your machine from being polluted with software related to an specific application.
Provisioning
Vagrant virtual machines can be provisioned with several tools:
- Shell Scripts
- Ansible
- Chef
- Salt
- Puppet
- even Docker(acting as a Docker host)