乐趣区

关于devops:Docker安装jenkins

Docker装置jenkins

一、环境筹备

1. 宿主机筹备
   宿主机装置 JDK\MAVEN\GIT\DOCKER\NPM
2. 镜像筹备
   jenkins/jenkins:latest-jdk8

二、jenkins部署

1. 镜像下载

docker pull jenkins/jenkins:latest-jdk8
docker images | grep jenkins

2. 查看镜像版本

docker inspect 44f8e2d8566c

3. 启动脚本

restart.sh

  • 挂载 docker 目录,容器里能够执行 docker 命令
  • 改用 jenkins/jenkins:latest-jdk8 版本镜像,能够装置gitlab hook
  • 挂载 maven|npm 等目录,jdk默认是:jdk8。否则容器内无奈应用构建命令
  • maven要做好私服配置,仓库治理等
#!/bin/bash
  
docker stop jenkins && docker rm jenkins

docker run -d --name jenkins \
    -u root \
    --privileged=true \
    --restart=always  \
    -p 8080:8080 -p 5000:5000 \
    -v /usr/local/jenkins:/var/jenkins_home \
    -v /toony/server/apache-maven-3.6.3:/usr/local/maven \
    -v /usr/bin/docker:/usr/bin/docker \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v /etc/localtime:/etc/localtime \
    -v /root/.ssh:/root/.ssh \
    jenkins/jenkins:latest-jdk8
    
#---- jenkinsci/blueocean 默认应用 jdk11 版本,装置 gitlab hook 失败 ------
   
#!/bin/bash
  
docker stop jenkins && docker rm jenkins

docker run -d --name jenkins \
        -u root \
        -p 8080:8080 -p 5000:5000 \
        -v /toony/jenkins/jenkins_home:/var/jenkins_home \
        -v /usr/local/java/jdk1.8.0_171:/var/jenkins_home/jdk8 \
        -v /toony/server/apache-maven-3.6.3:/var/jenkins_home/maven \
        -v /toony/jenkins/jenkinsci:/usr/jenkinsci \
        -v /usr/bin/docker:/usr/bin/docker \
        -v /var/run/docker.sock:/var/run/docker.sock \
        jenkinsci/blueocean

docker-compose.yml

cat > docker-compose.yml <<-EOF
version: '3'
services:                                     
  docker_jenkins:
    user: root                                
    restart: always                           
    image: jenkins/jenkins:lts                
    container_name: jenkins                   
    ports:                                    
      - 8080:8080
      - 5000:5000
    volumes:                                   
      - /toony/jenkins/jenkins_home:/var/jenkins_home 
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/bin/docker:/usr/bin/docker               
      - /usr/local/bin/docker-compose:/usr/local/bin/docker-compose
  docker_nginx:
    restart: always
    image: nginx
    container_name: nginx
    ports:
      - 8090:80
      - 80:80
      - 433:433
    volumes:
      - /toony/nginx/conf.d/:/etc/nginx/conf.d
      - /toony/webserver/static/jenkins/dist/dist:/usr/share/nginx/html
EOF      

4. 配置插件镜像减速

  • url https://updates.jenkins.io/update-center.json
  • 批改为 清华大学官网镜像:https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
cd /usr/local/jenkins/
vim hudson.model.UpdateCenter.xml

5. 拜访

http://http://xxx.61.41.102:8080

  • 用户明码:admin
cat /usr/local/jenkins/secrets/initialAdminPassword
或
docker exec -it jenkins bash cat /var/jenkins_home/secrets/initialAdminPassword

6. 插件装置

Jenkins -> Manage Jenkins -> System Configuration -> Manage Plugins -> Avaliable

Maven Integration
GitLab
GitLab Hook
SaltStack
Publish Over SSH 
Git Parameter    
Build With Parameters
Ansible
Build Pipeline
Parameterized Trigger
Parameterized Remote Trigger
Localization: Chinese (Simplified)


三、jenkins配置

1. 全局配置

Jenkins -> Manage Jenkins -> System Configuration -> Global Tool Configuration

  • jdk | maven | git | npm

2. 配置拜访gitlab

Jenkins -> Manage Jenkins -> Security -> Manage Credentials -> Stores scoped to Jenkins -> global -> Add Credentials -> Username 或者 SSH

3. 配置Push SSH Server

Jenkins -> Manage Jenkins -> System Configuration -> Publish over SSH -> SSH Servers -> Add -> Name|Hostname|Username|Remote Directory -> Use password authentication, or use a different key


四、配置 gitlab 触发webhook

1. gitlabz 设置 root 用户能够通过 SSH 形式拉取代码
2. Jenkins 装置好插件:Credentials Plugin|Gitlab Plugin|SSH Plugin|GitLab Hook
3. jenkins 增加 gitlab 的 root 用户私钥,以便前期拉取代码
4. jenkins 我的项目构建中减少触发器:Build Triggers -> Build when a change is pushed to GitLab -> Generate
   保留:webhook URL 和 `secret token`
5. gitlab 我的项目中:Settings -> Webhooks 增加 URL 和 Secret token
  • 减少触发器
  • 生成secret token

五、装置中文插件

1. 装置中文插件

Localization: Chinese (Simplified)
Locale plugin

2. 全局配置

Jenkins -> Manage Jenkins -> System Configuration -> Local -> Default Language : zh_CN 勾选 Ignore browser ...


六、集成SonarQube

1. 装置插件

SonarQube Scanner for Jenkins

2.SonarQube创立 Jenkins 登录token

3.Jenkins配置拜访 SonarQube 凭据

Jenkins -> Manage Jenkins -> Manage Credentials -> Stores scoped to Jenkins -> global -> Add Credentials -> Kind -> Secret text -> 填入token

4.Jenkins系统配置

Jenkins -> Manage Jenkins -> System Configuration -> SonarQube servers -> Name、Server URL、Server authentication token

5.Jenkins全局配置

Jenkins -> Manage Jenkins -> Global Tool Configuration -> 配置 SonarQube Scanner

6. 我的项目中配置

阐明

sonar.projectKey=test_01                    # SonarQube 中我的项目的惟一标识符。只有它是惟一的                
sonar.projectName=test_server               # SonarQube 中我的项目显示的名称
sonar.version=0.0.1-SNAPSHOT                # 版本号
sonar.sources=./src                         # 源码目录
sonar.java.binaries=./target/classes        # 编译后的文件目录
# sonar.exclusions=**/*.xml,**/*.html       #排除不须要检测的文件,如:xml、html              
sonar.language=java                         # 编程语言
sonar.sourceEncoding=UTF-8
sonar.scm.disabled=true


七、参考博客

  • jenkins jenkinsci/blueocean 应用
  • docker 装置 jenkinsci/blueocean 并且创立 pipeline 我的项目
  • docker 装置配置 jenkins, 并部署到其余服务器
  • 应用 docker 部署 jenkins(jdk11)
  • Centos 7.8 下部署 Jenkins Blue Ocean-Docker 版
  • docker 装置 Jenkins、jenkins 插件、映射 JDK 和 maven 和 RSA、publish over ssh 公布
  • Docker 手把手部署 jenkins 教程,jenkins 容器带 jdk,maven,docker,git
  • Jenkins 的装置及中文展现、装置插件创立

  • ★★ 配置 Jenkins 主动拉取 gitlab 中的代码并部署

  • ★★ sonarQube 部署及 Jenkins 集成 sonarQube
  • jenkins+sonar 继续集成检测代码品质
退出移动版