一、docker安装

  • 拉取镜像 docker pull jenkins/jenkins:lts # 拉取稳定版镜像
  • 使用docker-file创建容器
    version: "3"
    services:
      jenkins:
        image: 'jenkins/jenkins:lts'
        restart: on-failure:3
        working_dir: '/var/jenkins_home'
        user: root
        ports:
          - '127.0.0.1:8100:8080'
          #- '50000:50000'
        volumes:
          - '/data/jenkins/jenkins_home:/var/jenkins_home'
          - '/data/jenkins/ssh_key:/root/.ssh'
          - '/etc/localtime:/etc/localtime:ro'
          - '/etc/timezone:/etc/timezone' #时区与主机同步,解决Jenkins时间不对问题
        extra_hosts:
          - "git.abc.com:172.16.1.1" #写入特定的host地址
      ##项目启动命令##
      # docker-compose -f docker-jenkins-compose.yml  -p jenkins up  -d
    
      ##项目停止命令##
      # docker-compose -f docker-jenkins-compose.yml  -p jenkins down
    
  • docker file对应的run命令
    docker run -u root --name jenkins -d \
    -p 127.0.0.1:8100:8080 \
    -v /data/jenkins/jenkins_home:/var/jenkins_home \
    -v /data/jenkins/ssh_key:/root/.ssh \
    -v /etc/localtime:/etc/localtime:ro \
    -v /etc/timezone:/etc/timezone \
    --restart=on-failure:3 \
    jenkins/jenkins:lts  # 创建容器
    

二、配置

  • nginx 配置

    # nginx反向代理jenkins
    server
    {
        listen 80;
        server_name jenkins.abc.com;
        location / {
            proxy_pass http://127.0.0.1:8100;
            proxy_read_timeout  90;
            proxy_set_header X-Forwarded-Host $host:$server_port;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Real-IP $remote_addr;  
        }
        access_log  logs/jenkins_access.log;
        error_log   logs/jenkins._error.log;
    }
    
  • 查看jenkins初始化密码

    # 浏览器访问http://jenkins.abc.com/进入首页
    docker log jenkins # 查看第一次进入的初始化密码
    cat /home/jenkins/secrets/initialAdminPassword
    
  • Jenkins源替换

    管理Jenkins–> 插件管理 –> 高级 –> 替换升级站点url为: https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

  • 常用插件安装

    Localization: Chinese (Simplified) (中文语言包插件)、Git、Git client、Git Parameter Plug-In (git tag列表插件)、Job Configuration History Plugin (配置修改历史记录插件)、Matrix Authorization Strategy Plugin (基于项目的权限控制插件)

  • 钉钉机器人配置

    • 创建钉钉机器人

      点击查看钉钉机器人配置文档

    • 构建脚本增加执行本地shell,脚本如下:

      text="构建失败信息"
      curl 'https://oapi.dingtalk.com/robot/send?access_token=自己的access_token' \
      -H 'Content-Type: application/json' \
      -d "{\"msgtype\": \"link\", 
              \"link\": {
                  \"title\": \"${JOB_NAME}${BUILD_DISPLAY_NAME}构建失败\",
                  \"text\": \"我就是我, 是不一样的烟火\",
                  \"messageUrl\": \"http://localhost:1313/images/me/    avatar.jpeg\",
                  \"picUrl\": \"http://localhost:1313/images/me/    avatar.jpeg\"
              }
          }"