在Debian上定制GitLab功能的常見方法
在定制GitLab前,需先完成基礎安裝與配置。首先更新系統包并安裝依賴項:
sudo apt update
sudo apt install curl openssh-server ca-certificates postfix
添加GitLab官方倉庫并安裝GitLab Community Edition(CE):
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
sudo apt install gitlab-ce
編輯配置文件/etc/gitlab/gitlab.rb,設置外部訪問域名(替換為實際域名/IP):
external_url 'http://your-gitlab-domain.com'
gitlab_rails['gitlab_ssh_host'] 'your-gitlab-domain.com'
gitlab_rails['gitlab_shell_ssh_port'] 22
應用配置并啟動GitLab:
sudo gitlab-ctl reconfigure
sudo gitlab-ctl start
通過/etc/gitlab/gitlab.rb調整GitLab行為,常見修改包括:
nginx['listen_port'] = 8080
unicorn['port'] = 8080
letsencrypt['enable'] = true
letsencrypt['auto_renew'] = true
letsencrypt['auto_renew_hour'] = 5
letsencrypt['auto_renew_day_of_month'] = "*/6"
修改后需重新加載配置:
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
GitLab通過.gitlab-ci.yml文件實現持續集成/部署。在項目根目錄創建該文件,定義流水線階段(如build、test、deploy)及任務:
stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "Running build tasks..."
- ./build_script.sh
test_job:
stage: test
script:
- echo "Running tests..."
- ./run_tests.sh
deploy_job:
stage: deploy
script:
- echo "Deploying to production..."
- ./deploy_script.sh
需注冊GitLab Runner(執行任務的代理):
sudo gitlab-runner register
按照提示輸入GitLab實例URL、注冊令牌及Runner配置(如Docker執行器)。
通過修改CSS或替換模板文件定制GitLab界面:
docker exec -it gitlab /bin/bash
apt update && apt install -y vim
/opt/gitlab/embedded/service/gitlab-rails/public/assets/application.css,調整顏色、布局等樣式。/etc/gitlab/gitlab.rb中指定自定義主題路徑:custom_error_pages['enabled'] = true
custom_error_pages['404'] = '/themes/your-theme/404.html'
修改后重啟GitLab生效。
/etc/gitlab/gitlab.rb中設置SMTP參數,啟用通知郵件:gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.example.com"
gitlab_rails['smtp_port'] = 587
gitlab_rails['smtp_user_name'] = "your-email@example.com"
gitlab_rails['smtp_password'] = "your-password"
gitlab_rails['smtp_domain'] = "smtp.example.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
FROM gitlab/gitlab-ce:latest
RUN apt update && apt install -y your-package
COPY ./themes/your-theme /var/www/gitlab/themes/your-theme
構建并運行容器:docker build -t your-custom-gitlab .
docker run -d -p 443:443 -p 80:80 -p 2222:22 --name gitlab --restart always -v /srv/gitlab/config:/etc/gitlab -v /srv/gitlab/logs:/var/log/gitlab -v /srv/gitlab/data:/var/opt/gitlab your-custom-gitlab
定制完成后,需始終通過sudo gitlab-ctl reconfigure應用配置變更,并通過sudo gitlab-ctl status檢查服務狀態,確保GitLab穩定運行。