gitlab 安装和基本使用
这篇文章是站在运维人员的立场,测试最核心的功能Central Git Repo 和 GitLab CI。
1. 安装GitLab
参考官方文档 install on ubuntu18
1.1 安装依赖包
sudo apt-get update
sudo apt-get install -y curl openssh-server ca-certificates
sudo apt-get install -y postfix
邮件配置时候选择Internet即可
1.2 下载和安装 GitLab
$ curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 5933 0 5933 0 0 7416 0 --:--:-- --:--:-- --:--:-- 7406
Detected operating system as Ubuntu/bionic.
Checking for curl...
Detected curl...
Checking for gpg...
Detected gpg...
Running apt-get update... done.
Installing apt-transport-https... done.
Installing /etc/apt/sources.list.d/gitlab_gitlab-ce.list...done.
Importing packagecloud gpg key... done.
Running apt-get update... done.
The repository is setup! You can now install packages.
Next, install the GitLab package. Change https://roy-gitlab.eastasia.cloudapp.azure.com
to the URL at which you want to access your GitLab instance.
sudo EXTERNAL_URL="http://roy-gitlab.eastasia.cloudapp.azure.com" apt-get install gitlab-ce
1.3 网页登陆
第一次登陆,要重设密码,然后用 root 账户登陆进去。
img2. 创建Project,设置 ssh key
img
3. clone repo
在客户端执行
$ git clone git@roy-gitlabce.eastasia.cloudapp.azure.com:root/demo.git
现在就可以修改代码并push 代码了。
4. 安装runner
这是给GitLab CI,做准备的。
在新的一台VM操作(以ubuntu为例)。
runner 相当于 Jenkins 中的 slave,job 就在这里运行。
https://docs.gitlab.com/runner/install/linux-repository.html
Add GitLab’s official repository:
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
Install the latest version of GitLab Runner
sudo apt-get install gitlab-runner
公用的runner,找 token 去 admin/runners
page
Project runner,去 Settings > CI/CD 获取token
$ sudo gitlab-runner register
Runtime platform arch=amd64 os=linux pid=17752 revision=ac8e767a version=12.6.0
Running in system-mode.
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://roy-gitlabce.eastasia.cloudapp.azure.com/
Please enter the gitlab-ci token for this runner:
GgJxBX1KPeuCK7-8ut6-
Please enter the gitlab-ci description for this runner:
[roy-runner]: my-runner
Please enter the gitlab-ci tags for this runner (comma separated):
my-tag
Registering runner... succeeded runner=GgJxBX1K
Please enter the executor: parallels, shell, ssh, virtualbox, docker, docker-ssh, docker-ssh+machine, kubernetes, custom, docker+machine:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
5. CI/CD 测试
参考官方文档:https://docs.gitlab.com/ee/ci/
在demo repo 根目录创建文件 .gitlab-ci.yml
,内容如下
# 定义 stages(阶段,会依次执行)
stages:
- install_deps
- build_prod
- deploy_prod
# 安装构建依赖
install_deps_job:
stage: install_deps
# 在哪个分支才会执行脚本
only:
# - dev
# - release
- master
script:
- echo 'It is pre-install phase'
tags:
- my-tag
build_prod_job:
stage: build_prod
only:
- master
script:
- echo 'It is build phase'
tags:
- my-tag
deploy_prod_job:
stage: deploy_prod
only:
- master
script:
- echo 'It is deploy phase'
tags:
- my-tag
.gitlab-ci.yml的具体写法,以及关键字含义见: 链接
把它push 到GitLab
git add .gitlab-ci.yml
git commit -m "Add .gitlab-ci.yml"
git push origin master
GitLab 会自动触发CI/CD
image-20191223171325659.png可以看到Pipeline里面 3个job build 成功,可以点击进去,看详细的日志。
网友评论