One-Click Deployment Scripts

One-Click Deployments for Small Projects

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.

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?

A WordPress theme is a simple directory with php, css and javascript files.
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 my-custom-theme.

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 /var/www/wordpress/wp-content/themes.

Step 3: How do we transfer it?

While there are many ways to upload our theme, SSH and FTP are the most popular ones.

Because setting up FTP via script is a little more work, I prefer using SSH.
If you are using Windows 10, you must enable the OpenSSH client first.

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 ssh-keygen and ssh-copy-id.

Step 4: Script it!

With the above information, we can now write our two scripts.
Be sure to replace USERNAME with your own username:

deploy-dev.sh

#!/bin/sh

# Copy the directory 'my-custom-theme' to our target server via ssh.
scp -r my-custom-theme [email protected]:/var/www/wordpress/wp-content/themes

deploy-prod.sh

#!/bin/sh

# Copy the directory 'my-custom-theme' to our target server via ssh.
scp -r my-custom-theme [email protected]:/var/www/wordpress/wp-content/themes

Conclusion

With the above two scripts, you can simply run ./deploy-dev.ps1 or ./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.

And so this part of the Getting Started with DevOps series comes to a close.
If you have any questions or feedback, you can reach me on Twitter or on the BrutalHack Discord Server.

I am live right now! Click to join.
I am live right now! Click to join.