当前位置:   article > 正文

Jenkins自动部署-GitHub_jenkins配置从github自动部署

jenkins配置从github自动部署

一、前期准备

1.1 服务器准备

序号系统内存CPUIP地址备注
1CentOS4G4192.168.61.68项目部署
2CentOS4G4192.168.61.69Jenkins安装
3CentOS4G4192.168.61.69Ngrok安装

1.2 Jenkins服务安装

参考链接:https://blog.csdn.net/weixin_41668084/article/details/113517910

1.3 Ngrok服务安装

参考链接:https://blog.csdn.net/weixin_41668084/article/details/113730829

1.4 部署项目准备

项目地址:https://github.com/shuangxinyuan/jenkinstest

二、Jenkins配置

2.1 插件安装

①**Maven Integration:新建job时有maven项目可以选择;
Deploy to container:将war包部署到tomcat所在的服务器上;
Publish Over SSH:**通过ssh推送文件,并可以执行shell命令;

image-20210208223316368

2.2 Maven、Git、JDK配置

点击:“系统管理”------>“全局工具配置”

image-20210208223518412

配置Maven,Git,JDK

image-20210208223644337

三、Jenkins新建任务

3.1 SSH配置

点击“系统管理”---->“系统设置”,进行ssh配置

image-20210208224152764

3.2 Ngrok反向代理配置连接Jenkins

3.2.1 ngrok启动
[root@localhost bin]# ./sunny clientid 申请的服务id
  • 1
image-20210208230209270
3.2.2 Jenkins代理连接
image-20210208230457434

3.3 WebHook设置

3.3.1 Github申请Token
image-20210208231800362 image-20210208231902785
3.3.2 项目设置WebHook
image-20210208232228568
3.3.3 Jenkins系统配置
3.3.3.1 Jenkins Location
image-20210208232507966
3.3.3.2 GitHub服务器配置
  1. 添加GitHub地址

    image-20210208232702420 image-20210208232826709 image-20210208233129534
  2. 添加钩子

    image-20210208232959593

3.4 新建任务配置

3.4.1 新建一个自由项目
3.4.1.1 通用设置
image-20210208233601327
3.4.1.2 源码管理
image-20210208233815765 image-20210208233936868
3.4.1.3 构建触发器
image-20210208234124265
3.4.1.4 构建环境
image-20210208234446006
3.4.1.5 重启脚本
#!/bin/bash
source /etc/profile
chmod 755 /app/jenkins-test.jar
count=`ps -ef|grep java|grep jenkins-test.jar |awk '{print $2}'`
if [ $count -gt 0 ]; then
  echo '原进程:'$count
  echo '重新启动中...'
  kill -9 $count
  sleep 1
  export BUILD_ID=dontKillMe
  (java -jar /app/jenkins-test.jar > /app/log/log.log 2>&1 &)
else
  echo '启动中...'
  sleep 1
  export BUILD_ID=dontKillMe
  (java -jar /app/jenkins-test.jar > /app/log/log.log 2>&1 &)
fi
echo '现进程:' `ps -ef|grep java|grep jenkins-test.jar |awk '{print $2}'`
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
3.4.1.6 项目启动部署
  1. 启动部署
image-20210208234904479
  1. 查看控制台
image-20210208235013230 image-20210208235401464
  1. 发布日志

    Started by user root
    Running as SYSTEM
    Building in workspace /var/lib/jenkins/workspace/test
    The recommended git tool is: NONE
    using credential 9bb2fab5-c4f1-4952-a1b1-1a5604d5d666
     > /usr/bin/git rev-parse --is-inside-work-tree # timeout=10
    Fetching changes from the remote Git repository
     > /usr/bin/git config remote.origin.url https://github.com/shuangxinyuan/jenkins.git # timeout=10
    Fetching upstream changes from https://github.com/shuangxinyuan/jenkins.git
     > /usr/bin/git --version # timeout=10
     > git --version # 'git version 1.8.3.1'
    using GIT_ASKPASS to set credentials 
     > /usr/bin/git fetch --tags --progress https://github.com/shuangxinyuan/jenkins.git +refs/heads/*:refs/remotes/origin/* # timeout=10
     > /usr/bin/git rev-parse refs/remotes/origin/master^{commit} # timeout=10
    Checking out Revision ce2b7c87e27b3430a91ccc13462439d565d61fb3 (refs/remotes/origin/master)
     > /usr/bin/git config core.sparsecheckout # timeout=10
     > /usr/bin/git checkout -f ce2b7c87e27b3430a91ccc13462439d565d61fb3 # timeout=10
    Commit message: "Jenkins 测试-Ngrok-4"
     > /usr/bin/git rev-list --no-walk ce2b7c87e27b3430a91ccc13462439d565d61fb3 # timeout=10
    [test] $ /usr/local/maven/bin/mvn -s /usr/local/maven/conf/settings.xml -gs /usr/local/maven/conf/settings.xml -DskipTests=true clean compile package
    [INFO] Scanning for projects...
    [INFO] 
    [INFO] --------------------------< com.test:jenkins >--------------------------
    [INFO] Building jenkins 0.0.1-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO] 
    [INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ jenkins ---
    [INFO] Deleting /var/lib/jenkins/workspace/test/target
    [INFO] 
    [INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ jenkins ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 1 resource
    [INFO] Copying 1 resource
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ jenkins ---
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 1 source file to /var/lib/jenkins/workspace/test/target/classes
    [INFO] 
    [INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ jenkins ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 1 resource
    [INFO] Copying 1 resource
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ jenkins ---
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 1 source file to /var/lib/jenkins/workspace/test/target/classes
    [INFO] 
    [INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ jenkins ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/test/src/test/resources
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ jenkins ---
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 1 source file to /var/lib/jenkins/workspace/test/target/test-classes
    [INFO] 
    [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ jenkins ---
    [INFO] Tests are skipped.
    [INFO] 
    [INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ jenkins ---
    [INFO] Building jar: /var/lib/jenkins/workspace/test/target/jenkins-test.jar
    [INFO] 
    [INFO] --- spring-boot-maven-plugin:2.3.5.RELEASE:repackage (repackage) @ jenkins ---
    [INFO] Replacing main artifact with repackaged archive
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  7.523 s
    [INFO] Finished at: 2021-02-08T23:49:39+08:00
    [INFO] ------------------------------------------------------------------------
    SSH: Connecting from host [localhost.localdomain]
    SSH: Connecting with configuration [idea-maven-test] ...
    SSH: EXEC: completed after 1,206 ms
    SSH: Disconnecting configuration [idea-maven-test] ...
    SSH: Transferred 1 file(s)
    Finished: SUCCESS
    
    • 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
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
3.4.1.7 发布校验
  1. 项目进程查看

    image-20210208235653887
  2. 页面校验

    image-20210208235845221
3.4.1.8 WebHook自动发布
  1. 代码修改

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Title</title>
    </head>
    <body>
        <!--<h1>Welcome to Jenkins-Ngrok </h1>-->
        <h1>Welcome To Jenkins-Ngrok </h1>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  2. 代码提交

    image-20210209000518010
  3. Jenkins自动发布

image-20210209003235634 image-20210209003132460 image-20210209003324784
3.4.2 新建一个maven项目
3.4.2.1 通用设置
image-20210209003458794
3.4.2.2 源码管理
image-20210209003603898
3.4.2.3 源码管理
image-20210209003640154
3.4.2.4 构建环境
image-20210209003733047
3.4.2.5 重启脚本
#!/bin/bash
source /etc/profile
chmod 755 /app/jenkins/Jenkins-GitHub-Ngrok.jar
count=`ps -ef|grep java|grep Jenkins-GitHub-Ngrok.jar |awk '{print $2}'`
if [ $count -gt 0 ]; then
  echo '原进程:'$count
  echo '重新启动中...'
  kill -9 $count
  sleep 1
  export BUILD_ID=dontKillMe
  (java -jar /app/jenkins/Jenkins-GitHub-Ngrok.jar > /app/jenkins/log/log.log 2>&1 &)
else
  echo '启动中...'
  sleep 1
  export BUILD_ID=dontKillMe
  (java -jar /app/jenkins/Jenkins-GitHub-Ngrok.jar > /app/jenkins/log/log.log 2>&1 &)
fi
echo '现进程:' `ps -ef|grep java|grep Jenkins-GitHub-Ngrok.jar |awk '{print $2}'`
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
3.4.2.6 构建前操作
image-20210209003819780
3.4.2.7 项目发布
image-20210209004213915
3.4.2.8 项目验证
image-20210209004404627

四、注意要点

4.1 运行权限问题

yum安装的Jenkins 配置文件默认位置/etc/sysconfig/jenkins

默认jenkins服务以jenkins用户运行,这时在jenkins执行maven脚本时可能会发生没有权限操作某个目录下的文件,覆盖文件等情况。例如可能会报下面的错误。

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-install-plugin:2.5.2:install (default-install) on project icps-parent: Failed to install artifact io.binghe:binghe-parent:pom:1.0.0-SNAPSHOT: /home/repository/io/binghe/binghe-parent/1.0.0-SNAPSHOT/binghe-parent-1.0.0-SNAPSHOT.pom (权限不够) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

可以让jenkins以root用户运行来解决这个问题

  1. 将jenkins账号分别加入到root组中
gpasswd -a jenkins root
  • 1
  1. 修改/etc/sysconfig/jenkins文件中
#user id to be invoked as (otherwise will run as root; not wise!)
JENKINS_USER=root
JENKINS_GROUP=root
  • 1
  • 2
  • 3

可以修改为root权限运行
重启服务,搞定。

4.2 部署项目报错

[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[FATAL] Non-readable POM /var/lib/jenkins/workspace/zblog\code\pom.xml: 
/var/lib/jenkins/workspace/zblog\code\pom.xml (No such file or directory) @ 
 @ 
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]   
[ERROR]   The project  (/var/lib/jenkins/workspace/zblog\code\pom.xml) has 1 error
[ERROR]     Non-readable POM /var/lib/jenkins/workspace/zblog\code\pom.xml: 
/var/lib/jenkins/workspace/zblog\code\pom.xml (No such file or directory)
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
Build step 'Invoke top-level Maven targets' marked build as failure
[DeployPublisher][INFO] Build failed, project not deployed
Finished: FAILURE
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

报错的大致意思是:

不能从配置的Git仓库中找到pom.xml文件。

查看Jenkins工作空间下是否有pom.xml文件。

image-20210209005115115

发现我的工作空间下没有pom.xml文件,我的pom.xml文件在code目录下。但是Jenkins配置时候默认是找工作空间下的pom.xml。

解决方案:修改Jenkins查找pom.xml文件的路径。

img

重新构建即可。


参考链接

  1. Jenkins+Maven+Github+Springboot实现可持续自动部署(非常详细)
  2. centos7安装git服务器并使用(笔记)
  3. centos系统查看软件安装路径
  4. LINUX 上GIT默认安装路径
  5. 【Jenkins】Jenkins 运行权限问题
  6. 实现内网映射的三种方式
  7. jenkins之代码推送github触发jenkins自动构建
  8. jenkins自动构建1:github触发钩子
  9. 关于Gitee
  10. Jenkins+花生壳+nginx实现内网穿透的Springboot项目持续部署
  11. Sunny-Ngrok使用教程
  12. 手把手教你搭建Jenkins+Github持续集成环境
  13. Jenkins之配置GitHub-Webhook
  14. Jenkins系列(四)----Jenkins+Github持续集成
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/952078
推荐阅读
相关标签
  

闽ICP备14008679号