Vagrant Push is a new feature with the release of Vagrant 1.7 that allows you to deploy any application with a single command -
vagrant push. In simple uses, this helps improve a developer's single workflow when building an application. In more complex uses, it allows developers to collaborate and integrate vagrant directly with their existing continuous integration tools.
In this article, I'll discuss the different uses of the
vagrant push command, provide a quick tutorial on how to use it, and, lastly, a walk-through of how you can use Vagrant Push with our open-source Vagrant setup "Scotch Box".
The General Idea
One of the most important things to understand is that even though the verbiage "push" and "deploy" are loosely interrupted and used to mean the same thing, it is not necessarily the case at all. The actual meaning of a Vagrant "Push" is to simply move your application to the next step in the deployment process (with the ultimate goal of moving your changes into production).
Table of Contents
This is all contigent on your application, the project, the team in hand, and your current workflow setup. So the general idea is that a "vagrant push" actually can do or trigger many different things. Hashicorp, the creators of Vagrant, calls each of these "things" a push strategy. The current types of push strategies are:
- FTP/SFTP strategy: Copying all files somewhere via FTP/SFTP
- Heroku strategy: Deploying your code to Heroku
- Atlas strategy: Pushing your code up to Hashicorp's Atlas
- Local Execution strategy: Invoke an arbitrary shell command or script
Since you have all these options, you can easily change up your Push Strategy as your application grows.
FTP / SFTP
The FTP / SFTP push strategy is dead simple. All it does is copy your files to another location via FTP / SFTP. This can be really useful for super simple deployments, small apps, or even small sites like WordPress.
Heroku is a platform that takes the pain out of hosting. With Heroku, you can easily deploy all types (PHP, Ruby, Node, Python ,etc.) applications on easily scalable infrastructure. Heroku uses git for deployment. Running "vagrant push" with the Heroku strategy will push only your committed changes to Heroku.
I'm in no way an expert on using their platform, but the process as I understand it, for say building a simple LAMP stack based application, would go roughly something like this:
- Use Packer.io to build identical LAMP machine images (aka to be used in each developer's local Vagrant VirtualBox environment and on the remote production server such as Amazon)
- Use Vagrant to develop the application locally and comfortably
- Use Terraform to build out the infrastructure for production (maybe building out nodes connected to a load balancer, a separate database server, etc.)
- Push your code from Vagrant to Atlas, upload your machine configurations from Packer, and move your infrastructure settings from Terraform up to Atlas
- Use Atlas to combine and deploy all the separate pieces together
- Maintain and monitor with Consul
In a sense, the concept is you're not just deploying code to different servers anymore, but, instead, you're also deploying servers and infrastructure all from one place.
Atlas essentially lets you manage all these things on their platform so you can pick and choose your apps and their deployments in one smooth process. Big teams will probably eat all this up, while smaller teams might struggle with the added overhead of using it. I imagine in time that this whole process and concept will become configurable and painless for less server savvy people.
Local Execution Strategy
You can also use Vagrant Push to initiate a custom command or shell script. This can be useful for team's or people who alredy have deployment systems or some sort of continuous integration built out. This way you're able to integrate vagrant seamlessly into your existing workflow with little disruption or changes.
A Quick Demo
For our simple demo, I'll show you a simple workflow using Scotch Box, Digital Ocean, and the FTP / SFTP Vagrant Push strategy.
Clone and Run Scotch Box
The first step is to clone Scotch Box. If you aren't familiar with Scotch Box, you can check out the article on it here. In short, it's a preconfigured Vagrant LAMP Box that's dead simple and hassle free. Just follow these steps:
git clone https://github.com/scotch-io/scotch-box.git my-project cd my-project vagrant up
Now that we have a full lamp stack, we can now see a preview of it at 192.168.33.10. Since we're doing a static application, let's quickly replace index.php in the public folder with a Less and Gulp Starter I made.
cd public rm index.php git clone https://github.com/scotch-io/gulp-and-less-starter-kit .
Setup a Digital Ocean Droplet
Now that we have a local application, let's setup a production server. We're using Digital Ocean, but you can use any server you want really. The only thing to note about the server is to make sure you have FTP / SFTP access with a username and password.
Configure the Vagrant File
Now that we have a web server setup, we'll need to configure the Vagrantfile. In the Scotch Box Vagrantfile, let's add the Vagrant Push config to our settings.
config.push.define "ftp" do |push| # Login Credentials push.host = "18.104.22.168" push.username = "root" push.password = "password" # FTP vs SFTP push.secure = "true" # Where to copy the files on the server push.destination = "/var/www/html" # Where are App is push.dir = "public" end
Now, reload Vagrant with
vagrant reload to save the settings.
Run Vagrant Push
That's all there is to set this up. Now, each time we run
vagrant push it will package and copy the files to the Digital Ocean server we setup.
If you're getting errors, trying SSHing into the box first and setting a new password. You can adjust this in many different ways. For example, you can have a setup for staging and production so you're running
vagrant push staging or
vagrant push production. You can also configure a bunch of custom rules for your setup like excluding or including a specific file. A good example of this could be
vagrant push docs.
Having a solid workflow is infinitely important to successful development. With Vagrant Push, Vagrant makes the development process easy from start to finish.
If you're interested in learning more about Vagrant Push, check out these articles:
If you want to learn more about Vagrant, check out our other articles: