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
1 B. Create a new branch named as dev
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
cd some_project.git && GIT_WORK_TREE=/home/USER/public_html git checkout -f dev
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
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
- On your local (working) git repository first of all go on to the dev branch before you make any change
1git checkout dev
- 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.
1git commit -m "Some message"
- Now push your changes to the dev server
1git push origin dev
- Observe the changes on the dev server
- Assuming the changes are ok, move on to the master branch
1git checkout master
- Merge the dev branch with master
1git merge dev
- Push the changes to the production/live server
1git push origin master