主机名称 | IP | cpu核数/内存/硬盘 | 安装软件 | 用途 |
---|---|---|---|---|
controlnode | 172.16.1.120 | 2/2/60 | git | 代码仓库 |
slavenode1 | 172.16.1.121 | 2/2/60 | jenkins、jdk | jenkins主节点 |
slavenode2 | 172.16.1.122 | 2/2/60 | jdk、maven、tomcat | jenkins从节点2(用于构建和部署java程序) |
slavenode3 | 172.16.1.123 | 2/2/60 | jdk、lnmp环境 | jenkins从节点3(用于部署php程序) |
2 CICD DevOps介绍
3 安装jenkins
在 172.16.1.121 节点上操作
Jenkins官方文档地址:https://www.jenkins.io/download/
3.1 配置jenkins yum源
# wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
# rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
3.2 安装
# yum install jenkins -y
由于是国外的源安装可能会很慢,可以下载阿里云上的 rpm 进行安装
https://mirrors.aliyun.com/jenkins/redhat-stable/jenkins-2.235.2-1.1.noarch.rpm
3.3 配置java环境
# tar -xzf jdk-8u45-linux-x64.tar.gz
# mv jdk1.8.0_45/ /usr/local/jdk
# sed -i.ori '$a export JAVA_HOME=/usr/local/jdk export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar export PATH=$JAVA_HOME/bin:$PATH' /etc/profile
# source /etc/profile
# ln -s /usr/local/jdk/bin/java /usr/bin/java
或
vim /etc/init.d/jenkins
/usr/bin/java => /usr/local/jdk/bin/java
3.4 设置jenkins运行用户为root
# vim /etc/sysconfig/jenkins
JENKINS_USER="root"
3.5 启动jenkins服务
# systemctl start jenkins
# systemctl enable jenkins => chkconfig jenkins on
3.6 配置jenkins
1 在浏览器中通过 http://172.16.1.121:8080/ url进行访问
2 解锁jenkins
# tailf /var/lib/jenkins/secrets/initialAdminPassword
e5599334d1884634a910a0cbf2279b65
4 创建用户和密码
5 配置jenkins url地址
6 jenkins安装完成
7 更新 jenkins 插件地址如下
http://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
或
http://updates.jenkins.io/update-center.json
此时会在/var/lib/jenkins/目录下生成一个updates目录
修改插件下载路径
# cd /var/lib/jenkins/updates/
# sed -i.bak 's/http://updates.jenkins-ci.org/download/http://mirrors.tuna.tsinghua.edu.cn/jenkins/g' default.json
# sed -i.bak 's/http://www.google.com/http://www.baidu.com/g' default.json
# systemctl restart jenkins.service
8 安装插件
参数构建化过程插件:Build With Parameters Persistent Parameter
参数构建插件:Extended Choice Parameter、Git Parameter、邮件插件:Email Extension
权限管理插件:Role-based Authorization Strategy、中文插件:Localization: Chinese (Simplified)
git插件:Git、Maven插件:Maven Integration、jenkins主从代理插件:SSH Build Agents、
工作空间清理:Workspace Cleanup、流水线插件:Pipeline
4 jenkins从节点2安装配置
在 172.16.1.122 节点上操作
安装 unzip,不然后面的pipeline脚本会报错,提示“No zipfiles found.”
# yum install unzip -y
4.1 安装jdk
# tar -xzf jdk-8u45-linux-x64.tar.gz # mv jdk1.8.0_45/ /usr/local/jdk # sed -i.ori '$a export JAVA_HOME=/usr/local/jdk export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar export PATH=$JAVA_HOME/bin:$PATH' /etc/profile # source /etc/profile
4.2 安装maven
# tar -xzf apache-maven-3.5.3-bin.tar.gz # mv apache-maven-3.5.3/ /usr/local/maven # sed -i.ori '$a export MAVEN_HOME=/usr/local/maven export PATH=$MAVEN_HOME/bin:$PATH' /etc/profile # source /etc/profile # mvn -v Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T03:49:05+08:00) Maven home: /usr/local/maven Java version: 1.8.0_45, vendor: Oracle Corporation Java home: /usr/local/jdk/jre Default locale: zh_CN, platform encoding: UTF-8 OS name: "linux", version: "3.10.0-862.el7.x86_64", arch: "amd64", family: "unix"
修改maven构建时所需软件包的下载源 # vim /usr/local/maven/conf/settings.xml +158 <mirror> <id>central</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>https://maven.aliyun.com/repository/public</url> </mirror>
4.3 安装tomcat
# tar -xzf apache-tomcat-8.5.31.tar.gz # mv apache-tomcat-8.5.31/ /usr/local/tomcat # rm -rf /usr/local/tomcat/webapps/* # mkdir -p /usr/local/tomcat/webapps/ROOT # echo "ok" >/usr/local/tomcat/webapps/ROOT/status.html
# /usr/local/tomcat/bin/startup.sh # http://172.16.1.122:8080/status.html
5 jenkins从节点3安装配置
在 172.16.1.123 节点上操作
5.1 安装lnmp环境
# yum install nginx php-mysql php-fpm mariadb-server -y # systemctl start php-fpm.service # systemctl enable php-fpm.service # systemctl start mariadb.service # systemctl enable mariadb.service
增加配置
# 注释掉 /etc/nginx/nginx.conf 中的 server 配置项,然后按如下操作进行 # vim /etc/nginx/conf.d/www.conf server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ .(html|css|js|jpg|png|gif)$ { root /usr/share/nginx/html; index index.html index.htm; } } # nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
# rm -rf /usr/share/nginx/html/* # echo '<?php phpinfo(); ?>' >/usr/share/nginx/html/status.php # systemctl start nginx.service # systemctl enable nginx.service # http://172.16.1.123/status.php
5.2 安装 jdk
# tar -xzf jdk-8u45-linux-x64.tar.gz # mv jdk1.8.0_45/ /usr/local/jdk # sed -i.ori '$a export JAVA_HOME=/usr/local/jdk export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar export PATH=$JAVA_HOME/bin:$PATH' /etc/profile # source /etc/profile
6 配置 jenkins 连接从节点
6.1 为从节点添加凭据
Manage Jenkins—>Manage Credentials—>添加凭据
6.2 连接 jenkins从节点2(172.16.1.122)
Manage Jenkins—>Manage Nodes and Clouds—>新建节点
1 创建代理
2 配置代理相关参数
3 启动代理
4 启动成功
5 在节点上验证
# ps -ef | grep jar root 5844 5798 0 18:44 ? 00:00:00 bash -c cd "/var/lib/jenkins" && /usr/local/jdk/bin/java -jar remoting.jar -workDir /var/lib/jenkins -jar-cache /var/l ib/jenkins/remoting/jarCacheroot 5851 5844 8 18:44 ? 00:00:06 /usr/local/jdk/bin/java -jar remoting.jar -workDir /var/lib/jenkins -jar-cache /var/lib/jenkins/remoting/jarCache root 5964 2184 0 18:45 pts/1 00:00:00 grep --color=auto jar
6.3 连接 jenkins从节点3(172.16.1.123)
7 安装 git
在 172.16.1.120 节点上操作
7.1 创建仓库
1、安装Git # yum install git -y 2、创建git用户并设置密码 # useradd git # echo "123456" | passwd --stdin git 3、创建仓库 # su - git $ mkdir -p repos/java-item.git $ mkdir -p repos/php-item.git $ mkdir -p repos/pipeline-scripts.git $ git init --bare repos/java-item.git/ $ git init --bare repos/php-item.git/ $ git init --bare repos/pipeline-scripts.git/ $ su - root # mkdir -p /tools && cd /tools/
7.2 向git仓库推送数据
1、配置ssh # ssh-keygen # ssh-copy-id -p 22 git@172.16.1.120 # git config --global user.email "you@example.com" # git config --global user.name "Your Name" 2、提交java代码 # git clone git@172.16.1.120:/home/git/repos/java-item.git # 上传maven构建的java源码包,解压后移动到java-item目录下 # cd java-item/ # git add . # git commit -m "all" # git push origin master 3、提交php代码 # git clone git@172.16.1.120:/home/git/repos/php-item.git # 上传wordpress的源码包,解压后移动到php-item目录下 # cd php-item/ # git add . # git commit -m "all" # git push origin master 4、提交pipeline脚本 # git clone git@172.16.1.120:/home/git/repos/pipeline-scripts.git # cd pipeline-scripts # 将Jenkinsfile-java、Jenkinsfile-php脚本上传到pipeline-scripts目录下 # git add . # git commit -m "all" # git push origin master
7.3 Jenkinsfile-java 脚本
node ("tomcat(172.16.1.122)") { //def mvnHome = '/usr/local/maven' stage('git checkout') { checkout([$class: 'GitSCM', branches: [[name: '${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'git@172.16.1.120 :/home/git/repos/java-item.git']]]) } stage('maven build') { sh ''' JAVA_HOME=/usr/local/jdk PATH=$JAVA_HOME/bin:/usr/local/maven/bin:$PATH mvn clean package -Dmaven.test.skip=true ''' } stage('deploy') { sh ''' JENKINS_NODE_COOKIE=dontkillme TOMCAT_NAME=tomcat TOMCAT_HOME=/usr/local/$TOMCAT_NAME WWWROOT=$TOMCAT_HOME/webapps/ROOT BACKUP_DIR=/data/backup [ ! -d $BACKUP_DIR ] && mkdir -p $BACKUP_DIR [ -d $WWWROOT ] && mv $WWWROOT $BACKUP_DIR/${TOMCAT_NAME}-$(date +"%F_%T") unzip ${WORKSPACE}/target/*.war -d $WWWROOT PID=$(ps -ef |grep $TOMCAT_NAME |egrep -v "grep" |awk 'NR==1{print $2}') [ -n "$PID" ] && kill -9 $PID /bin/bash $TOMCAT_HOME/bin/startup.sh ''' } stage('test') { sh ''' sleep 5 curl http://172.16.1.122:8080 ''' } }
7.4 Jenkinsfile-php 脚本
node ("php(172.16.1.123)") { stage('git checkout') { checkout([$class: 'GitSCM', branches: [[name: '${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'git@172.16.1.120 :/home/git/repos/php-item.git']]]) } stage('code copy') { sh ''' WWWROOT=/usr/share/nginx/html BACKUP_DIR=/data/backup [ ! -d $BACKUP_DIR ] && mkdir -p $BACKUP_DIR [ -d $WWWROOT ] && mv $WWWROOT $BACKUP_DIR/PHP-$(date +"%F_%T") rm -rf ${WORKSPACE}/.git cp -a ${WORKSPACE} $WWWROOT chown -R apache.apache $WWWROOT ''' } stage('test') { sh ''' sleep 5 curl http://172.16.1.123/ ''' } }
7.5 所有节点和 git 仓库建立 ssh 密钥验证登陆
分别在 172.16.1.121、172.16.1.122、172.16.1.123 上执行如下命令 # ssh-keygen # ssh-copy-id -p 22 git@172.16.1.120
8 Jenkins+Pipeline+Java
1 创建项目
2 设置构建参数
3 流水线参数配置
4 构建项目
5 构建视图
6 访问网站
9 Jenkins+Pipeline+Maven+PHP
1 创建项目
2 设置构建参数
3 流水线参数配置
4 构建项目
5 构建视图
6 访问网站
10 用户权限设置
1 开启允许用户注册功能和 启用 Role-Based Strategy 插件
Manage Jenkins—>Configure Global Security
2 新建两个项目分别时 A-item、B-item
3 分别注册两个用户userA、userB
4 新建用户角色和项目角色
Manage Jenkins—>Manage and Assign Roles—>Manage Roles
5 将用户赋予用户角色和项目角色上
Manage Jenkins—>Manage and Assign Roles—>Assign Roles
6 userA用户登陆
7 userB用户登陆
11 补充
1 git
git remote -v git remote rm origin git remote add origin git@172.16.1.120:/home/git/repos/pipeline-scripts.git
2 参数化构建
项目构建参数配置
构建项目时可见选项
3 邮箱设置
Manage Jenkins—>Extended E-mail Notification
下面是全局邮箱服务设置,在项目构建完成后也可指定动作发送邮件