GitLab CI + Docker 实现持续集成
GitLab CI + Docker 实现持续集成
目录
一、概述
使用Gitlab-Runner docker模式
官网相关参考:
https://docs.gitlab.com/runner/install/docker.html
https://docs.gitlab.com/runner/configuration/autoscale.html
二、安装Docker
$ yum install -y yum-utils device-mapper-persistent-data lvm2
$ yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
$ yum makecache fast
$ yum -y install docker-ce
#启动docker
$ systemctl start docker.service
#配置开机启动docker
$ systemctl enable docker.service
三、安装gitlab-runner镜像
$ docker run -d --name gitlab-runner1 --restart always \
-v /srv/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
gitlab/gitlab-runner:latest
四、注册gitlab-runner
登录gitlab,选择要集成CICD的项目,Settings->CI/CD Settings->Runners settings 获得url和token
$ docker exec gitlab-runner1 gitlab-ci-multi-runner register -n \
--url https://git.xxxx.com/ \
--registration-token yourtoken \
--tag-list runInDk \
--executor docker \
--docker-pull-policy="if-not-present" \
--docker-image "maven:3.5.4-jdk-8" \
--docker-volumes /root/.ssh:/root/.ssh \
--docker-volumes /var/runcache:/cache \
--docker-volumes /root/.m2:/root/.m2 \
--docker-volumes /root/.npm:/root/.npm \
--docker-volumes /var/run/docker.sock:/var/run/docker.sock \
--description "100-runner1"
五、修改gitlab-runner参数
$ vi /srv/gitlab-runner/config/config.toml
修改并发数 concurrent
调整日志输出最大长度,增加output_limit = 8192
修改镜像拉取策略,仅本地不存在时才拉取,增加 pull_policy = "if-not-present"
concurrent = 1
check_interval = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "100-runner1"
url = "https://git.xxxx.com/"
token = "yourtoken"
executor = "docker"
output_limit = 8192
[runners.docker]
tls_verify = false
image = "maven:3.5.4-jdk-8"
privileged = true
disable_cache = false
pull_policy = "if-not-present"
volumes = ["/root/.ssh:/root/.ssh","/var/run/docker.sock:/var/run/docker.sock","/cache","/home/.m2:/root/.m2:rw","/home/.npm:/root/.npm:rw"]
shm_size = 0
[runners.cache]
重启gitlab-runner
$ docker restart gitlab-runner
六、启动gitlab-runner容器
$ docker start gitlab-runner1
#查看状态
$ docker status gitlab-runner1
#重启
$ docker restart gitlab-runner1
七、.gitlab-ci.yml配置示例
注意:tags配置,需要在注册时的tag-list中(可以在gitlab界面上修改)
variables:
VAR_JAR_NAME: 'myapp'
VAR_FULLPATH_JAR_NAME: '/app/bin/lib/myapp.jar'
VAR_FULLPATH_JAR_DIR: '/app/bin/lib/'
VAR_FULLPATH_JAR_DEPLOYMENT: '/app/bin/start_myapp.sh'
stages:
- build
- install
- deploy
build-dev:
stage: build
image: maven:3.5.4-jdk-8
script:
- mvn clean package -e -Dmaven.test.skip=true
artifacts:
paths:
- $VAR_JAR_NAME/target/$VAR_JAR_NAME-*.jar
expire_in: 40 mins
tags:
- runInDk
only:
- dev
install-dev:
stage: install
script:
- scp ./$VAR_JAR_NAME/target/$VAR_JAR_NAME-*.jar appuser@192.168.1.100:${VAR_FULLPATH_JAR_DIR}
- ssh appuser@192.168.1.100 "rm -rf ${VAR_FULLPATH_JAR_NAME}"
dependencies:
- build-dev
tags:
- dam
only:
- dev
deploy-dev:
stage: deploy
script:
- ssh appuser@192.168.1.100 ${VAR_FULLPATH_JAR_DEPLOYMENT}
tags:
- runInDk
only:
- dev
如果,gitlab-runner与应用部署不在一台机器上,需要先配置免密认证
在gitllab-runner机器上执行:
$ ssh-keygen -t rsa (首次)
#假设192.168.1.101为应用部署机器
$ scp -p ~/.ssh/id_rsa.pub root@192.168.1.101:/root/.ssh/authorized_keys
八、其他docker相关命令
#查看所有镜像
$ docker image ls
#查看所有容器
$ docker ps -a 或 docker container ls -a
#停止容器及删除
$ docker stop 79659aa80bb9 & docker rm 79659aa80bb9