gitlab持续集成
安装Runner
正常安装
sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
sudo chmod +x /usr/local/bin/gitlab-runner
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start
image: node:8cache:paths:- node_modules/stages:- test- buildtest:stage: testscript:- npm install- ./node_modules/@angular/cli/bin/ng test --browsers PhantomJS --watch=falsebuild:stage: buildscript:- ./node_modules/@angular/cli/bin/ng build --prod
docker in docker 安装
推荐使用此方式安装runner。
-
pull镜像
sudo docker pull gitlab/gitlab-runner:latest
-
创建容器运行
docker run -d --name gitlab-runner --restart always -v /srv/gitlab-runner/config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest
-
查看注册token
查看注册需要的url和token
-
注册runner
sudo docker exec -it gitlab-runner gitlab-ci-multi-runner register
执行完上面命令后,会交互式的让你输入相关信息,如下操作:
# 1、输入上图中的注册URL Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/): http://192.168.2.100:9799/# 2、输入上图中的注册令牌 Please enter the gitlab-ci token for this runner: xxxxxxxxxx# 3、输入描述 如 another runner Please enter the gitlab-ci description for this runner: [gitlab]:another runner# 4、输入Runner的tag 多个的话逗号隔开 Please enter the gitlab-ci tags for this runner (comma separated): my-tag# 7、选择执行类型 输入shell即可 Registering runner... succeeded runner=E7XAAAAA Please enter the executor: docker+machine, docker-ssh, parallels, shell, ssh, virtualbox, docker-ssh+machine, kubernetes, docker: docker
接下来选择镜像根据自己的需求,我所使用的是node:8.9.4
-
注册成功后,在gitlab项目中即可查看gitlab-runner
-
修改gitlab-runner容器内的配置
进入容器内部
sudo docker exec -it gitlab-runner /bin/bash
然后编辑gitlab-runner配置文件
nano /etc/gitlab-runner/config.toml
修改如下
concurrent = 1 check_interval = 0[session_server]session_timeout = 1800[[runners]]name = "Angular build ^ ^ test and deploy."clone_url = "http://192.168.2.100:9799/" # 增加此段,否则默认不带端口,会出现无法克隆项目的情况url = "http://192.168.2.100:9799/"token = "ea6a17d35baf89d3d7ddab5f026428"executor = "shell"[runners.cache][runners.cache.s3][runners.cache.gcs]
查看runner
gitlab-runner list
-
编写.gitlab-ci.yml然后push
test:script:- ls
-
增加部署runner
进入容器
sudo docker exec -it gitlab-runner /bin/bash
注册新的runner
绑定/var/run/docker.sock到容器
gitlab-runner register -n \ > -url http://192.168.2.100:9799/ \ > --registration-token UX91nm9za1YbgGPSymci \ > --executor docker \ > --description "Angular Runner" \ > --docker-image "docker:latest" \ > --docker-volumes /var/run/docker.sock:/var/run/docker.sock
这个runner是使用宿主的docker构建镜像
同样需要在配置文件中增加... clone_url = "http://192.168.2.100:9799/" ...
-
由于gitlab runner会使用宿主机的docker下载一些镜像,以及会有一些缓存,长期使用所占的空间对于一个磁盘空间不足的机器来说是个问题,需要定时清理相应文件。可以使用自己写定时任务清理,也可以使用官方提供的cleanup镜像来定时清理。
docker run -d \ -e LOW_FREE_SPACE=10G \ -e EXPECTED_FREE_SPACE=20G \ -e LOW_FREE_FILES_COUNT=1048576 \ -e EXPECTED_FREE_FILES_COUNT=2097152 \ -e DEFAULT_TTL=10m \ -e USE_DF=1 \ --restart always \ -v /var/run/docker.sock:/var/run/docker.sock \ --name=gitlab-runner-docker-cleanup \ quay.io/gitlab/gitlab-runner-docker-cleanup
docker不加sudo执行
- 如果还没有docker group就添加一个
sudo groupadd docker
2.将用户加入该group内。然后退出并重新登录就生效啦。
sudo gpasswd -a ${USER} docker
3.重启docker
sudo service docker restart
4.将gitlab-runner添加到docker组
sudo gpasswd -a gitlab-runner docker
项目的gitlab runner
[[runners]]name = "Angular Runner"url = "http://192.168.2.100:9799/"token = "a2bf55eaea82fffce1cff48b3ebf3b"executor = "docker"clone_url = "http://192.168.2.100:9799/"[runners.docker]tls_verify = falseimage = "docker:latest"privileged = falsedisable_entrypoint_overwrite = falseoom_kill_disable = falsedisable_cache = falsevolumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]shm_size = 0[runners.cache][runners.cache.s3][runners.cache.gcs]
[[runners]]name = "Angular test build"clone_url = "http://192.168.2.100:9799/"url = "http://192.168.2.100:9799/"token = "b2a10b9b16d5678e141bf5df636853"executor = "docker"[runners.docker]tls_verify = falseimage = "node:8.9.4"privileged = falsedisable_entrypoint_overwrite = falseoom_kill_disable = falsedisable_cache = falsevolumes = ["/cache"]shm_size = 0[runners.cache][runners.cache.s3][runners.cache.gcs]
错误
ERROR: Job failed: Error response from daemon: repository node-cnpm not found: does not exist or no pull access (executor_docker.go:168:3s)
报错的原因在于,gitlab-runner尝试去官方的docker hub仓库拉取镜像。通过修改gitlab-runner中的配置,设置只拉取本地镜像:
never, if-not-present or always (default)
修改 /etc/gitlab-runner/config.toml ,在 [runners.docker] 下,添加:
pull_policy = "if-not-present" # 该配置默认always,即只在线上拉取镜像
gitlab-ci.yml
# 定义 stages
stages:- install_deps- test- build- deploy# 设置缓存
cache:key: ${CI_COMMIT_REF_NAME}paths:- node_modules/# 安装依赖
install_deps:stage: install_depstags:- Angular-clionly:- develop- masterscript:- npm config set registry https://registry.npm.taobao.org --global # 设置淘宝镜像加速- npm install# 测试 job
test: # 这是 Job 的名字,不用与 stage 选项一致,也可以写成 job1stage: testtags:- Angular-cliscript:- echo "unit test successful"# 构建 job
build:stage: buildtags:- Angular-clionly:- develop- masterscript:- ./node_modules/@angular/cli/bin/ng build --prodartifacts: # 成功后将dist发送到gitlab,供后续步骤使用name: "ioa-clent-dist"expire_in: 1 weekpaths:- dist/# 部署 job
deploy:stage: deploydependencies: - build # 是具体的job名称,这样就会自动下载artifactstags:- Angular-Deployonly:- develop- masterscript:- sh deploy.sh