以下操作均在CentOs下操作
1、Gitlab install
① 啟動gitlab
docker run --detach \
--hostname 115.30.149.35 \
--publish 8888:8888 --publish 24:22 \
--name gitlab \
--restart always \
--volume /data/docker/gitlab/config:/etc/gitlab \
--volume /data/docker/gitlab/logs:/var/log/gitlab \
--volume /data/docker/gitlab/data:/var/opt/gitlab \
--privileged=true \
gitlab/gitlab-ce:latest
--hostname 當前服務器的域名
--publish 24:22 SSH訪問端口 默認22:22
--publish 8888:8888 http訪問的端口 默認是80:80
--publish 443:443 https訪問的端口
--privileged=true 授權
--volume 三個卷掛載gitlab的數據,需要提前創建好
443是https端口 80默認是http端口 根據官網文檔,更改端口需要更改對應的配置文件。所在的配置信息存放在
/etc/gitlab/gitlab.rb。映射在主機的/data/docker/gitlab/config。
② 配置GitLab
由於我們沒用使用默認的暴露端口,因此需要修改 /data/docker/gitlab/config/gitlab.rb。
Ⅰ、修改external_url
:
# For HTTP external_url "http://115.30.149.35:8888"
or
For HTTPS (notice the https)
external_url "http://115.30.149.35:8888"
Ⅱ、修改gitlab_shell_ssh_port
:
itlab_rails['gitlab_shell_ssh_port'] = 24
更多配置信息可以參考官方文檔。
③ 重啟gitlab
docker restart gitlab
④ 訪問gitlab
瀏覽器輸入 http://115.30.149.35:8888
官方建議4G內存,啟動gitlab很吃CPU。
1核2G gitlab啟動服務器卡死。解決方案:
https://yq.aliyun.com/articles/52098
2、Gitlab Runner Install
①安裝gitlab-runnner rpm包
在這里選擇適合你系統的gitlab runner包https://gitlab-runner-downloads.s3.amazonaws.com/latest/index.html
curl -LJO https://gitlab-runner-downloads.s3.amazonaws.com/latest/rpm/gitlab-runner_amd64.rpm
②安裝rpm 包
rpm -i gitlab-runner_amd64.rpm
③注冊Runner
Ⅰ、Run the following command
gitlab-runner register
Ⅱ、Enter your GitLab instance URL:(GitLab實例地址,上面GitLab的地址)
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
http://115.30.149.35:8888
Ⅲ、Enter the token you obtained to register the Runner(輸入注冊Runner的token):
Please enter the gitlab-ci token for this runner
hUCad4cXYPEKTt
token所在地方:
1、登入GitLabhttp://115.30.149.35:8888
2、點擊工具刀
3、Overview->Runners
紅色框里面即為token。
Ⅳ、Enter a description for the Runner, you can change this later in GitLab’s UI(添加Runner描述):
Please enter the gitlab-ci description for this runner
my-runner
Ⅴ、Add a Tag
Please enter the gitlab-ci tags for this runner (comma separated):
Ⅵ、Enter the Runner executor:
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
shell
④刷新GitLab頁面
可看到注冊的Runner。
3、提交項目到gitlab
1、創建一個項目
2、按照提示將我們的項目提交到gitlab上(Push an existing folder)
git remote add origin http://115.30.149.35:8888/root/miniserver-userapi.git
關閉VS
git push 的提示要輸入gitlab的用戶名和密碼
編寫以下.gitlab-ci.yml文件(需要放在整個項目的根目錄)
提交 在具體的project 的CICD下即可看到項目在跑
4、問題集合
問題一、
docker gitlab 的用戶組沒有加入docker
usermod -aG docker gitlab-runner
sudo service docker restart
問題二、某次CI失敗后再啟動
fatal: git fetch-pack: expected shallow list
fatal: The remote end hung up unexpectedly
原因是git版本太老不持之新API,需要升級一下git:
#安裝源
yum install http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-2.noarch.rpm
#安裝git
yum install git
#更新git
yum update git
參考資料:
Gitlab:https://docs.gitlab.com/omnibus/docker/
GitLab Runner:https://docs.gitlab.com/runner/install/linux-manually.html