实验条件2个服务器,
git服务器 ip地址:192.168.200.151
Jenkins服务器 ip地址:192.168.200.132
操作系统:centos7.5
[root@git ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) [root@git ~]# uname -r 3.10.0-862.el7.x86_64 [root@git ~]# systemctl stop firewalld [root@git ~]# systemctl disable firewalld [root@git ~]# systemctl stop NetworkManager [root@git ~]# systemctl disable NetworkManager [root@git ~]#
在git服务器上操作
[root@git ~]# yum -y install git
[root@git ~]# useradd git [root@git ~]# echo "12341234" | passwd --stdin git Changing password for user git. passwd: all authentication tokens updated successfully.
用git账号创建项目仓库
[root@git ~]# su - git [git@git ~]$ mkdir repos [git@git ~]$ cd repos/ [git@git repos]$ mkdir app.git [git@git repos]$ cd app.git/ [git@git app.git]$ pwd /home/git/repos/app.git [git@git app.git]$ git --bare init Initialized empty Git repository in /home/git/repos/app.git/ [git@git app.git]$ ls branches config description HEAD hooks info objects refs [git@git app.git]$
在Jenkins服务器上测试推送代码
[root@jenkins ~]# yum -y install git
[root@jenkins ~]# mkdir -p /test
[root@jenkins ~]# cd /test
[root@jenkins test]# git clone git@192.168.200.151:/home/git/repos/app.git
Cloning into 'app'...
The authenticity of host '192.168.200.151 (192.168.200.151)' can't be established.
ECDSA key fingerprint is SHA256:gm/RhqGrfDo5Rgcr/LmBAaqPv6tmni7cRpXjGEWZQpg.
ECDSA key fingerprint is MD5:ae:f6:0b:6e:80:96:67:cf:bd:e8:f5:b5:c4:e0:da:11.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.200.151' (ECDSA) to the list of known hosts.
git@192.168.200.151's password:
warning: You appear to have cloned an empty repository.
[root@jenkins test]# ls
app
[root@jenkins test]# cd app
[root@jenkins app]# touch test.sh
[root@jenkins app]# echo "hello" >> test.sh
[root@jenkins app]# git add *
[root@jenkins app]# git commit -m "first"
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'root@jenkins.(none)')
[root@jenkins app]# git config --global user.email "493115250@qq.com"
[root@jenkins app]# git config --global user.name "cash_su"
[root@jenkins app]# git commit -m "first"
[master (root-commit) 731ab6e] first
1 file changed, 1 insertion(+)
create mode 100644 test.sh
[root@jenkins app]# git push -u origin master
git@192.168.200.151's password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 207 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.200.151:/home/git/repos/app.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
给git做一个免秘钥
[root@jenkins app]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:toacG+JZEW1VNmQKQcp9slNyCHCaq55K9Ym82EZcboU root@jenkins The key's randomart image is: +---[RSA 2048]----+ | ..o.+..o* | | = = + + . | | o = B = | | E + B | | ..+ o S | | o+o+.= o | | .oooo* o | |..o+.+ + | |.o+oo . | +----[SHA256]-----+ [root@jenkins app]# [root@jenkins app]# [root@jenkins app]# ssh-copy-id -i ~/.ssh/id_rsa.pub git@192.168.200.151 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys git@192.168.200.151's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'git@192.168.200.151'" and check to make sure that only the key(s) you wanted were added. [root@jenkins app]# ssh git@192.168.200.151 Last login: Sun Dec 23 13:28:56 2018 [git@git ~]$ exit
准备安装Jenkins
Jenkins官网:https://jenkins.io/ redhat版Jenkins官网下载页面:https://pkg.jenkins.io/redhat-stable/ 下载了rpm包以后通过localinstall直接安装 yum -y localinstall 安装包名
利用yum直接安装Jenkins
[root@jenkins app]# wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
#导入证书秘钥
[root@jenkins app]# rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
[root@jenkins app]# yum -y install jenkins
#用命令查看安装路径
[root@jenkins app]# rpm -ql jenkins
/etc/init.d/jenkins
/etc/logrotate.d/jenkins
/etc/sysconfig/jenkins
/usr/lib/jenkins
/usr/lib/jenkins/jenkins.war
/usr/sbin/rcjenkins
/var/cache/jenkins
/var/lib/jenkins
/var/log/jenkins
[root@jenkins app]#
安装java
[root@jenkins ~]# tar xf jdk-8u171-linux-x64.tar.gz -C /usr/local/ [root@jenkins ~]# cd /usr/local/ [root@jenkins local]# mv jdk1.8.0_171/ jdk [root@jenkins local]# vim /etc/profile [root@jenkins local]# tail -3 /etc/profile export JAVA_HOME=/usr/local/jdk/ export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
[root@jenkins local]# source /etc/profile
[root@jenkins local]# java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
[root@jenkins local]#
安装maven
[root@jenkins ~]# tar xf apache-maven-3.5.0-bin.tar.gz -C /usr/local/ [root@jenkins ~]# cd /usr/local/ [root@jenkins local]# mv apache-maven-3.5.0/ maven [root@jenkins local]# vim /etc/profile [root@jenkins local]# tail -2 /etc/profile MAVEN_HOME=/usr/local/maven export PATH=${MAVEN_HOME}/bin:$PATH
[root@jenkins local]# source /etc/profile
[root@jenkins local]# mvn -v
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00)
Maven home: /usr/local/maven
Java version: 1.8.0_171, vendor: Oracle Corporation
Java home: /usr/local/jdk/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-862.el7.x86_64", arch: "amd64", family: "unix"
[root@jenkins local]#
启动jenkins
[root@jenkins ~]# ln -s /usr/local/jdk/bin/java /usr/bin/ [root@jenkins ~]# systemctl start jenkins [root@jenkins ~]# ss -antup | grep 8080 tcp LISTEN 0 50 :::8080 :::* users:(("java",pid=5642,fd=162)) [root@jenkins ~]#
添加到开机自启
[root@jenkins ~]# systemctl enable jenkins jenkins.service is not a native service, redirecting to /sbin/chkconfig. Executing /sbin/chkconfig jenkins on [root@jenkins ~]# echo $? 0
开始使用Jenkins
查看解锁码
[root@jenkins ~]# cat /var/lib/jenkins/secrets/initialAdminPassword 6c2b251fde9348e1ba5f3ecd968f0917 [root@jenkins ~]#
使用默认的插件即可
权限管理
开发人员用的 生产环境项目代码版本发布(A/B测试等)
测试人员用的 测试环境自动部署
运维人员用的 生产环境项目代码版本回滚
安装控制角色的插件
测试创建2个用户,一个开发,一个测试的
同样主从user2测试的账号密码
最终的权限选项
创建2个项目来实验权限,空项目即可
超户视角
测试视角
开发视角
在超户上构建两个项目的视图
超户视角
开发视角
测试视角
测试开发的构建功能
超户改变配置
构建成功
带参数的构建(在超户里修改配置)
在开发视角开始构建
构建成功
安装插件Extended可以让用户自己选分支
在超户修改配置,进行多项选择实验
我们可以将参数写到文件里,让其引用这个文件,就可以有参数了
[root@jenkins ~]# vim /opt/jenkins.property [root@jenkins ~]# cat /opt/jenkins.property ok=can1.can2,can3
用git参数化构建
安装插件
添加私钥到全局凭据
[root@jenkins ~]# cat ~/.ssh/id_rsa -----BEGIN RSA PRIVATE KEY----- MIIEpQIBAAKCAQEA11fzf+Ur3ENQRFazuqNn3WgSca9s9b4mT71WYjSx0OlwRQKW cAht2NAhR7ZYWFcu8kXJiPYNiuNWvNzRaPRQLNdELrplI4mJlznjUYlogVKlJTEi vXlWnI/sCnwOADAYMeMCB+vvMuHs+XAzShoBPa7spZ2zOsUEgkdQ9b/nnRdI6rgz A/V0xMOoChXQviis2fdUgxt5se6A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wRuqVxHsoNZ1ZrsUhPhQsI0ezK3lEsse//oWjsNkv/9mkOjJ Ycr1Dun5/DHJO25s/BAmiqB3QLBuOYVHYw7LyxR7PkdTUpAbIGxTxfF1DrLtaaur hI101q0CgYEA8IQKMINCF4SuJ+TSAtzav0WKBqayOWi1mdVoOZl7MoqxjUywFXw5 V4oKxxty3tWvTa5MR6R0jfY0Om92LWXKmicZWnq6pZC/26H3ZuuMwO1WlGE5OBmo ZpuYlD1okBWUhVSzKmzLp6MzCpUgUbu2ffuSjeaSv6bVEYdS0l/uX9Q= -----END RSA PRIVATE KEY----- [root@jenkins ~]#
修改配置文件
节点主从构建,
创建全局凭证,
测试用新的节点构建
在git服务器上查看Jenkins工作的目录有没有拉取下来
[root@git A-web]# pwd /var/lib/jenkins/workspace/A-web [root@git A-web]# ls test.sh [root@git A-web]# cat test.sh hello [root@git A-web]#