Skip to content

简介

https://www.jenkins.io/

https://github.com/jenkinsci/jenkins

https://www.jenkins.io/zh/doc/tutorials/

开始使用

要求

  • 256 MB 内存,建议大于 512 MB
  • 10 GB 硬盘空间(用于 Jenkins 和 Docker 镜像)

从 jenkins release(https://github.com/jenkinsci/jenkins/releases) 中下载 jenkins.war(例如 https://github.com/jenkinsci/jenkins/releases/download/jenkins-2.396/jenkins.war)

这里使用的 huawei 弹性云服务器 ECS,允许 TCP 2333-8080 端口访问,1 核 2 GB 内存,40 GB 硬盘, Ubuntu 18.04 操作系统

安装 Java:

1
2
3
4
5
6
7
8
# 安装 OpenJDK 11
$ sudo apt update
$ sudo apt install openjdk-11-jdk -y  

$ java -version
openjdk version "11.0.18" 2023-01-17
OpenJDK Runtime Environment (build 11.0.18+10-post-Ubuntu-0ubuntu118.04.1)
OpenJDK 64-Bit Server VM (build 11.0.18+10-post-Ubuntu-0ubuntu118.04.1, mixed mode, sharing)

复制安装好的 jenkins.war 到云服务器上:

1
scp ~/Downloads/jenkins.war huawei:/root

运行 Jenkins:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 在 tmux 中运行
$ java -jar jenkins.war --httpPort=8080

Running from: /root/jenkins.war
webroot: /root/.jenkins/war
2023-03-23 06:34:52.130+0000 [id=1] INFO    winstone.Logger#logInternal: Beginning extraction from war file
2023-03-23 06:34:52.261+0000 [id=1] WARNING o.e.j.s.handler.ContextHandler#setContextPath: Empty contextPath
2023-03-23 06:34:52.395+0000 [id=1] INFO    org.eclipse.jetty.server.Server#doStart: jetty-10.0.13; built: 2022-12-07T20:13:20.134Z; git: 1c2636ea05c0ca8de1ffd6ca7f3a98ac084c766d; jvm 11.0.18+10-post-Ubuntu-0ubuntu118.04.1
2023-03-23 06:34:53.135+0000 [id=1] INFO    o.e.j.w.StandardDescriptorProcessor#visitServlet: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet
2023-03-23 06:34:53.282+0000 [id=1] INFO    o.e.j.s.s.DefaultSessionIdManager#doStart: Session workerName=node0
2023-03-23 06:34:54.290+0000 [id=1] INFO    hudson.WebAppMain#contextInitialized: Jenkins home directory: /root/.jenkins found at: $user.home/.jenkins
2023-03-23 06:34:55.665+0000 [id=1] INFO    o.e.j.s.handler.ContextHandler#doStart: Started w.@4ced35ed{Jenkins v2.396,/,file:///root/.jenkins/war/,AVAILABLE}{/root/.jenkins/war}
2023-03-23 06:34:55.697+0000 [id=1] INFO    o.e.j.server.AbstractConnector#doStart: Started ServerConnector@4206a205{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
2023-03-23 06:34:55.740+0000 [id=1] INFO    org.eclipse.jetty.server.Server#doStart: Started Server@4b168fa9{STARTING}[10.0.13,sto=0] @4690ms
2023-03-23 06:34:55.754+0000 [id=23]    INFO    winstone.Logger#logInternal: Winstone Servlet Engine running: controlPort=disabled
2023-03-23 06:34:56.273+0000 [id=29]    INFO    jenkins.InitReactorRunner$1#onAttained: Started initialization
2023-03-23 06:34:56.305+0000 [id=28]    INFO    jenkins.InitReactorRunner$1#onAttained: Listed all plugins
2023-03-23 06:34:58.080+0000 [id=29]    INFO    jenkins.InitReactorRunner$1#onAttained: Prepared all plugins
2023-03-23 06:34:58.085+0000 [id=29]    INFO    jenkins.InitReactorRunner$1#onAttained: Started all plugins
2023-03-23 06:34:58.119+0000 [id=28]    INFO    jenkins.InitReactorRunner$1#onAttained: Augmented all extensions
2023-03-23 06:34:58.538+0000 [id=29]    INFO    jenkins.InitReactorRunner$1#onAttained: System config loaded
2023-03-23 06:34:58.538+0000 [id=29]    INFO    jenkins.InitReactorRunner$1#onAttained: System config adapted
2023-03-23 06:34:58.539+0000 [id=29]    INFO    jenkins.InitReactorRunner$1#onAttained: Loaded all jobs
2023-03-23 06:34:58.540+0000 [id=29]    INFO    jenkins.InitReactorRunner$1#onAttained: Configuration for all jobs updated
2023-03-23 06:34:58.650+0000 [id=42]    INFO    hudson.util.Retrier#start: Attempt #1 to do the action check updates server
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1 (file:/root/.jenkins/war/WEB-INF/lib/groovy-all-2.4.21.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int)
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.vmplugin.v7.Java7$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2023-03-23 06:34:59.738+0000 [id=28]    INFO    jenkins.install.SetupWizard#init:

*************************************************************
*************************************************************
*************************************************************

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

0224ae0dc0a943c2be9923ec678a3c54

This may also be found at: /root/.jenkins/secrets/initialAdminPassword

*************************************************************
*************************************************************
*************************************************************

访问 服务器公网地址:8080

需要管理员密码登录, 复制 log 里面的密码登录(0224ae0dc0a943c2be9923ec678a3c54)即可

  • 自定义 Jenkins: 安装推荐的插件(Folders, Timestamper, Pipeline, Git, LDAP)
  • 创建第一个管理员用户: nocilantro, Nocilantro233666, 不要香菜, 954241552@qq.com
  • Jenkins URL: http://服务器公网地址:8080/
  • 开始使用

centos 安装

1
2
yum install -y java-11-openjdk
java --version
1
2
scp ~/Downloads/jenkins.war buyaoxiangcai:
/root
1
2
3
4
5
6
7
8
yum install -y tmux   
tmux new -s jenkins
mkdir jenkins
cd jenkins
mv ../jenkins.war .
java -jar jenkins.war --httpPort=8080

# 51cb61e861b4413fab5c28e215c5e290

hello world pipeline

https://www.jenkins.io/zh/doc/pipeline/tour/hello-world/

在一个 github 项目(不能是私密项目)根目录下新建文件 Jenkinsfile 并 push:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'echo "Hello World"'
                sh '''
                    echo "Multiline shell steps works too"
                    ls -lah
                '''
            }
        }
    }
}
  • 点击 "新建Item"
  • 为新工程起一个名字(例如 Hello Pipeline), 选择 MultiBranch Pipeline(多分支流水线)
  • 点击 Add Source(增加源) 按钮,选择 github 类型并填写详细信息(填写 github 仓库地址后验证一下是否成功)
  • 点击 Save(保存) 按钮,观察第一个 Pipeline 运行

在配置好 Pipeline 之后,Jenkins 会自动检测您仓库中创建的任何新的分支或合并请求, 并开始为它们运行 Pipelines。

jenkins 所在目录: /root/.jenkins

github token

github 生成 Personal Access Token:

github -> 右上角头像 -> Settings -> Developer settings -> Personal access tokens -> Generate new token

注意,生成 token 之后一定要记录下来,因为只显示一次

系统管理 -> 系统配置 -> Github -> 添加 Github 服务器,名称 github-work, API URL: https://api.github.com -> 添加凭据 -> 类型 secret text -> Secret: token -> 连接测试: Credentials verified for user YangzhenZhao, rate limit: 4998

给 Github 项目设置 webhooks(必须有)

私有的项目必须的

jenkins 主页 -> 系统管理 -> 系统配置 -> GitHub -> 高级 -> 覆盖 Hook URL 为 Github 指定另外一个 Hook URL(打上对勾) -> http://服务器公网地址:端口号/github-webhook/ -> 保存

github 上打开项目主页 -> Settings -> Webhooks -> Add webhook -> 在 Payload URL 中设置 Jenkins 的 hook URL -> 勾选 Let me select individual events -> 勾选 Pull Requests 复选框 -> 点击 Add webhook -> 添加完成后如果前面有 ✅ 说明连接是通的,配置成功

Jenkins 添加 SSH 密钥(貌似没用)

系统管理 -> Manage Credentials -> 添加凭据

New credentials:
类型: SSH Username with private key
范围: 全局(jenkins, node, items, all child items, etc)
Username: gh-deploy
Private Key: ~/.sssh/id_rsa
Create

Jenkins 任务配置 -> Git Credentials -> 选择 gh-deploy -> 保存

对 Github 私有项目构建任务(如果部署在 jenkins 这个服务器上)

访问 github 报错提示: ssh-keygen -f "/root/.ssh/known_hosts" -R "github.com",那就执行一次好了

首先确保项目添加了 github 服务器,配置了 Jenkins webhook

  • 新建任务
  • 输入任务名称
  • 构建一个自由风格的软件项目
  • 勾选 GitHub 项目,填写 URL,例如 https://github.com/YangzhenZhao/workspace/
  • 源码管理选择 Git, Repo URL,例如: git@github.com:YangzhenZhao/workspace.git, Credentials 不用选
  • 构建触发器: 勾选 GitHub hook trigger for GITScm polling
  • build Steps: 执行 shell,可以用来执行项目中的脚本,例如项目中有个 deploy.sh,可以在执行 Sheel 命令中执行 sh deploy.sh

Publish Over SSH 插件

感觉不是很好用,需要复制文件到远程服务器,如果文件较大的话,耗时很久

https://plugins.jenkins.io/publish-over-ssh/

Install without restart -> 安装完成后重启 Jenkins(空闲时)

将 jenkins 服务器的私钥 ~/.ssh/id_rsa,放到 Jenkins SSH Key 中(Begin 和 End 这两行也要有)

1
2
3
-----BEGIN RSA PRIVATE KEY-----
xxxxx
-----END RSA PRIVATE KEY-----

将 jenkins 服务器的 ~/.ssh/id_rsa.pub 添加到目标服务器的 ~/.ssh/authorized_keys

系统管理 -> 系统配置 -> SSH Servers -> 新增 -> 设置 SSH Server 的 Name,Hostname, Username, Remote Directory /home/ubuntu -> Test config -> 成功后保存

1
2
3
4
5
6
7
8
9
// 不可用,需要复制文件,耗时太多
- 打开项目
- 配置      
- 高级 Verbose output in console 上打对勾    
- Build Steps: 执行 Shell `sh pack.sh`     
- 构建环境: Send files or execute commands over SSH after the build runs, 填写 Source files, 这里的根目录是项目目录所以填写文件名就好 `nocilantro.tar.gz`把项目所有文件的压缩包复制过去, Remote directory, 因为之前的 Server Remote Director  `/home/ubuntu`, 所以这里是根据这个目录的相对目录, 例如 `/home/ubuntu/remoteNocilantro` 的话就设置为 `remoteNocilantro`    
- 构建环境: Send files or execute commands over SSH before the build starts 打对勾填写 Source files, 这里的根目录是项目目录所以填写 `*/**`把项目所有文件复制过去, Remote directory, 因为之前的 Server Remote Director  `/home/ubuntu`, 所以这里是根据这个目录的相对目录, 例如 `/home/ubuntu/remoteNocilantro` 的话就设置为 `remoteNocilantro`        
- Build Steps: Send files or execute commands over SSH -> SSH Server 选择刚才新建的 SSH Server  Name, 设置 Exec command     
- 保存   
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
系统管理 -> 系统配置 -> SSH Servers -> 设置 SSH Server  Remote Directory `/` -> Test config -> 成功后保存   

// 去远程服务器 pull 最新代码 deploy   
- 将代码 clone 到远程服务器, 例如项目为 nocilantro   
- 打开项目
- 配置      
-    
- Build Steps: Send files or execute commands over SSH, 高级 Verbose output in console 上打对勾 , Exec command: `cd /home/ubuntu/nocilantro`, `git pull origin master`, `sh deploy.sh`  
- 高级, Exec Timeout(ms) 设置为 600000ms(10min)   
- 保存   

书籍

《Jenkins 2.x 实践指南》翟志军 2019-04-01