安装Gitlab开源版
使用Docker安装
bash
sudo docker run --detach \
--hostname gitlab.example.com \
--env GITLAB_OMNIBUS_CONFIG="external_url 'http://${ip}:9999/'; gitlab_rails['gitlab_shell_ssh_port'] = 8822;" \
--publish 443:443 --publish 9999:9999 --publish 2224:22 \
--name my-gitlab \
--restart always \
--volume /home/docker/mount/gitlab/config:/etc/gitlab \
--volume /home/docker/mount/gitlab/logs:/var/log/gitlab \
--volume /home/docker/mount/gitlab/data:/var/opt/gitlab \
--shm-size 256m \
gitlab/gitlab-ce:latest
注意:${ip}为当前服务器的ip,9999为访问的端口可自定义。安装时间需要比较久,大概2-5分钟,耐心等待就好了。如果等很久之后还是无法打开,需要重启该容器。
打开链接http://${ip}:9999/就能访问Gitlab的web页面了,如果打开链接后出现如图页面,再等一等就可以使用了。
成功后的页面
获取登录的密码
用户名为root,密码在挂载目录config的initial_root_password文件里
bash
cat /home/docker/mount/gitlab/config/initial_root_password

该文件24小时后会被自动删除,记得保存密码
重置密码
如果该文件中的密码不正确,我们可以自己重置密码,进入Gitlab容器内部,在容器内部执行命令 gitlab-rake "gitlab:password:reset"。也可查看官方文档提供的其他方法:官方文档-找回密码
bash
gitlab-rake "gitlab:password:reset"

登录
登录成功后我们就成功安装好Gitlab了
安装Gitlab Runner
注意:安装前需先安装Git 安装Git 指定版本
查看Linux的发行版,教程使用的是CentOS,其他发行版安装可查看官方文档-安装Gitlab Runner
bash
cat /etc/redhat-release && cat /etc/lsb-release
下载官方 GitLab 存储库
bash
curl -LJO "https://gitlab-runner-downloads.s3.amazonaws.com/latest/rpm/gitlab-runner_amd64.rpm"
安装GitLab Runner
bash
rpm -i gitlab-runner_amd64.rpm
安装成功后,查看版本
bash
gitlab-runner --version

注册Gitlab Runner
获取Gitlab Runner注册时所需要的token
创建项目
点击Settings > CI/CD > Runners

注册
在安装了Gitlab Runner的服务器上执行命令 sudo gitlab-runner register
bash
sudo gitlab-runner register
- 运行注册命令 sudo gitlab-runner register
- 输入您的 GitLab URL
- 输入您获得令牌(token)
- 输入描述
- 输入标签,以逗号分隔
- 输入可选维护说明
- 输入执行者的类型,由你安装Gitlab Runner方式决定,教程是使用的是shell

注册成功后,可以在Gitlab页面看到Runner信息,还可以直接编辑、删除
Gitlab CI/CD 验证
在Gitlab项目中新建.gitlab-ci.yml文件
.gitlab-ci.yml内容如下
yml
variables:
APP_NAME: gitlab-demo
stages:
- build
- deploy
build:
stage: build
tags:
- build
only:
- dev
- test
- main
script:
- echo "==================start build ================"
deploy:
stage: deploy
tags:
- deploy
only:
- dev
- test
- main
script:
- echo "==================start deploy ================"

点击提交后,查看CI/CD