docker中gitlab-runner配置

一、啓動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

二、註冊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,以下所示:java

[[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
來源:簡書
簡書著做權歸做者全部,任何形式的轉載都請聯繫做者得到受權並註明出處。

三、 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

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

 

這個時候,咱們從該項目的Pipelines選項卡下能夠看到,有正在運行的剛新建的hwy的這個runner的pipelines了。點擊進去能夠看到控制檯實時輸出日誌。如圖: 
這裏寫圖片描述docker

上面是一個簡單的demo實例,一個pipeline只有一個job的類型,通常咱們CI都是有好幾步組成,好比java項目,咱們先build打包一下,若是成功了在執行一下test,最後咱們deploy到機器上。像多步操做的流程,咱們能夠配置yml文件,分解爲多個job,來依次執行。例以下邊這個.gitlab-ci.yml文件:shell

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流程截圖以下: api

這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述

相關文章
相關標籤/搜索