gitlab Continuous Integration
Installation Runner
Normal installation
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:8
cache:
paths:
- node_modules/
stages:
- test
- build
test:
stage: test
script:
- npm install
- ./node_modules/@angular/cli/bin/ng test --browsers PhantomJS --watch=false
build:
stage: build
script:
- ./node_modules/@angular/cli/bin/ng build --prod
docker in docker installation
This method is recommended to install runner.
-
pull Mirror
sudo docker pull gitlab/gitlab-runner:latest
-
Create a container operation
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
-
View registration token
View url and token registration required
-
Registration runner
sudo docker exec -it gitlab-runner gitlab-ci-multi-runner register
After executing the above command will let you interactively enter the relevant information, as follows:
# 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
Next, select the image according to their needs, I use a node: 8.9.4
-
After successful registration, you can see gitlab-runner in gitlab project
-
Modify the configuration of the container gitlab-runner
Into the interior of the container
sudo docker exec -it gitlab-runner /bin/bash
Then edit the configuration file gitlab-runner
nano /etc/gitlab-runner/config.toml
amend as below
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]
View runner
gitlab-runner list
-
Write .gitlab-ci.yml then push
test: script: - ls
-
The deployment runner
Into the container
sudo docker exec -it gitlab-runner /bin/bash
Register a new runner
Bind to the container /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
The runner is used to build the mirror docker host
also needs to increase in the configuration file... clone_url = "http://192.168.2.100:9799/" ...
-
Since gitlab runner will use the host's docker download some images, and there will be some cache, the space occupied by long-term use of a shortage of disk space for the machine is a problem, need to regularly clean up the file. You can use to write their own scheduled tasks to clean up, you can also use the cleanup mirroring official to regularly clean up.
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 executed without sudo
- If you do not add a docker group on
sudo groupadd docker
2. Users of the inner group. Then it out and sign it into force again.
sudo gpasswd -a ${USER} docker
3. Restart docker
sudo service docker restart
4. Add to the gitlab-runner group docker
sudo gpasswd -a gitlab-runner docker
The project 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 = false
image = "docker:latest"
privileged = false
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/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 = false
image = "node:8.9.4"
privileged = false
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/cache"]
shm_size = 0
[runners.cache]
[runners.cache.s3]
[runners.cache.gcs]
error
ERROR: Job failed: Error response from daemon: repository node-cnpm not found: does not exist or no pull access (executor_docker.go:168:3s)
The reason given is that, gitlab-runner try to official docker hub warehouse pull the mirror. By modifying the configuration gitlab-runner is provided only pulling local mirror:
never, if-not-present or always (default)
Modified /etc/gitlab-runner/config.toml, in [runners.docker] Under added:
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_deps
tags:
- Angular-cli
only:
- develop
- master
script:
- npm config set registry https://registry.npm.taobao.org --global # 设置淘宝镜像加速
- npm install
# 测试 job
test: # 这是 Job 的名字,不用与 stage 选项一致,也可以写成 job1
stage: test
tags:
- Angular-cli
script:
- echo "unit test successful"
# 构建 job
build:
stage: build
tags:
- Angular-cli
only:
- develop
- master
script:
- ./node_modules/@angular/cli/bin/ng build --prod
artifacts: # 成功后将dist发送到gitlab,供后续步骤使用
name: "ioa-clent-dist"
expire_in: 1 week
paths:
- dist/
# 部署 job
deploy:
stage: deploy
dependencies:
- build # 是具体的job名称,这样就会自动下载artifacts
tags:
- Angular-Deploy
only:
- develop
- master
script:
- sh deploy.sh