DEV Community

Roelof Jan Elsinga
Roelof Jan Elsinga

Posted on • Originally published at roelofjanelsinga.com on

2

Automating Laravel deployment using Ansible

Ansible Logo

Automating Laravel deployment using Ansible

If you, like me, have been deploying changes manually to any of your websites consistently for months, if not years, you know that this is a repetitive task. Usually, you pull your changes from your version control system (VCS), run a few tasks to install production dependencies and/or compile them, cache your configuration, and reload some kind of service. It's usually the same few steps with a few optional steps, in case you need to run database migrations for example.

What is Ansible?

You know that if something is repetitive, you can automate it. This is where Ansible comes in. Originally, Ansible is a tool to help with server orchestration and repeat tasks reliably on an infinite amount of servers. The best part of Ansible is that you don't need to install anything on your remote machines. The only requirement is that you need to be able to connect to your remote machine through SSH. If you can do that, you can use Ansible.

You could compare Ansible to a large bash script that runs commands on the remote machine through SSH. The main difference between these two is that Ansible makes everything much easier and has built-in modules for abstracting many tasks. Pulling changes from GitHub, specifying only a repository and a destination folder is one of these modules. It makes writing tasks much quicker and easier.

Why is Ansible useful?

I mentioned that Ansible is originally used for server orchestration. As Ansible is essentially an easy to manage bash file, you can make it do anything you want to. This includes using it for deploying your websites, be it one 1 or 1000 servers. As long as you can use SSH on all of those servers, you can deploy on all of them.

As most of my websites are built using Laravel, I'll provide a simple configuration to deploy your Laravel website to your server, migrate your database, cache your configuration, and clear your views cache. This is very basic, but it's a starting point. This is not a tutorial, because frankly, I've just started out with using Ansible.

The basic configuration

- hosts: roelofjanelsinga.com # You can also specify an IP address
vars_files:
- secrets.yml # Contains the github_user and github_token variable
vars: # Define new variables
github_repo_url: https://{{github_user}}:{{github_token}}@github.com/roelofjan-elsinga/portfolio.git
working_directory: /path/to/app
tasks:
- name: "Pull changes from GitHub"
git:
repo: "{{github_repo_url}}", # This is how we can make this step reusable across projects
dest: "{{working_directory}}"
version: master # Branch to pull
accept_hostkey: yes
register: repo # Store the result of this task in a variable
- name: "Install Composer dependencies"
script: composer install --no-scripts --no-dev
when: repo.changed # Only run this step if we actually pulled new changes from GitHub
- name: "Cache the configuration"
script: "php artisan config:cache"
when: repo.changed # Only run if we pulled changes
- name: "Clear the view cache"
script: "php artisan view:clear"
when: repo.changed # Only run if we pulled changes
- name: "Run the migrations"
script: "php artisan migrate --force"
when: repo.changed # Only run if we pulled changes
view raw playbook.yml hosted with ❤ by GitHub

Then you'll need to create the secrets.yml file, you can use ansible vault:

ansible-vault create secrets.yml
Enter fullscreen mode Exit fullscreen mode

Then fill it with these pieces of information:

github_user: your_username
github_token: your_github_access_token
Enter fullscreen mode Exit fullscreen mode

To edit this file, you can use the following command

ansible-vault edit secrets.yml
Enter fullscreen mode Exit fullscreen mode

This is still something I'm learning, so that's why this is not a full-on tutorial. But by putting this out there, I've already learned a lot of new things about using Ansible.

There will definitely be more content about Ansible, because I'm already loving how easy it is and how many modules are built-in. When I know more about how it works in-depth, I will write a tutorial for it.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay