docker中gitlab-runner配置


1、启动gitlab-runner

docker run -d --name gitlab-runner --restart always \
  -v /opt/data/gitlab-runner/config:/etc/gitlab-runner \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner

2、注册runner

docker exec -it gitlab-runner gitlab-ci-multi-runner register -n \  
  --url http://gitlab.8dat.com/ci \
  --registration-token ip-Adefsfsfasdfdsfjkljd\
  --tag-list=dev,uat,prod \
  --description "project_build_runner" \
  --docker-privileged=false \
  --docker-pull-policy="if-not-present" \
  --docker-image "mvn-jdk:latest" \
  --docker-volumes /var/run/docker.sock:/var/run/docker.sock \
  --docker-volumes /opt/data/gitlab-runner/.m2:/root/.m2 \
  --executor docker


# gitlab-runner register

Please enter the gitlab-ci coordinator URL:
# 示例:http://gitlab.alibaba-inc.com/ci
Please enter the gitlab-ci token for this runner:
# xxxxxx
Please enter the gitlab-ci description for this runner:
# 示例:qd_api_runner
Please enter the gitlab-ci tags for this runner (comma separated):
# 示例:hwy
Whether to run untagged builds [true/false]:
# true
Please enter the executor: docker, parallels, shell, kubernetes, docker-ssh, ssh, virtualbox, docker+machine, docker-ssh+machine:
# docker
Please enter the default Docker image (e.g. ruby:2.1):
# maven:3-jdk-8

注: url 和token来自 gitlab 的 Settings--> CI/CD --> Runners

进入gitlab-runner容器生成的配置文件 cat /etc/gitlab-runner/config.toml,如下所示:

[[runners]]
  name = "dev_build_runner"
  url = "http://gitlab.XXX.top/"
  token = "9103bafa16b1f63232323434345"
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "mvn-jdk:latest"
    privileged = false
    disable_cache = false
    volumes = ["/cache","/opt/data/gitlab-runner/.m2:/root/.m2"]
    pull_policy = "if-not-present"
    shm_size = 0
  [runners.cache]
  • volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache","/root/m2:/root/.m2"]中增加了/root/m2:/root/.m2,避免maven每次都下载jar包,提高效率。
  • pull_policy = "if-not-present" 避免docker 镜像每次都pull

作者:大罗马的太阳
链接:https://www.jianshu.com/p/c1effc3179be
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

3、 project中 .gitlab-ci.yml的内容如下:

image: mvn-jdk:latest

variables:
  MAVEN_OPTS: "-Dmaven.repo.local=/root/.m2/repository"

stages:
  - build

build:config:
  stage: build
  script:
    - mvn clean package -DskipTests

我们提交该文件到gitlab对应项目上去。

git add .gitlab-ci.yml
git commit -m "Add .gitlab-ci.yml"
git push origin develop

这个时候,我们从该项目的Pipelines选项卡下可以看到,有正在运行的刚新建的hwy的这个runner的pipelines了。点击进去可以看到控制台实时输出日志。如图:

上面是一个简单的demo实例,一个pipeline只有一个job的类型,一般我们CI都是有好几步组成,比如java项目,我们先build打包一下,如果成功了在执行一下test,最后我们deploy到机器上。像多步操作的流程,我们可以配置yml文件,分解为多个job,来依次执行。例如下边这个.gitlab- ci.yml文件:

image: maven:3-jdk-8

stages:
  - build
  - test
  - deploy

job_build:
  stage: build
  script:
    - mvn clean install
  only:
    - develop
  tags:
    - hwy

job_test:
  stage: test
  script:
    - mvn test
  tags:
    - hwy

job_deploy:
  stage: deploy
  script:
    - echo "deploy over..."
  tags:
    - hwy

说明:这个yml文件就是有3个job组成,依次为build、test、deploy。在执行这个CI流程时候,会分解成3个job依次执行。这里tags: hwy就是指定使用哪个runner来执行这个job,我们也可以执行其他已注册可用的runner。详细gitlab pipline流程截图如下:





原文链接:https://www.cnblogs.com/river2005/p/9527030.html