When we think of DevOps and deployments, we usually think about big fancy automated deployment pipelines.
Although I love deployment pipelines, others have written great articles and tutorials.
If you are looking to expand our Travis CI build server from Getting Started with Build Servers, its super easy to implement with the Travis CI documentation at https://docs.travis-ci.com/user/deployment/.
Instead I want to show you how I deploy small projects with a one-click deployment script.
Before continuing, make sure you know the PowerShell basics from my Cross-Platform Command Line article.
Table of Contents
Writing a Deployment Script
For this example, we will deploy a WordPress theme. It’s a common use-case and does not require you to know anything about WordPress.
As for all small projects, we will follow four simple steps to help us determine how we will deploy via script.
Step 1: What do we need?
If you want to see an example, have a look at the TwentySeventeen sample theme.
As a wordpress theme only consists of this directory, we do not have any builds to upload. We simply upload our entire theme directory.
For this example, we will call this folder
Step 2: Where do we need it?
Now that we have a wordpress theme, where must we deploy it to?
For this example, we will assume that we have two servers:
dev.justice.sh– a private server for testing new code
justice.sh– a public server hosting your website
WordPress themes are installed by uploading them into the
wp-content/themes directory. If you wish to learn more on the wordpress folder structure, this article gives a great overview.
So let’s say our target path is
Step 3: How do we transfer it?
While there are many ways to upload our theme, SSH and FTP are the most popular ones.
First of all, make sure that you can access your server via ssh.
I recommend using ssh keys instead of username/password. See this article using windows + powershell.
If you are on mac or Linux, you can simply use
Step 4: Script it!
With the above information, we can now write our two scripts.
Be sure to replace USERNAME with your own username:
#!/bin/sh # Copy the directory 'my-custom-theme' to our target server via ssh. scp -r my-custom-theme USERNAME@dev.justice.sh:/var/www/wordpress/wp-content/themes
#!/bin/sh # Copy the directory 'my-custom-theme' to our target server via ssh. scp -r my-custom-theme USERNAME@justice.sh:/var/www/wordpress/wp-content/themes
With the above two scripts, you can simply run
./deploy-prod.ps1 to deploy to DEV or PROD servers.
I like to commit my deployment scripts in git, so I always have them together with my projects.