Create staging environment using GitLab

Gitlab Webhooks

Prequisite: Please go through the steps to set up GitLab on the Production/live server before going through this tutorial.

Steps to create a staging environment using GitLab branch

1. Create a new branch for your GitLab project as shown below

1 A. Go to your project page in GitLab and click the option to add New branch

Create a Gitlab branch
Add new Gitlab branch

1 B. Create a new branch named as dev

Gitlab new branch

2. Create your staging site by copying the files and the necessary databases

3. Follow all the steps mentioned on the article Autodeployment using GitLab Webhooks on your staging sever with only the exception of Step 7 which needs to be modified slightly on the staging server. For the staging server that command would change to

Please note we are checking out only the dev branch on the staging server. So only your changes on the dev branch will be seen on this server.

4. Add the staging webhook.php file also to the list of webhooks on GitLab for Push events as shown below

GitLab Webhook

This completes setting up your staging server to preview your changes on the dev branch before they get merged to the master branch.

Now the question is how do we commit our changes?

Steps to commit your changes

    1. On your local (working) git repository first of all go on to the dev branch before you make any change
    2. Now make changes to the required files and commit your changes. Since you are on dev branch the changes will automatically get committed to the dev branch.
    3. Now push your changes to the dev server
    4. Observe the changes on the dev server
    5. Assuming the changes are ok, move on to the master branch
    6. Merge the dev branch with master
    7. Push the changes to the production/live server

Create a WordPress staging site through shell script

wordpress staging
Photo credit – 132889348@N0722868800432

Creating a wordpress staging environment requires 4 things. Here we are assuming that the staging environment is on the same server.

Requirements for creating a wordpress staging environement

  1. Clone Database – Each Time
  2. Copy the code – Only the wp-content folder
  3. Edit wp-config to point to the Staging Database (single site only) plus the Staging Domain (multisite) – Once Only
  4. Update the wp-options table with the Staging Domain (single site), Update the wp_blogs, wp_site, wp_options, wp_1_options, etc tables with the Staging Domain (multisite) – Each Time

So how to to achive creating a wordpress staging environement with a single script? Here are the steps.

Step 1: Create staging.sh and define variables

Create a file named staging.sh inside a directory named staging (preferably outside your public_html folder)

Define all the variables related to your production and staging database connection as shown below

Step 2: MySQL Dump of the Production database

Add below code to staging.sh file to get MySQL Dump of the current production database

Step 3: Export the Database to the Staging database

Again add below code to staging.sh. Here we are connecting to staging database and importing the sql file created in Step 2

Step 4: Batch File

Now create a new file within the staging directory named as batch.txt. Inside this batch file we will write all the queries we want to run to rename the production instances within the staging database to refer to the staging environment. The batch file will hugely depend on the type of site setup you have i.e. single site or multisite

If you have a single site then its very easy. Add below query to the file. Remember to replace PROD_DOMAIN and STAG_DOMAIN with the respective names. e.g. www.myproddomain.com

For mulsite it is bit complicated

Now just repeat the last time changing the number in wp_1_options to the number of the subsites

Step 5: Run the Batch file queries

To run the batch file queries add below code to staging.sh file

Step 6: Copy WordPress files

This can be done by a simple cp (copy command).

Step 7: Edit wp-config on the Staging

Now the last thing is to edit the wp-config file to point to the staging database. Thats all.

Finally just running the staging.sh script through SSH will set up the staging server.

Step 8: Weekly Cron Job

To refresh/update the staging server every week add cp command to staging.sh file and this time just copy the wp-content folder. You could also copy differences i.e. last changes only using rsync command.

Then set up a cron job to run staging.sh to run every week