以下是Debian环境下GitLab的自动化部署方案,涵盖安装、配置及CI/CD流程:
系统准备
更新系统并安装依赖:
sudo apt update && sudo apt install -y curl openssh-server ca-certificates postfix
添加GitLab仓库并安装
执行官方脚本添加仓库,安装GitLab CE:
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash sudo apt install gitlab-ce
配置GitLab
编辑 /etc/gitlab/gitlab.rb
,设置 external_url
(如 http://your-server-ip
),然后执行:
sudo gitlab-ctl reconfigure sudo systemctl restart gitlab
开放防火墙(可选)
若启用防火墙,需放行HTTP/HTTPS端口:
sudo ufw allow http sudo ufw allow https
安装GitLab Runner
在目标服务器安装Runner:
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash sudo apt install gitlab-ci-multi-runner sudo gitlab-runner register # 按提示输入GitLab URL和Token
配置.gitlab-ci.yml
在项目根目录创建文件,定义构建、测试、部署阶段:
stages: - build - test - deploy build_job: stage: build script: - echo "Building application..." - mvn package # 示例:Java项目构建 test_job: stage: test script: - echo "Running tests..." - mvn test deploy_job: stage: deploy script: - echo "Deploying to production..." - scp target/app.jar user@server:/opt/app # 示例:部署到服务器 environment: production only: [main] # 仅主分支触发
触发自动化流程
提交代码到GitLab后,CI/CD会自动按阶段执行任务,部署结果可在GitLab项目→CI/CD→Pipelines中查看。
/etc/gitlab/gitlab.rb
中启用Let’s Encrypt,实现HTTPS访问。以上步骤参考自,可根据实际需求调整配置。