jenkins到底是什么?
jenkins其实可以看作一个管家,他能帮我们从git 上pull下来代码,通过一些工具进行解析,得到一些可以直接用的代码,这个过程也可称为“持续化集成(CI)”,后面通过SSH等方式部署到服务器上,叫做“持续化部署(CD)”


jenkins下载安装
[root@jenkins ~]wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
[root@jenkins ~]rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
[root@jenkins ~] yum install jenkins
[root@jenkins ~] rpm -qa | grep jenkins
#初始化,得到第一次进入的密码
cat /var/lib/jenkins/secrets/initialAdminPassword
浏览器访问 127.0.0.1:8080 #默认地址和端口
进去后安装推荐插件,注册第一个管理员账户
推荐自己选择安装的插件:
Ansible
Ant Plugin
Blue Ocean
Build Timeout
Email Extension Plugin
Git Parameter Plug-In
Gitlab Hook
GitLab
Maven Invoker
Maven Integration plugin
Publish Over SSH
SSH plugin
SSH Slaves plugin
Rebuilder
Safe Restart
用docker-compose起一个容器来下载部署一个jenkins
version: '3'
services:
jenkins:
image: stephdw/jenkins-ansible
volumes:
- /data/jenkins/:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock
- /usr/bin/docker:/usr/bin/docker
- /usr/lib/x86_64-linux-gnu/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7
ports:
- "8080:8080"
expose:
- "8080"
- "50000"
privileged: true
user: root
restart: always
container_name: jenkins
environment:
JAVA_OPTS: '-Djava.util.logging.config.file=/var/jenkins_home/log.properties'
忘记密码的进入办法

将gitlab与jenkins连接,服务器再与jenkins连接
总体思路:gitlab上要有jenkins的公钥,服务器与jenkins用ssh连接,所以jenkins上传入服务器的私钥,这样,服务器就能获取到gitlab上的代码,jenkins只是充当中间件的作用
用邮件测试一下jenkins
smtp功能
在Manage Jenkins里选择configuration。
Jenkins Location:
Jenkins URL: http://192.168.1.2:8080/(填写访问地址)
System Admin e-mail address: 1@rou.com
E-mail Notification:
SMTP server: smtp.rou.com
Default user e-mail suffix : @rou.com
勾选Use SMTP Authentication
用户名:邮箱地址(与系统管理员的邮箱一致)
密码:授权码
SMTP Port:25(勾选SSL后端口为465)
勾选Test configuration by sending test e-mail
填写自己的邮箱地址
点击test configration(测试)
SSH互信
Publish over SSH:
key:为被服务的机器的私钥
hostname:被服务机器的ip
username:root(使用的远端用户)
Remote Directory:远端的目录
点击test configration(测试是否成功)
构建gitlab项目
在gitlab中:
建立新项目并添加new file: show_system_info.sh
show_system_info.sh的内容:
echo "-----------主机名-------------"
hostname
touch /app/rourou.txt
echo "hello" rourou1.txt
free -h
uptime
复制clone地址
点击new item
输入项目名,选择项目类型,这里选择freestyle item
Description:添加描述
Source Code Management:
选择git:
Repository URL:填写复制的clone地址
Credentials:选择登陆的用户
或者点击ADD:
选择git方式(前提是jenkins以把公钥传给了gitlab)或username and passeord 方式登陆gitlab
选择build:
Name:选择被管理的机器
Source files:(show_system_info.sh)这里填写的是打好包的路径,是相对路径,相对于工作空间目录的,工作空间默认是在 $JENKINS_HOEM/workspace 目录下。每次新建一个构建任务时,会在此目录下创建一个和任务名同名的目录(比如:HelloMaven),在此目录下存放从远程代码库拉取的源代码,并且构建完成的文件(比如jar 包)也会在 HelloMaven/target 目录下。
Remove prefix:是在把打好包的文件传输到远程应用服务器时,不希望被创建的目录名。 这个目录名必须是在 Source files 选项中填写的路径最前面的部分中。
Remote directory:(code)远程应用服务器的目录,这个目录假如没有会被自动创建,但是此目录是给相对路径,相对于之前在系统设置中的 SSH Servers 中配置的服务器端路径
Exec command:(sh /app/code/show_system_info.sh)指构建后在远程应用服务器上执行的命令。
保存后,点击Build now
待构建完成之后,点击console output查看
<pre class="console-output" style="box-sizing: border-box; white-space: pre-wrap; overflow-wrap: break-word; margin: 0px; color: rgb(51, 51, 51); font-size: 13px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Started by an SCM change
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/jenkins projects
using credential 1
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url [http://10.0.122.124:8081/rourou/ll.git](http://10.0.122.124:8081/rourou/ll.git) # timeout=10
Fetching upstream changes from [http://10.0.122.124:8081/rourou/ll.git](http://10.0.122.124:8081/rourou/ll.git)
> git --version # timeout=10
using GIT_ASKPASS to set credentials get system info script
> git fetch --tags --progress [http://10.0.122.124:8081/rourou/ll.git](http://10.0.122.124:8081/rourou/ll.git) +refs/heads/*:refs/remotes/origin/*
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision c86f70e2dc86a336bbbd912c77c6f7b6a9260b6b (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f c86f70e2dc86a336bbbd912c77c6f7b6a9260b6b
Commit message: "Update show_system_info.sh"
> git rev-list --no-walk f395fc2203b5509378ddb7e105a719e176438dfe # timeout=10
SSH: Connecting from host [74a3c2c9ae63]
SSH: Connecting with configuration [webserver1] ...
SSH: EXEC: STDOUT/STDERR from command [sh /app/code/show_system_info.sh] ...
-----------主机名-------------
49a6713a34ee
hello rourou1.txt
total used free shared buff/cache available
Mem: 7.7G 6.0G 182M 762M 1.5G 530M
Swap: 19G 457M 19G
07:06:23 up 7:19, 0 users, load average: 0.52, 0.53, 0.59
SSH: EXEC: completed after 200 ms
SSH: Disconnecting configuration [webserver1] ...
SSH: Transferred 1 file(s)
Build step 'Send files or execute commands over SSH' changed build result to SUCCESS
Finished: SUCCESS</pre>
到被管理的服务器上查看构建的目录
|-- code
| `-- show_system_info.sh
`-- rourou.txt
网友评论