陷入jenkins自动化部署的坑

背景

Jenkins官网[2]javascript

Build great things at any scale The leading open source automation server, Jenkins provides hundreds of plugins to support building, deploying and automating any project.css


其是一款开源自动化部署服务器,由java编写,目的是为了持久集成。
html

具体步骤

项目和远程仓库    •将最新的代码推送到远程仓库远程仓库和Jenkins    •定时获取远程仓库上最新的完整项目下载到本地Jenkins与远程服务器    •经过jenkins上传到远程服务器前端





安装

先添加其Debian软件包,而后更新存储库,最后使用存储库apt-get安装jenkinsvue

安装运行环境

安装jdkjava

sudo apt-get install openjdk-8-jdk

安装完成以下node

update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/bin/appletviewer to provide /usr/bin/appletviewer (appletviewer) in auto modeupdate-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/bin/jconsole to provide /usr/bin/jconsole (jconsole) in auto modeProcessing triggers for libc-bin (2.23-0ubuntu10) ...Processing triggers for systemd (229-4ubuntu21.2) ...Processing triggers for ureadahead (0.100.0-19) ...Processing triggers for ca-certificates (20170717~16.04.1) ...Updating certificates in /etc/ssl/certs...0 added, 0 removed; done.Running hooks in /etc/ca-certificates/update.d...
done.done.

存储库密钥添加到系统

wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -

出现ok,添加成功linux

Debian包存储库地址添加到服务器sources.list

echo deb http://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list

添加成功以下git

deb http://pkg.jenkins.io/debian-stable binary/

更新存储库

sudo apt-get update

更新成功以下github

Ign:17 http://pkg.jenkins.io/debian-stable binary/ InRelease Get:18 http://pkg.jenkins.io/debian-stable binary/ Release [2042 B]Get:19 http://pkg.jenkins.io/debian-stable binary/ Release.gpg [181 B]Ign:20 https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 InReleaseHit:21 https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 ReleaseGet:23 http://pkg.jenkins.io/debian-stable binary/ Packages [14.7 kB]Fetched 6813 kB in 3s (2187 kB/s) Reading package lists... Done

安装jenkins

sudo apt-get install jenkins

安装成功以下

perl: warning: Falling back to a fallback locale ("en_US.UTF-8").locale: Cannot set LC_ALL to default locale: No such file or directorySelecting previously unselected package daemon.(Reading database ... 136881 files and directories currently installed.)Preparing to unpack .../daemon_0.6.4-1_amd64.deb ...Unpacking daemon (0.6.4-1) ...Selecting previously unselected package jenkins.Preparing to unpack .../jenkins_2.164.2_all.deb ...Unpacking jenkins (2.164.2) ...Processing triggers for man-db (2.7.5-1) ...Processing triggers for systemd (229-4ubuntu21.2) ...Processing triggers for ureadahead (0.100.0-19) ...Setting up daemon (0.6.4-1) ...Setting up jenkins (2.164.2) ...Processing triggers for systemd (229-4ubuntu21.2) ...Processing triggers for ureadahead (0.100.0-19) ...

修改默认端口8080

修改 /etc/init.d/jenkins 脚本

sudo vim /etc/init.d/jenkins

修改$HTTP_PORT改为所需的端口

# Verify that the jenkins port is not already in use, winstone does not exit # even for BindException check_tcp_port "http" "$HTTP_PORT" "1314" "$HTTP_HOST" "0.0.0.0" || return 2
# If the var MAXOPENFILES is enabled in /etc/default/jenkins then set the max open files to the # proper value

修改 /etc/default/jenkins 文件

sudo vim /etc/default/jenkins


修改`HTTP_PORT`改为所需的端口

ration, build records,# that sort of things.## If commented out, the value from the OS is inherited, which is normally 022 (as of Ubuntu 12.04,# by default umask comes from pam_umask(8) and /etc/login.defs
# UMASK=027
# port for HTTP connector (default 8080; disable with -1)HTTP_PORT=1314

# servlet context, important if you want to use apache proxyingPREFIX=/$NAME
# arguments to pass to jenkins.# --javahome=$JAVA_HOME# --httpListenAddress=$HTTP_HOST (default 0.0.0.0)# --httpPort=$HTTP_PORT (default 8080; disable with -1)# --httpsPort=$HTTP_PORT# --argumentsRealm.passwd.$ADMIN_USER=[password]

重启服务器

sudo systemctl restart jenkins


改完后会重启出现bug Warning: jenkins.service changed on disk. Run 'systemctl daemon-reload' to reload units


解决方法:

1.

systemctl daemon-reload

    2.

systemctl start jenkins

新端口是1314



启动jenkins

sudo systemctl start jenkins



用下面命令测试或者直接ip+端口或者0.0.0.0:8080访问jenkins

sudo systemctl status jenkins

成功以下:

● jenkins.service - LSB: Start Jenkins at boot timeLoaded: loaded (/etc/init.d/jenkins; bad; vendor preset: enabled)Active: active (exited) since 四 2019-04-18 09:00:28 CST; 8h agoDocs: man:systemd-sysv-generator(8)Process: 1136 ExecStart=/etc/init.d/jenkins start (code=exited, status=0/SUCCE

418 09:00:24 devue-System-Product-Name systemd[1]: Starting LSB: Start Jenkin418 09:00:27 devue-System-Product-Name jenkins[1136]: Correct java version fo418 09:00:27 devue-System-Product-Name jenkins[1136]: * Starting Jenkins Aut418 09:00:27 devue-System-Product-Name su[1511]: Successful su for jenkins by418 09:00:27 devue-System-Product-Name su[1511]: + ??? root:jenkins418 09:00:27 devue-System-Product-Name su[1511]: pam_unix(su:session): sessio418 09:00:28 devue-System-Product-Name jenkins[1136]: ...done.418 09:00:28 devue-System-Product-Name systemd[1]: Started LSB: Start Jenkins



jenkins默认端口就是8080,须要解锁jenkins


将拷贝的密码填入,点击继续

sudo cat /var/lib/jenkins/secrets/initialAdminPassword



点击安装建议的插件


开始安装,等的时间会比较久


安装完成,会提示设置管理用户。能够选择跳过,可是密码未知。最好仍是建立用户。


建立好用户后,进入实例配置


欢迎来到jenkins面板,开始咱们的表演



jenkins没法登陆或者空白页面



解决方案:

vim /var/lib/jenkins/config.xml


在其中版本信息后加入

<authorizationStrategy class="hudson.security.AuthorizationStrategy$Unsecured"/><securityRealm class="hudson.security.SecurityRealm$None"/>


重启服务器便可 

 sudo systemctl restart jenkins


链接github

在jenkin上的操做:安装相关插件

安装插件'Publish Over SSH',链接远程服务器的插件。


下图是已经安装后


安装插件'GitHub Integration Plugin[3]',GitHub集成插件

在github上的操做:配置webhook

github项目中点击Settings选项卡

点击webhook菜单项

添加webhook


Payload URLhttp://+jenkins部署的ip和端口号+/github-webhook/Content type中选择application/jsonWhich events would you like to trigger this webhook?选择just the push event选择Active点击Update webhook


jenkins拉取github上vue代码在本地启动





新建任务

任务名随意选择构建一个自由风格的软件项目,最后肯定


绑定github项目



绑定项目的下载连接

选择GitRepositories中的Repository URL填入项目下载连接(http)Credentials中添加身份信息

在类型中选择Username with password用户名和密码就是github的帐号和密码,最后肯定


Branches to build选择部署的分支(*/分支名)




构建触发器

选择GitHub hook trigger for GITScm polling


构建

执行shell

命令

cd /var/lib/jenkins/workspace/vue npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromedriver npm installnpm run dev





命令不能缺,不然包下不完整

 npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromedriver




保存
任务建立完成,jenkins大功告成

jenkins拉取github上vue代码在远程服务器启动

链接远程服务器

系统管理->系统设置->Publish over SSH

Passphrase:输入jenkins的密码Key:jenkins私钥Name:服务器ip名字Hostname:服务器ipUsername:服务器中的用户名Remote Directory:项目地址点击Test Configuration,出现success,那么链接成功


远程服务器

设置公钥




获取公钥和私钥

su jenkinsssh-keygen -t rsa

判断是否生成公钥和私钥

ls -l /var/lib/jenkins/.ssh/
jenkins@devue-System-Product-Name:/home/devue$ ls -l /var/lib/jenkins/.ssh/total 8-rw------- 1 jenkins jenkins 1675 419 16:26 id_rsa-rw-r--r-- 1 jenkins jenkins 415 419 16:26 id_rsa.pub



拷贝公钥

cd /var/lib/jenkins/.ssh/ cat id_rsa.pub >> authorized_keys chmod 600 authorized_keys

将公钥放进远程服务器

vim ~/.ssh/authorized_keys





当即构建 传输失败


链接gitlab

源码管理

Repository URL必需要http请求

构建

安装Gitlab Hook

若是没有安装Gitlab Hook和gitlab上增长webhook的话,会报错 

gitlab项目侧边栏中Settings-Integrations增长webhook

添加Gitlab Hook插件


jenkins轮询gitlab(必需要是管理员身份)

jenkins想要执行下一个构建任务的时候,是必须等上一个任务完成的(没有勾选并发执行任务)

因为npm run dev,因此在定时构建的时候,并无收到理想效果。需求是:维护进程,定时执行 步骤以下:

加上pm2构建项目,pm2入口[4]

构建触发器,定时构建和轮询SCM二选一


构建中的执行shell为

cd /var/lib/jenkins/workspace/ceres-cms-vue npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromedrivernpm installnpm run buildnpm run pm2pm2 list




仓库代码更新自动构建

不用勾选定时构建和轮询SCM勾选GitHub hook trigger for GITScm polling

这样你的jenkins就能够随着你的代码自动构建了

轮询

定时构建:不管有无最新代码,都按时构建轮询SCM:只要代码有更新,都会构建


构建语法说明:

首先格式为:* * * * *(五个星)


选项 意思
第一个*表示分钟 取值0~59
第二个*表示小时 取值0~23
第三个*表示一个月的第几天 取值1~31
第四个*表示第几月 取值1~12
第五个*表示一周中的第几天 取值0~7,其中0和7表明的都是周日

使用举例(不加H为时刻以前):

选项 意思
每隔1分钟构建一次 H/1 * * * *
每隔1小时构建一次 H H/1 * * *
每个月1号构建一次 H H 1 * *

定时构建和轮询SCM使用互不冲突,具体如何组合,须要根据项目状况合理配置;

目前出现的问题是:没法根据远程仓库是否更新来拉取代码和部署。

测试

本地push代码到github

点击当即构建

构建执行状态


点击#13,进入工程详情


控制台输出结果  


执行中控制台输出



Started by user unknown or anonymous Started by user unknown or anonymous Started by user unknown or anonymous Building in workspace /var/lib/jenkins/workspace/vue using credential 12dc8386-52e8-4c57-b667-bd8d263626cdgit rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository git config remote.origin.url https://github.com/qiufeihong2018/vuepress-app.git # timeout=10 Fetching upstream changes from https://github.com/qiufeihong2018/vuepress-app.git git --version # timeout=10 using GIT_ASKPASS to set credentials git fetch --tags --progress https://github.com/qiufeihong2018/vuepress-app.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 ab0c6b6de9b810dcd9fd107c6329d1e782054976 (refs/remotes/origin/master) git config core.sparsecheckout # timeout=10 git checkout -f ab0c6b6de9b810dcd9fd107c6329d1e782054976 Commit message: "Merge branch 'master' of github.com:qiufeihong2018/vuepress-app" git rev-list --no-walk 9b7e2475ffaef9a60cc38cec1c660d0f9d8dc490 # timeout=10 [vue] $ /bin/sh -xe /tmp/jenkins5471132310334499324.sh•cd /var/lib/jenkins/workspace/vue•npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromedriver

chromedriver@73.0.0[5] install /var/lib/jenkins/workspace/vue/node_modules/chromedriver node install.jsChromeDriver binary exists. Validating... ChromeDriver 73.0.3683.20 (8e2b610813e167eee3619ac4ce6e42e3ec622017)ChromeDriver is already available at '/tmp/73.0.3683.20/chromedriver/chromedriver'. Copying to target path /var/lib/jenkins/workspace/vue/node_modules/chromedriver/lib/chromedriver Fixing file permissions Done. ChromeDriver binary available at /var/lib/jenkins/workspace/vue/node_modules/chromedriver/lib/chromedriver/chromedriver npm WARN vuepress-app@1.0.0[6] No repository field. npm WARN vuepress-app@1.0.0[7] scripts['server'] should probably be scripts['start']. npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.7[8] (node_modules/fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.7[9]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})•chromedriver@73.0.0[10] updated 1 package and audited 14738 packages in 13.642s found 15 vulnerabilities (1 low, 7 moderate, 7 high) run npm audit fix to fix them, or npm audit for details•npm install npm WARN vuepress-app@1.0.0[11] No repository field. npm WARN vuepress-app@1.0.0[12] scripts['server'] should probably be scripts['start']. npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.7[13] (node_modules/fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.7[14]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})audited 14738 packages in 8.37s found 15 vulnerabilities (1 low, 7 moderate, 7 high) run npm audit fix to fix them, or npm audit for details•npm run devvuepress-app@1.0.0[15] dev /var/lib/jenkins/workspace/vue vuepress dev docsWAIT Extracting site metadata... <button @click.ctrl="onClick">A <button @click.ctrl.exact="onCtrlClick">A <button @click.exact="onClick">A TIP override.styl has been split into 2 APIs, we recommend you upgrade to continue. See: https://v0.vuepress.vuejs.org/default-theme-config/#simple-css-override [2:52:53 PM] Compiling Client [2:52:56 PM] Compiled Client in 4s c DONE [14:52:56] Build 854bb5 finished in 3536 ms!VuePress dev server listening at http://localhost:7777/ c[2:52:57 PM] Compiling Client [2:52:57 PM] Compiled Client in 204ms c DONE [14:52:57] Build 056d13 finished in 208 ms! (http://localhost:7777/)




项目启动成功修改提交后,一键当即构建,就能够将最新提交的代码运行起来

解决构建完成后自动杀掉衍生进程的问题

修改BUILD_ID

jenkins默认会在构建完成后杀掉构建过程当中shell命令触发的衍生进程。jenkins根据BUILD_ID识别某个进程是否为构建过程的衍生进程,故修改BUILD_ID后,jenkins就没法识别是否为衍生进程,则此进程能在后台保留运行

OLD_BUILD_ID=$BUILD_IDecho $OLD_BUILD_IDBUILD_ID=dontKillMecd /var/lib/jenkins/workspace/ceres-cms-vuenpm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromedrivernpm installnpm run buildnpm run pm2pm2 listBUILD_ID=$OLD_BUILD_IDecho $BUILD_ID

杀掉改变id的衍生进程

netstat -lntpkill -g {id}

若是没法查看id

则输入命令

ps -ef|grep

显示全部命令,连带命令行 查找文件里符合条件的字符串


参考文献

如何在Ubuntu 16.04上安装Jenkins[16]

Jenkins卸载方法(Windows/Linux/MacOS)[17]

jenkins 忘记admin用户帐号密码[18]

手把手教你搭建Jenkins+Github持续集成环境[19]

Integrate with GitHub: build after each commit (Get started with Jenkins, part 13)[20]

实战笔记:Jenkins打造强大的前端自动化工做流[21]

教你用Vue、GitLab、Jenkins、Nginx实现自动打包发布上线[22]

Jenkins+github 前端自动化部署[23]

Jenkins自动化构建vue项目而后发布到远程服务器[24]

Jenkins自动化发布前端代码VUE (配置模式)[25]

jenkins配置publish over ssh遇到的问题[26]

jenkins使用publishover ssh插件链接应用机器时,报Message Auth fail的问题[27]

Jenkins+git+webhook自动触发部署和测试任务[28]

Jenkins定时构建和轮询SCM设置说明[29]

第四十一章 微服务CICD(3)- jenkins + gitlab + webhooks + publish-over-ssh(1)[30]

jenkins配置权限不对致使没法登录或者空白页面解决办法[31]

jenkins解决构建完成后自动杀掉衍生进程[32]

References

[1] 原文地址: https://github.com/qiufeihong2018/vuepress-blog/tree/master/docs/technical-summary/jenkins
[2] Jenkins官网: https://jenkins.io/
[3] GitHub Integration Plugin: https://github.com/KostyaSha/github-integration-plugin/blob/master/README.adoc
[4] pm2入口: https://github.com/qiufeihong2018/vuepress-blog/tree/master/docs/technical-summary/pm2
[5] chromedriver@73.0.0: mailto:chromedriver@73.0.0
[6] vuepress-app@1.0.0: mailto:vuepress-app@1.0.0
[7] vuepress-app@1.0.0: mailto:vuepress-app@1.0.0
[8] fsevents@1.2.7: mailto:fsevents@1.2.7
[9] fsevents@1.2.7: mailto:fsevents@1.2.7
[10] chromedriver@73.0.0: mailto:chromedriver@73.0.0
[11] vuepress-app@1.0.0: mailto:vuepress-app@1.0.0
[12] vuepress-app@1.0.0: mailto:vuepress-app@1.0.0
[13] fsevents@1.2.7: mailto:fsevents@1.2.7
[14] fsevents@1.2.7: mailto:fsevents@1.2.7
[15] vuepress-app@1.0.0: mailto:vuepress-app@1.0.0
[16] 如何在Ubuntu 16.04上安装Jenkins: https://www.jianshu.com/p/845f267aec52
[17] Jenkins卸载方法(Windows/Linux/MacOS): https://www.cnblogs.com/EasonJim/p/6277708.html
[18] jenkins 忘记admin用户帐号密码: http://www.cnblogs.com/xiami303/p/3625829.html
[19] 手把手教你搭建Jenkins+Github持续集成环境: https://github.com/muyinchen/woker/blob/master/%E9%9B%86%E6%88%90%E6%B5%8B%E8%AF%95%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/%E6%89%8B%E6%8A%8A%E6%89%8B%E6%95%99%E4%BD%A0%E6%90%AD%E5%BB%BAJenkins%2BGithub%E6%8C%81%E7%BB%AD%E9%9B%86%E6%88%90%E7%8E%AF%E5%A2%83.md
[20] Integrate with GitHub: build after each commit (Get started with Jenkins, part 13): https://www.youtube.com/watch?v=Z3S2gMBUkBo
[21] 实战笔记:Jenkins打造强大的前端自动化工做流: https://juejin.im/post/5ad1980e6fb9a028c42ea1be
[22] 教你用Vue、GitLab、Jenkins、Nginx实现自动打包发布上线: https://www.vmartaw.com/zcc/2018/07/08/%E6%95%99%E4%BD%A0%E7%94%A8Vue-GitLab-Jenkins-Nginx%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%8A%A8%E6%89%93%E5%8C%85%E5%8F%91%E5%B8%83%E4%B8%8A%E7%BA%BF/
[23] Jenkins+github 前端自动化部署: https://segmentfault.com/a/1190000010200161
[24] Jenkins自动化构建vue项目而后发布到远程服务器: https://app.csdn.net/ansu2009/article/details/83584796
[25] Jenkins自动化发布前端代码VUE (配置模式): https://app.csdn.net/rankawin/article/details/81699460
[26] jenkins配置publish over ssh遇到的问题: https://zhuanlan.zhihu.com/p/39549204
[27] jenkins使用publishover ssh插件链接应用机器时,报Message Auth fail的问题: https://app.csdn.net/u010947098/article/details/61922969
[28] Jenkins+git+webhook自动触发部署和测试任务: https://www.jianshu.com/p/ad018160aff9
[29] Jenkins定时构建和轮询SCM设置说明: https://app.csdn.net/MenofGod/article/details/81288987
[30] 第四十一章 微服务CICD(3)- jenkins + gitlab + webhooks + publish-over-ssh(1): http://www.bubuko.com/infodetail-1823810.html
[31] jenkins配置权限不对致使没法登录或者空白页面解决办法: https://blog.csdn.net/gaochao1995/article/details/41897141
[32] jenkins解决构建完成后自动杀掉衍生进程: https://blog.csdn.net/wzqnls/article/details/78506055



本文分享自微信公众号 - 全栈大佬的修炼之路(gh_7795af32a259)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。

相关文章
相关标签/搜索