gitlab-runner使用docker executor

文章目录

  • gitlabrunner 使用docker executor
    • 在gitlab上找到url和token
    • 启动容器
    • 注册runner
      • 进入容器
      • 创建runner
        • 大致需要以下信息
        • 修改runner详细配置
          • [runners.docker.sysctls] 作用等效于
    • gitlabrunner宿主机配置
    • gitlab配置
      • Jenkinsfile简单示例
    • gitlab同时可以使用k8s和docker等多种runner

gitlabrunner 使用docker executor

gitlab-runner使用kubernetes executor

在gitlab上找到url和token

  1. 打开你想要配置CICD的项目或项目组
  2. runner配置地址:设置->CICD->Runner->展开
  3. 找到设置runner需要的url和token,拿小本本记下来

启动容器

gitlab-runner版本:11.7.0

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

注册runner

进入容器

docker exec -it $(docker ps -aq --filter name=gitlab-runner) /bin/bash

创建runner

gitlab-runner register

大致需要以下信息

  • url:小本本上有
  • token:小本本上有
  • description:myrunner with docker executor
  • tags:docker,node1
  • executor:docker

修改runner详细配置

vi /etc/gitlab-runner/

concurrent = 4
check_interval = 0

[session_server]
    session_timeout = 1800

[[runners]]
    name = "myrunner with docker executor"
    url = "http://${gitlabIP}"  #gitlab地址
    #特别注意,这个token不是小本本上的token!!!
    token ="......"
    executor = "docker"
    [runners.docker]
        host = ""
        hostname = ""
        privileged = false
        tls_verify = false
        disable_entrypoint_overwrite = false
        oom_kill_disable = false
        disable_cache = true
        pull_policy = "if-not-present"
        #这是runner的默认镜像;具体镜像maven:3-jdk-8在.gitlab-ci.yml中配置
        image = "busybox:latest"  
        helper_image = "gitlab-runner-helper:x86_64-f100a208"
        #映射maven配置
        volumes = ["/home/v_in_docker/:/usr/share/maven/conf2/:rw"]
        [runners.docker.sysctls]
            "net.ipv4.ip_forward" = "1"
    [runners.cache]
        [runners.cache.s3]
        [runners.cache.gc3]
[runners.docker.sysctls] 作用等效于
sudo vi /etc/sysctl.conf
#加一行:
net.ipv4.ip_forward=1

sudo sysctl -p

gitlabrunner宿主机配置

  • 在/home/v_in_docker/下,创建maven settings.xml
  • 下载所需镜像

gitlab配置

打开runner配置地址,可以看到刚才创建的runner,以及连接状态。
在项目根目录下创建Jenkinsfile

Jenkinsfile简单示例

variables:
  MAVEN_OPTS: "-Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
  MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true --settings /usr/share/maven/conf2/settings.xml"

compile:
  stage: build
  script:
    - 'mvn $MAVEN_CLI_OPTS compile'
  image: $(registryIP)/gitlab-runner/maven:3-jdk-8
  artifacts:
    name: "jars"
    expire_in: 60 mins
    paths:
      - target/*.jar

deploy:
  stage: deploy
  script:
    - 'echo deploy_caas is not set yet'
  only:
    - develop
  image: $(registryIP)/gitlab-runner/maven:3-jdk-8
  services:
  - name: $(registryIP)/gitlab-runner/mysql
    alias: mysql
  - name: $(registryIP)/gitlab-runner/redis
    alias: redis
  dependencies:
    - compile

gitlab同时可以使用k8s和docker等多种runner

你可能感兴趣的:(#,gitlab,devops,#,docker,gitlab,cicd,docker)