给gitlab添加gitlab-ci和gitlab-runner

给gitlab添加gitlab-ci和gitlab-runner

说明

使用的是gitlab v7.13.3
run在docker中。
配置了ldap登录,和smtp邮件通知。失败时会发送邮件通知
最近测试gitlab-ci服务。

服务配置:

直接在配置文件gitlab.rb底部打开相关配置,即可使用。

############################################
# Url on which GitLab CI will be reachable #
############################################
## see https://gitlab.com/gitlab-org/omnibus-gitlab/tree/629def0a7a26e7c2326566f0758d4a27857b52a3/doc/gitlab-ci/README.md
ci_external_url 'http://ci.gitlab.yourdomain.com'
#################################
# application.yml configuration #
#################################
# 这个app_id在gitlab中创建application后获得
gitlab_ci['gitlab_server'] = { "url" => 'http://gitlab.yourdomain.com', "app_id" => 'xxxxxxxxxxxx', "app_se
cret" => 'yyyyyyyyyyyyyyy' }
gitlab_ci['gitlab_ci_email_from'] = '[email protected]'
gitlab_ci['gitlab_ci_support_email'] = '[email protected]'
gitlab_ci['gitlab_ci_all_broken_builds'] = true
gitlab_ci['gitlab_ci_add_pusher'] = true
gitlab_ci['builds_directory'] = '/var/opt/gitlab/gitlab-ci/builds'
gitlab_ci['gravatar_enabled'] = false
# gitlab_ci['gravatar_plain_url'] = "http://www.gravatar.com/avatar/%{hash}?s=%{size}&d=mm"
# gitlab_ci['gravatar_ssl_url'] = "https://secure.gravatar.com/avatar/%{hash}?s=%{size}&d=mm"
## For setting up backups
## see https://gitlab.com/gitlab-org/omnibus-gitlab/blob/629def0a7a26e7c2326566f0758d4a27857b52a3/README.md#backups
gitlab_ci['backup_path'] = "/var/opt/gitlab/ci-backups"
# gitlab_ci['backup_keep_time'] = 604800
# gitlab_ci['backup_upload_connection'] = {
# 'provider' => 'AWS',
# 'region' => 'eu-west-1',
# 'aws_access_key_id' => 'AKIAKIAKI',
# 'aws_secret_access_key' => 'secret123'
# }

配置gitlab-runner

PS:新版本gitlab-runner从ruby改为go语言实现,名字为gitlab-ci-multi-runner,软链接为gitlab-runner
官方文档:Run gitlab-runner in a container
启动gitlab-runner服务

docker run -d --name gitlab-runner --restart always \
-v /srv/gitlab-runner/config:/etc/gitlab-runner \
gitlab/gitlab-runner:latest

也可以用官方的另一个实现:

gitlab/gitlab-runner latest 3e8077e209f5 13 hours ago 304.3 MB
gitlab/gitlab-runner alpine 7c431ac8f30f 13 hours ago 25.98 MB

注册runner

docker exec -it gitlab-runner gitlab-runner register
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/ci):
http://ci.gitlab.yourdomain.com/
Please enter the gitlab-ci token for this runner:
# 这个token在gitlab-ci中Runners选项卡获得
e0ab42c98fff06b3dd4f3d4c776bc4
Please enter the gitlab-ci description for this runner:
[3366d1121148]: test-ci
Please enter the gitlab-ci tags for this runner (comma separated):
delpoy
INFO[0086] e0ab42c9 Registering runner... succeeded
Please enter the executor: ssh, shell, parallels, docker, docker-ssh:
[]: shell
INFO[0097] Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

注册成功后,查看生成的配置文件

# cat /srv/gitlab-runner/config/config.toml
concurrent = 1
[[runners]]
url = "http://ci.gitlab.yourdomain.com/"
token = "c87c7ad883af316b7d173c1094c6fe"
name = "test-ci"
executor = "shell"
[runners.ssh]
[runners.docker]
image = ""
privileged = false
[runners.parallels]
base_name = ""

debug:
docker logs -f gitlab-runner
docker exec -it gitlab-runner gitlab-runner --help

使用gitlab-ci

打开http://ci.gitlab.yourdomain.com ,会跳转到http://gitlab.yourdomain.com去做验证,然后跳回。
选择一个需要CI的项目,点击”Add project to CI”。

#####.gitlab-ci.yml配置:
在项目中添加.gitlab-ci.yml文件,然后push到gitlab
git add . ; git commit -m 'test' ; git push
注意:有坑!如果按照官方最新文档的话,会构建失败,因为句法有变化,可用下面这个链接在线验证配置文件。
在线编辑器

下面是一个栗子

staging:
type: deploy
script:
- echo 'deploy to test env' >> /tmp/test-ci.log
- echo "CI:$CI CI_SERVER:$CI_SERVER CI_SERVER_NAME:$CI_SERVER_NAME GITLAB_CI:$GITLAB_CI CI_SERVER_VERSION:$CI_SERVER_VERSION CI_SERVER_REVISION:$CI_SERVER_REVISION CI_BUILD_REF:$CI_BUILD_REF CI_BUILD_BEFORE_SHA:$CI_BUILD_BEFORE_SHA CI_BUILD_REF_NAME:$CI_BUILD_REF_NAME CI_BUILD_ID:$CI_BUILD_ID CI_BUILD_REPO:$CI_BUILD_REPO CI_PROJECT_DIR:$CI_PROJECT_DIR" >>/tmp/test-ci.log
# only:
# - master
production:
type: deploy
script:
- echo 'deploy to production env' >> /tmp/test-ci.log
- echo "CI:$CI CI_SERVER:$CI_SERVER CI_SERVER_NAME:$CI_SERVER_NAME GITLAB_CI:$GITLAB_CI CI_SERVER_VERSION:$CI_SERVER_VERSION CI_SERVER_REVISION:$CI_SERVER_REVISION CI_BUILD_REF:$CI_BUILD_REF CI_BUILD_BEFORE_SHA:$CI_BUILD_BEFORE_SHA CI_BUILD_REF_NAME:$CI_BUILD_REF_NAME CI_BUILD_ID:$CI_BUILD_ID CI_BUILD_REPO:$CI_BUILD_REPO CI_PROJECT_DIR:$CI_PROJECT_DIR" >>/tmp/test-ci.log
only:
- tags
# except:
# - stable
# - /^deploy-.*$/
# cat /tmp/test-ci.log
deploy to test env
CI:true CI_SERVER:yes CI_SERVER_NAME:GitLab CI GITLAB_CI:true CI_SERVER_VERSION: CI_SERVER_REVISION: CI_BUILD_REF:66b8d99e51ff39e98629068bb5e9ef302288e899 CI_BUILD_BEFORE_SHA:b7c8b5d458375902426bb0777da3c0ddf553f308 CI_BUILD_REF_NAME:my-dev CI_BUILD_ID:9 CI_BUILD_REPO:http://gitlab-ci-token:[email protected]/arkii/test-ci.git CI_PROJECT_DIR:builds/53b58d35/0/arkii/test-ci
deploy to production env
CI:true CI_SERVER:yes CI_SERVER_NAME:GitLab CI GITLAB_CI:true CI_SERVER_VERSION: CI_SERVER_REVISION: CI_BUILD_REF:66b8d99e51ff39e98629068bb5e9ef302288e899 CI_BUILD_BEFORE_SHA:b7c8b5d458375902426bb0777da3c0ddf553f308 CI_BUILD_REF_NAME:my-dev CI_BUILD_ID:10 CI_BUILD_REPO:http://gitlab-ci-token:[email protected]/arkii/test-ci.git CI_PROJECT_DIR:builds/0c3c27e4/0/arkii/test-ci

#####看到这些变量的输出,大家有思路该怎么玩了吗?

官方文档Configuration of your builds with .gitlab-ci.yml
特别重要的说明官方说明
How your build script is run

The runner runs the line below before it runs the commands in your build script:
cd /gitlab-ci-runner/tmp/builds && git clone git@gitlab_server_fqdn:group/project.git project-1 && cd project-1 && git checkout master
Build script example

bundle install
bundle exec rake db:create RAILS_ENV=test
bundle exec rake db:migrate RAILS_ENV=test
script/run_all_tests

Environmental variables

The runner sets the following environmental variables:

CI=true
CI_SERVER=true
CI_SERVER_NAME=GitLab CI
GITLAB_CI=true
CI_SERVER_VERSION
CI_SERVER_REVISION
CI_BUILD_REF
CI_BUILD_BEFORE_SHA
CI_BUILD_REF_NAME (branch)
CI_BUILD_ID
CI_BUILD_REPO
CI_PROJECT_DIR