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

Autodeployment using GitLab Webhooks

Gitlab webhooks

GitLab Webhooks

GitLab provides various Webhooks to perform automated tasks after a certain event. For e.g. Push events Gitlab webhooks will work exactly similar to post-receive hook in GIT. Only thing is it needs a webpage (mostly on your server) which gets executed after a certain commit is made to the GitLab repository. This webpage can perform tasks like updating the local mirror repository and making the corresponding change in the files in your public_html folder. Read more about webhooks in GitLab

Assuming you have a GitLab repository at: git@gitlab.com:username/some_project.git

Steps to follow on your production/live server

  1. SSH to your production/live server
  2. Generate an SSH key for auto deployment to be used by the GitLab server

    Name of the key: gitlab_rsa_deploy, keep the passphrase blank
  3. Add the contents of the gitlab_rsa_deploy.pub key to Deyloy Keys section of your Project Settings and Enable it
  4. Create a config file in .ssh folder and add below contents to the file. Add your user key added to GitLab user key settings.
  5. Create a folder name gitlab preferably outside your public_html folder
  6. Create a mirror repository by cloning your gitlab project
  7. Set Work Directory
  8. Perform git fetch once on the mirror repository so that the SSH keys get added to the server
  9. Create the webhook file on your server as webhook.php. Add below contents to the file and save this file inside your public_html folder.
  10. Add the URL of the webhook.php file to the list of webhooks on GitLab for Push events as shown below
    GitLab Webhook
  11. Now just commit some change either through GitLab or through your local repository and check if autodeployment is working