头图

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


七、参考博客



阅读 215
1 声望
0 粉丝
0 条评论
1 声望
0 粉丝
文章目录
宣传栏