在CentOS上实现GitLab的自动化运维,通常涉及以下几个关键步骤:
更新系统软件包:
sudo yum update -y
安装必要的依赖项:
sudo yum install -y curl policycoreutils openssh-server openssh-client postfix
配置防火墙:
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
添加GitLab软件仓库:
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
安装GitLab CE:
sudo EXTERNAL_URL="http://your_server_ip" yum install -y gitlab-ce
配置GitLab:
sudo vi /etc/gitlab/gitlab.rb
external_url 'http://your_server_ip'
sudo gitlab-ctl reconfigure
sudo gitlab-ctl start
安装GitLab Runner:
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
sudo apt-get install gitlab-runner
注册GitLab Runner:
在GitLab项目页面,导航到 Settings -> CI/CD -> Runners
,点击“Register an instance runner”。复制提供的Registration token,然后在你的CentOS服务器上以root用户身份执行以下命令来注册Runner:
sudo gitlab-runner register --non-interactive --executor "docker" --docker-image alpine:latest --url "http://your_server_ip" --registration-token "your_registration_token" --description "docker-runner" --tag-list "newdocker" --run-untagged "true" --locked "false" --docker-volumes /var/run/docker.sock:/var/run/docker.sock --docker-privileged "true" --access-level "not_protected"
.gitlab-ci.yml
文件创建 .gitlab-ci.yml
文件:
在项目的根目录下创建一个名为 .gitlab-ci.yml
的文件,定义CI/CD流程。以下是一个简单的示例:
stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "Building the application..."
- mkdir -p build
- touch build/info.txt
test_job:
stage: test
script:
- echo "Running tests..."
- test -f build/info.txt
deploy_job:
stage: deploy
script:
- echo "Deploying the application..."
提交 .gitlab-ci.yml
文件:
将 .gitlab-ci.yml
文件提交到你的GitLab仓库,并推送到远程仓库:
git add .gitlab-ci.yml
git commit -m "Add GitLab CI/CD configuration"
git push origin master
安装Ansible:
sudo apt-get update
sudo apt-get install ansible
创建Ansible Playbook: 创建一个Ansible Playbook来自动化部署GitLab Server:
---
- name: Deploy GitLab
hosts: gitlab_server
become: yes
tasks:
- name: Update apt cache
apt:
update_cache: yes
- name: Upgrade packages
apt:
name:
- gitlab-ce
state: latest
- name: Restart GitLab
systemd:
name: gitlab
state: restarted
配置免密登录: 在控制节点上配置免密登录GitLab Server节点:
ssh-keyscan gitlab.example.com >> ~/.ssh/known_hosts
通过上述步骤,你可以在CentOS上实现GitLab的自动化运维,提高开发和部署效率。