• DEVOPS技术实践_03:Jenkins自动构建


    一、提交代码自动构建

    当开发人员在gitlab提交代码后,会自动触发jenkin构建

    点击项目---->点击diy_maven-TEST----->点击配置--->构建触发器---->根据需求选择触发器

    1.1 安装gitlab hook插件

    系统管理----->选择插件管理-------->查找gitlab-------->选择直接安装

    安装完成

     

    1.2 配置gitlab仓库

    点击项目---->点击diy_maven-TEST----->点击配置--->构建触发器

    发现没有变化,需要重启jenkins

    点击是,jenkins重启

    发现没有,升级jenkins

    关机

    然后重新使用Java启动war包就可以了

    继续安装gitlab的其他插件尝试

     点击项目---->点击diy_maven-TEST----->点击配置--->构建触发器

     

    保存这个地址

    http://172.25.254.130:9000/project/diy-maven_TEST

    1.3 gitlab设置

    选择仓库设置setting

     复制url到此处

     

    1.4 点击添加

     

    gitlab 10.6 版本以后为了安全,不允许向本地网络发送webhook请求,如果想向本地网络发送webhook请求,则需要使用管理员帐号登录,默认管理员帐号是admin@example.com,密码就是你gitlab搭建好之后第一次输入的密码,登录之后, 点击Configure Gitlab ,如下图所示

    即可进入Admin area,在Admin area中,在settings标签下面,找到OutBound Request,勾选上Allow requests to the local network from hooks and services ,保存更改即可解决问题

     

    成功

    1.5 点击测试test

    得到200成功

    验证

    控制台输出

    Started by GitLab push by tester admin
    Building remotely on slave1 in workspace /var/jenkins/workspace/diy-maven_TEST
    No credentials specified
     > git rev-parse --is-inside-work-tree # timeout=10
    Fetching changes from the remote Git repository
     > git config remote.origin.url http://172.25.254.131/tester/mvn-test01.git # timeout=10
    Fetching upstream changes from http://172.25.254.131/tester/mvn-test01.git
     > git --version # timeout=10
     > git fetch --tags --progress http://172.25.254.131/tester/mvn-test01.git +refs/heads/*:refs/remotes/origin/*
    skipping resolution of commit remotes/origin/master, since it originates from another repository
     > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
     > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
    Checking out Revision 026cbb9df8efcfc15ff98a27f0fa0c255716bc6a (refs/remotes/origin/master)
     > git config core.sparsecheckout # timeout=10
     > git checkout -f 026cbb9df8efcfc15ff98a27f0fa0c255716bc6a
    Commit message: "test"
     > git rev-list --no-walk 026cbb9df8efcfc15ff98a27f0fa0c255716bc6a # timeout=10
    Parsing POMs
    Established TCP socket on 43253
    maven35-agent.jar already up to date
    maven35-interceptor.jar already up to date
    maven3-interceptor-commons.jar already up to date
    [diy-maven_TEST] $ java -cp /var/jenkins/maven35-agent.jar:/usr/local/maven/apache-maven-3.6.1/boot/plexus-classworlds-2.6.0.jar:/usr/local/maven/apache-maven-3.6.1/conf/logging jenkins.maven3.agent.Maven35Main /usr/local/maven/apache-maven-3.6.1 /usr/local/jenkins-slave/agent.jar /var/jenkins/maven35-interceptor.jar /var/jenkins/maven3-interceptor-commons.jar 43253
    <===[JENKINS REMOTING CAPACITY]===>channel started
    Executing Maven:  -B -f /var/jenkins/workspace/diy-maven_TEST/pom.xml clean install
    [INFO] Scanning for projects...
    [INFO] 
    [INFO] ---------------------< com.example.app:maven-app >----------------------
    [INFO] Building maven-app 1.3-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO] 
    [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-app ---
    [INFO] Deleting /var/jenkins/workspace/diy-maven_TEST/target
    [INFO] 
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-app ---
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory /var/jenkins/workspace/diy-maven_TEST/src/main/resources
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-app ---
    [INFO] Changes detected - recompiling the module!
    [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
    [INFO] Compiling 1 source file to /var/jenkins/workspace/diy-maven_TEST/target/classes
    [INFO] 
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-app ---
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory /var/jenkins/workspace/diy-maven_TEST/src/test/resources
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-app ---
    [INFO] Changes detected - recompiling the module!
    [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
    [INFO] Compiling 1 source file to /var/jenkins/workspace/diy-maven_TEST/target/test-classes
    [INFO] 
    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-app ---
    [INFO] Surefire report directory: /var/jenkins/workspace/diy-maven_TEST/target/surefire-reports
    
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running com.example.app.AppTest
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec
    
    Results :
    
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    
    [JENKINS] Recording test results
    [WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.SurefireArchiver$2; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/
    [WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.BuildInfoRecorder$1; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/
    [INFO] 
    [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-app ---
    [INFO] Building jar: /var/jenkins/workspace/diy-maven_TEST/target/maven-app-1.3-SNAPSHOT.jar
    [INFO] 
    [INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-app ---
    [INFO] Installing /var/jenkins/workspace/diy-maven_TEST/target/maven-app-1.3-SNAPSHOT.jar to /root/.m2/repository/com/example/app/maven-app/1.3-SNAPSHOT/maven-app-1.3-SNAPSHOT.jar
    [INFO] Installing /var/jenkins/workspace/diy-maven_TEST/pom.xml to /root/.m2/repository/com/example/app/maven-app/1.3-SNAPSHOT/maven-app-1.3-SNAPSHOT.pom
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  12.960 s
    [INFO] Finished at: 2019-04-11T12:04:08+08:00
    [INFO] ------------------------------------------------------------------------
    Waiting for Jenkins to finish collecting data
    [JENKINS] Archiving /var/jenkins/workspace/diy-maven_TEST/pom.xml to com.example.app/maven-app/1.3-SNAPSHOT/maven-app-1.3-SNAPSHOT.pom
    [JENKINS] Archiving /var/jenkins/workspace/diy-maven_TEST/target/maven-app-1.3-SNAPSHOT.jar to com.example.app/maven-app/1.3-SNAPSHOT/maven-app-1.3-SNAPSHOT.jar
    channel stopped
    Finished: SUCCESS

     1.6 限制项目于宁节点

    到gitlab验证

    1.7 提交代码验证

    写一些内容,提交

    正在构建

    1.8 控制台输出

    Started by GitLab push by tester admin
    Building remotely on slave1 in workspace /var/jenkins/workspace/diy-maven_TEST
    No credentials specified
     > git rev-parse --is-inside-work-tree # timeout=10
    Fetching changes from the remote Git repository
     > git config remote.origin.url http://172.25.254.131/tester/mvn-test01.git # timeout=10
    Fetching upstream changes from http://172.25.254.131/tester/mvn-test01.git
     > git --version # timeout=10
     > git fetch --tags --progress http://172.25.254.131/tester/mvn-test01.git +refs/heads/*:refs/remotes/origin/*
    skipping resolution of commit remotes/origin/master, since it originates from another repository
     > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
     > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
    Checking out Revision 6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d (refs/remotes/origin/master)
     > git config core.sparsecheckout # timeout=10
     > git checkout -f 6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d
    Commit message: "Add CHANGELOG"
     > git rev-list --no-walk 026cbb9df8efcfc15ff98a27f0fa0c255716bc6a # timeout=10
    Parsing POMs
    Established TCP socket on 34682
    maven35-agent.jar already up to date
    maven35-interceptor.jar already up to date
    maven3-interceptor-commons.jar already up to date
    [diy-maven_TEST] $ java -cp /var/jenkins/maven35-agent.jar:/usr/local/maven/apache-maven-3.6.1/boot/plexus-classworlds-2.6.0.jar:/usr/local/maven/apache-maven-3.6.1/conf/logging jenkins.maven3.agent.Maven35Main /usr/local/maven/apache-maven-3.6.1 /usr/local/jenkins-slave/agent.jar /var/jenkins/maven35-interceptor.jar /var/jenkins/maven3-interceptor-commons.jar 34682
    <===[JENKINS REMOTING CAPACITY]===>channel started
    Executing Maven:  -B -f /var/jenkins/workspace/diy-maven_TEST/pom.xml clean install
    [INFO] Scanning for projects...
    [INFO] 
    [INFO] ---------------------< com.example.app:maven-app >----------------------
    [INFO] Building maven-app 1.3-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO] 
    [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-app ---
    [INFO] Deleting /var/jenkins/workspace/diy-maven_TEST/target
    [INFO] 
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-app ---
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory /var/jenkins/workspace/diy-maven_TEST/src/main/resources
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-app ---
    [INFO] Changes detected - recompiling the module!
    [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
    [INFO] Compiling 1 source file to /var/jenkins/workspace/diy-maven_TEST/target/classes
    [INFO] 
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-app ---
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory /var/jenkins/workspace/diy-maven_TEST/src/test/resources
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-app ---
    [INFO] Changes detected - recompiling the module!
    [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
    [INFO] Compiling 1 source file to /var/jenkins/workspace/diy-maven_TEST/target/test-classes
    [INFO] 
    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-app ---
    [INFO] Surefire report directory: /var/jenkins/workspace/diy-maven_TEST/target/surefire-reports
    
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running com.example.app.AppTest
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec
    
    Results :
    
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    
    [JENKINS] Recording test results
    [WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.SurefireArchiver$2; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/
    [WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.BuildInfoRecorder$1; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/
    [INFO] 
    [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-app ---
    [INFO] Building jar: /var/jenkins/workspace/diy-maven_TEST/target/maven-app-1.3-SNAPSHOT.jar
    [INFO] 
    [INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-app ---
    [INFO] Installing /var/jenkins/workspace/diy-maven_TEST/target/maven-app-1.3-SNAPSHOT.jar to /root/.m2/repository/com/example/app/maven-app/1.3-SNAPSHOT/maven-app-1.3-SNAPSHOT.jar
    [INFO] Installing /var/jenkins/workspace/diy-maven_TEST/pom.xml to /root/.m2/repository/com/example/app/maven-app/1.3-SNAPSHOT/maven-app-1.3-SNAPSHOT.pom
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  10.700 s
    [INFO] Finished at: 2019-04-11T12:12:19+08:00
    [INFO] ------------------------------------------------------------------------
    Waiting for Jenkins to finish collecting data
    [JENKINS] Archiving /var/jenkins/workspace/diy-maven_TEST/pom.xml to com.example.app/maven-app/1.3-SNAPSHOT/maven-app-1.3-SNAPSHOT.pom
    [JENKINS] Archiving /var/jenkins/workspace/diy-maven_TEST/target/maven-app-1.3-SNAPSHOT.jar to com.example.app/maven-app/1.3-SNAPSHOT/maven-app-1.3-SNAPSHOT.jar
    channel stopped
    Finished: SUCCESS

    二 、 Jenkins-pipeline自动化配置

    https://jenkins.io/doc/book/pipeline/

    2.1 安装pipeline插件

    2.2 gitlab创建一个group

     

    2.3 在group创建一个项目

    NEW Project

    2.4 Command line instructions

    Git global setup
    git config --global user.name "tester admin"
    git config --global user.email "joy04007@gmail.com"
    
    Create a new repository
    git clone http://172.25.254.131/devops/jenkins.git
    cd jenkins
    touch README.md
    git add README.md
    git commit -m "add README"
    git push -u origin master
    
    Existing folder
    cd existing_folder
    git init
    git remote add origin http://172.25.254.131/devops/jenkins.git
    git add .
    git commit -m "Initial commit"
    git push -u origin master
    
    Existing Git repository
    cd existing_repo
    git remote rename origin old-origin
    git remote add origin http://172.25.254.131/devops/jenkins.git
    git push -u origin --all
    git push -u origin --tags

    2.5 新加一个文件提交

    NEW File:java.jenkinsfile

    2.6 jenkins创建任务

    jenkins创建一个流水线项目,然后pipeline设置

    node {
       def mvnHome
       stage('Preparation') { // for display purposes
          // Get some code from a GitHub repository
          git 'https://github.com/jglick/simple-maven-project-with-tests.git'
          // Get the Maven tool.
          // ** NOTE: This 'M3' Maven tool must be configured
          // **       in the global configuration.           
          mvnHome = tool 'M3'
       }
       stage('Build') {
          // Run the maven build
          if (isUnix()) {
             sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean package"
          } else {
             bat(/"${mvnHome}inmvn" -Dmaven.test.failure.ignore clean package/)
          }
       }
       stage('Results') {
          junit '**/target/surefire-reports/TEST-*.xml'
          archiveArtifacts 'target/*.jar'
       }
    }

    2.7 添加一个key

    2.8 点击构建

    Started by user unknown or anonymous
    Obtained jaja.jenkinsfile from git http://172.25.254.131/devops/jenkins.git
    Running in Durability level: MAX_SURVIVABILITY
    [Pipeline] Start of Pipeline
    [Pipeline] node
    Running on Jenkins in /root/.jenkins/workspace/java_pipeline-Test
    [Pipeline] {
    [Pipeline] echo
    Hello World
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    Finished: SUCCESS

    成功!!!

    2.9 写一个完整的pipeline

    使用pipeline模板代码

    node {
       def mvnHome
       stage('Preparation') { // for display purposes
          // Get some code from a GitHub repository
          git 'https://github.com/jglick/simple-maven-project-with-tests.git'
          // Get the Maven tool.
          // ** NOTE: This 'M3' Maven tool must be configured
          // **       in the global configuration.           
          mvnHome = tool 'M3'
       }
       stage('Build') {
          // Run the maven build
          if (isUnix()) {
             sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean package"
          } else {
             bat(/"${mvnHome}inmvn" -Dmaven.test.failure.ignore clean package/)
          }
       }
       stage('Results') {
          junit '**/target/surefire-reports/TEST-*.xml'
          archiveArtifacts 'target/*.jar'
       }
    }

    newfile

     

    node {
       def mvnHome
       stage('Checkout') { // for display purposes
           println('checkcode')
       }
       stage('Build') {
          println('Build')
       }
       stage('Test') {
          println('test')
       }
       stage('Deploy'){
           println('Deploy')
       }
       stage('CodeScan'){
           println('code')
       }
    }

     Jenkins在任务配置重新配置脚本路径

    点击立即创建成功

    输出来信息

    Started by user unknown or anonymous
    Obtained Jenkinsfile from git http://172.25.254.131/devops/jenkins.git
    Running in Durability level: MAX_SURVIVABILITY
    [Pipeline] Start of Pipeline
    [Pipeline] node
    Running on Jenkins in /root/.jenkins/workspace/java_pipeline-Test
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (Checkout)
    [Pipeline] echo
    checkcode
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] stage
    [Pipeline] { (Build)
    [Pipeline] echo
    Build
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] stage
    [Pipeline] { (Test)
    [Pipeline] echo
    test
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] stage
    [Pipeline] { (Deploy)
    [Pipeline] echo
    Deploy
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] stage
    [Pipeline] { (CodeScan)
    [Pipeline] echo
    code
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    Finished: SUCCESS

  • 相关阅读:
    c++命名空间重命名
    ssh保持长连接的配置方法
    macos平台上使用 sed
    c++的使用
    NAT介绍以及穿透各种类型nat的技术实现包括对称型nat
    组态图绘制软件的实现
    关于五防系统
    Linux的应用
    java常用包及其类概述
    springboot整合redis
  • 原文地址:https://www.cnblogs.com/zyxnhr/p/10739415.html
Copyright © 2020-2023  润新知