How to Set Up Multi-Branch CI/CD with Docker, GitLab, and Jenkins for PHP Projects
This guide explains how to avoid code‑conflict and testing interference in large PHP projects by creating independent deployment branches using Docker, GitLab, and Jenkins, detailing the required tools, installation steps, container configuration, GitLab project setup, Jenkins job creation, and webhook integration.
Background
When a project grows and multiple developers work on different features, using a single branch for development, testing, and deployment leads to code conflicts, accidental breakages, and interference between concurrent test runs. A multi‑branch deployment strategy isolates each developer’s code in its own test environment.
Required Tools
Linux (CentOS 8 as used in the example)
GitLab for source‑code management
Jenkins for continuous integration and deployment
Docker for containerizing GitLab, Jenkins, and the web application
Nginx as the web server/reverse proxy
PHP runtime
Git command‑line client
All tools are installed via yum on a system that already has the yum package manager.
Step‑by‑Step Setup
1. Install Docker
yum install docker2. Deploy GitLab
docker pull gitlab-ce docker run --name gitlab -p 443:443 -p 80:80 -p 22:22 \
-v /data/www/gitlab/config:/etc/gitlab \
-v /data/www/gitlab/logs:/var/log/gitlab \
-v /data/www/gitlab/data:/var/opt/gitlab \
gitlab/gitlab-ce /bin/bashAfter the container starts, access http://localhost to verify GitLab.
3. Build a Custom Jenkins Image
The official Jenkins image lacks Composer, PHP, and Git, which are needed for the PHP project. The process is:
Pull a CentOS base image: docker pull centos:latest Create a container from the base image:
docker run -idt --name jenkins docker.io/centos /bin/bashEnter the container: docker exec -it jenkins /bin/bash Install Java, PHP, Composer, and Git via yum and the Composer installer script.
Install the official Jenkins package:
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
yum install jenkinsConfigure Jenkins by editing /etc/sysconfig/jenkins (set JENKINS_HOME, JENKINS_USER, JENKINS_PORT), then start it: service jenkins start Make the container start automatically by adding a startup script ( jenkins.sh) to /etc/rc.d/rc.local and committing the container as a new image:
chmod +x /usr/local/jenkins/jenkins.sh
echo "/usr/local/jenkins/jenkins.sh" >> /etc/rc.d/rc.local
docker commit -m 'Jenkins with docker/composer/git/php' jenkins jenkins:latestRun the new Jenkins container with port mappings and Docker socket volume:
docker run -idt --name jenkins -p 8080:8080 -p 50000:50000 \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /data/www/jenkins/jenkins_home:/var/lib/jenkins:rw \
jenkins /bin/bash4. Create a GitLab Project
Generate an access token in GitLab (used later by Jenkins).
Create a new repository.
Configure a Webhook that triggers on push events.
5. Configure Jenkins Jobs
Install the following plugins to enable GitLab integration:
GitLab Plugin
GitLab Hook Plugin
GitLab API Plugin
ruby-runtime
Set up the GitLab connection in Jenkins using the access token, then create a pipeline job that pulls the code from the GitLab repository, builds the PHP project (e.g., runs composer install), and deploys it to a dedicated Docker container or directory.
Result
After completing the steps above, each developer can push code to their own branch in GitLab, Jenkins will automatically build and deploy that branch to an isolated test environment, eliminating interference between concurrent feature tests.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
