Jenkins+github 前端自动化部署

安装

从Jenkins官网下载最新war文件

运行 java -jar jenkins.war

运行成功会在用户个人目录下新建.jenkins/文件夹
访问localhost:8080就能到达Jenkins首页

clipboard.png
第一次需要从文件夹下获取生成的密码登录,复制粘贴

clipboard.png
密码验证成功后进入页面,选择插件模式

clipboard.png
我选择是第一个-建议安装的插件

clipboard.png
接下来就是漫长的等待安装时间。
插件都安装完成之后进入用户登录界面,设定用户名、密码及邮箱。

clipboard.png
注意:以后的登录名就是 admin 密码就是从secrets目录文件下复制的密码。
clipboard.png

配置项目的 jenkins job
1、在 jenkins 左边栏点击 “新建”, 输入 job 名称,选择 “构建一个自由风格的软件项目” 一项。点击 “OK”

clipboard.png
2、进入 job 配置页面,点击 “General” 选项,配置名称和描述,我们用的github项目,那么勾选github并且填入项目地址

clipboard.png
3、点击 “源码管理” 选项,配置项目的 git 仓库地址的需要构建的分支信息

clipboard.png
4、点击 “构建触发器” 选项,配置 job 构建时机,勾选 “GitHub hook trigger for GITScm polling”,github hook 来触发 job 构建
clipboard.png

5、到github 的该项目下配置hook

clipboard.png

6、点击 “构建” 选项,再点击 “增加构建步骤”, 选择 “Execute shell”,配置构建命令。 如下,这里配置了 cnpm install、npm run unit、npm run build, 分别做安装依赖、单元测试、编译打包三件事。

clipboard.png
clipboard.png

若为windows服务器

7、点击 “构建后操作” 选项,添加两个构建后操作步骤:

“E-mail Notification”,配置构建失败的邮件通知人;
“Send build artifacts over ssh”, 执行预先写好的远程服务器的部署脚本

clipboard.png
邮件配置好了,但是却没找到‘Send build artifacts over ssh’原因是我们没有安装插件,到系统管理里安装好插件,如下:

clipboard.png

执行后报错,邮件无法发送,没有配置SMTP

clipboard.png

clipboard.png
配置如下:

clipboard.png

clipboard.png
填写好后点击test 测试是否收到邮件。

报错 找不到 Jenkins.sh
没有在github上配置jenkins的访问权限
https://github.com/settings/t...

clipboard.png

clipboard.png
复制生成的key,回到Jenkins点击Add按钮

clipboard.png

clipboard.png

点击test connection测试token,

clipboard.png
显示Credentials verified for user xxx, rate limit: xxxx,说明配置完成了,这样你的Jenkins就具有访问你的github的权限了。

添加构建后操作,将代码部署到服务器
配置jenkins服务器 .ssh
Terminal Server@DESKTOP-41JPNF2 MINGW64 ~
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Terminal Server/.ssh/id_rsa):
/c/Users/Terminal Server/.ssh/id_rsa already exists.
Overwrite (y/n)?
Terminal Server@DESKTOP-41JPNF2 MINGW64 ~
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Terminal Server/.ssh/id_rsa):
/c/Users/Terminal Server/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Terminal Server/.ssh/id_rsa.
Your public key has been saved in /c/Users/Terminal Server/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:u0Ega2OlOKv3lXidUcjIIufGOBoQXmccihAQSQT+6IA Terminal Server@DESKTOP-41JPN F2
The key's randomart image is:
+---[RSA 2048]----+
|%* ..+. |
|=.o +o o . |
|.+..+ = o . |
|o oB * . . |
|E.=.X S |
|oo B o + + |
|... . + = |
| .. o o |
|.. .. . |
+----[SHA256]-----+
Terminal Server@DESKTOP-41JPNF2 MINGW64 ~
$ ssh-copy-id web@121.196.197.140
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/c/Users/Terminal Server/.ssh/id_rsa.pub"
The authenticity of host '121.196.197.140 (121.196.197.140)' can't be establishe d.
ECDSA key fingerprint is SHA256:Qr335uKh2MTqQKveq/voVYB9k+2AH9JsyZOvfcNAboM.
Are you sure you want to continue connecting (yes/no)? yes
/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 prompt ed now it is to install the new keys
web@121.196.197.140's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'web@121.196.197.140'"
and check to make sure that only the key(s) you wanted were added.

clipboard.png

clipboard.png

clipboard.png

阅读 10.2k

推荐阅读