Deploying GitLab on CentOS 7 and Implementing Branch Protection
System Preparation and Installation
Begin by disabling the firewall and SELinux to prevent initial connectivity issues. Install the necessary dependencies for the SSH server and script utilities.
sudo systemctl stop firewalld
sudo setenforce 0
sudo yum install -y curl policycoreutils-python openssh-serverAdd the GitLab repository and install the Enterprise Edition package.
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh | sudo bash
sudo yum install -y gitlab-eeConfigure the external URL in the main configuration file. Replace the IP address with your server's FQDN or IP.
sudo vim /etc/gitlab/gitlab.rb
# Update the following line:
external_url 'http://192.168.1.100'
sudo gitlab-ctl reconfigureVerify that all services are running correctly.
sudo gitlab-ctl statusInitial Configuration and Project Setup
Navigate to the configured URL in a browser to set the root password. Once logged in, create a new Group (e.g., backend-team) and a Project within that group.
To interact with the repository, generate an SSH key pair on your local machine and add the public key to your GitLab user profile.
cat ~/.ssh/id_rsa.pubInitialize a local repository and link it to the remote GitLab project.
mkdir project-demo && cd project-demo
git init
git remote add origin git@192.168.1.100:backend-team/project-demo.gitInitial Commit and Master Branch
Create a file, stage it, and commit the changes to the local repository. Push the content to the remote master branch.
echo "System setup" > config.yaml
git add .
git commit -m "Initialize repository structure"
git push -u origin masterUser Management and Branch Protection
Create a new user account for a developer and add them to the project group with 'Developer' permissions. The developer must also configure their SSH keys in their profile settings.
On the development machine, clone the repository.
git clone git@192.168.1.100:backend-team/project-demo.git
cd project-demoAttempt to push directly to the master branch. By default, GitLab protects the master branch, preventing direct pushes from developers.
echo "Feature logic" > feature.py
git add .
git commit -m "Add new feature logic"
git push -u origin masterThe output will indicate a permission error. Developers must work on separate branches.
Feature Branch Workflow
Create a new branch, commit changes, and push this branch to the remote server.
git checkout -b feature/new-logic
git branch
git push -u origin feature/new-logicGitLab will provide a URL to create a Merge Request, allowing code review before merging into the master branch.