一、提交代码自动构建

当开发人员在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=
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=
> 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=
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=
Checking out Revision 026cbb9df8efcfc15ff98a27f0fa0c255716bc6a (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=
> git checkout -f 026cbb9df8efcfc15ff98a27f0fa0c255716bc6a
Commit message: "test"
> git rev-list --no-walk 026cbb9df8efcfc15ff98a27f0fa0c255716bc6a # timeout=
Parsing POMs
Established TCP socket on
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./boot/plexus-classworlds-2.6..jar:/usr/local/maven/apache-maven-3.6./conf/logging jenkins.maven3.agent.Maven35Main /usr/local/maven/apache-maven-3.6. /usr/local/jenkins-slave/agent.jar /var/jenkins/maven35-interceptor.jar /var/jenkins/maven3-interceptor-commons.jar
<===[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- 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-, i.e. build is platform dependent!
[INFO] Compiling 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- 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-, i.e. build is platform dependent!
[INFO] Compiling source file to /var/jenkins/workspace/diy-maven_TEST/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.: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: , Failures: , Errors: , Skipped: , Time elapsed: 0.002 sec Results : Tests run: , Failures: , Errors: , Skipped: [JENKINS] Recording test results
[WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.SurefireArchiver$; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/
[WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.BuildInfoRecorder$; 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: --11T12::+:
[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=
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=
> 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=
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=
Checking out Revision 6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=
> git checkout -f 6454cc57fd8252c2859ff8ebd5cf78d087bbdc0d
Commit message: "Add CHANGELOG"
> git rev-list --no-walk 026cbb9df8efcfc15ff98a27f0fa0c255716bc6a # timeout=
Parsing POMs
Established TCP socket on
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./boot/plexus-classworlds-2.6..jar:/usr/local/maven/apache-maven-3.6./conf/logging jenkins.maven3.agent.Maven35Main /usr/local/maven/apache-maven-3.6. /usr/local/jenkins-slave/agent.jar /var/jenkins/maven35-interceptor.jar /var/jenkins/maven3-interceptor-commons.jar
<===[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- 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-, i.e. build is platform dependent!
[INFO] Compiling 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- 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-, i.e. build is platform dependent!
[INFO] Compiling source file to /var/jenkins/workspace/diy-maven_TEST/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.: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: , Failures: , Errors: , Skipped: , Time elapsed: 0.002 sec Results : Tests run: , Failures: , Errors: , Skipped: [JENKINS] Recording test results
[WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.SurefireArchiver$; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/
[WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.BuildInfoRecorder$; 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: --11T12::+:
[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}\bin\mvn" -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}\bin\mvn" -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

DEVOPS技术实践_03:Jenkins自动构建的更多相关文章

  1. DEVOPS技术实践_02:jenkins自动构建项目

    一.用户名密码错误 打开jenkins发现用户名密码错误,解决 1.1 找到config.xml文件 [root@jenkins-master ~]# ll -a drwxr-xr-x. root r ...

  2. DEVOPS技术实践_17:Jenkins使用扩展邮件功能发送邮件

    一 环境准备 1.1 安装插件Email Extension 系统管理-管理插件-安装Email Extension插件 1.2 配置 配置jenkins邮箱的全局配置:系统管理-系统设置-完成邮箱配 ...

  3. DEVOPS技术实践_09:Jenkins多分支管道

    简介 多分支的管道是在jenkins2.x中新增的功能 . 多分支管道允许你针对分布式的控制器的每个分支创建一个管道. 下图是对它的一个描述.使用jenkinsfile去创建多分支的管道,jenkin ...

  4. DEVOPS技术实践_07:Jenkins 管道工作

    一 简介 由于在公司构建很多工作,都是使用的maven工作构建的,这种构建方式很大缺点就是所有的工作都需要一步一步的配置,当项目较多,需求变动时,需要很大的精力去修改配置,很费劲 Pipeline即为 ...

  5. DEVOPS技术实践_18:Jenkins的Pinpeline对于参数的使用

    因为最近使用Pipeline声明式语法构建项目,但是最近项目的参数设置较多,特地的来学习一下关于参数的调用和测试,主要式从一个大神那里学习的,结尾回贴上大神的博客链接 1 构建一个pipeline项目 ...

  6. DEVOPS技术实践_11:Jenkins集成Sonar

    前言 前面已经有介绍sonar的安装,简单应用,下面在简答的研究一下sonar和jenkins集成的简单使用,对于sonar的安装不做介绍 一 sonar的简单介绍 持续检查避免了低质量的代码,比如S ...

  7. DEVOPS技术实践_01:jenkins集成平台

    一.准备环境 准备三台机器 角色 IP地址 用户名 密码 jenkins-master   172.25.254.130    admin   meiyoumima gitlab 172.25.254 ...

  8. Gitlab源码库里代码提交后,如何触发jenkins自动构建?

    版本库里代码提交后,如何触发jenkins自动构建?这是一个面试题,感觉自己回答的并不好,因为并没有用过这个功能,之前公司实际项目用的是svn版本管理,一般都用立刻构建,和定时任务构建(不管代码是否有 ...

  9. 小伙伴想学Jenkins自动构建发布项目,我:安排上了!!

    写在前面 趁着十一长假,很多小伙伴都在悄悄学习,有些是为了能够顺利通过面试,进入大厂升职加薪.有些则是为了进一步巩固和提高自己的专业技能,希望有朝一日能过成为互联网架构师乃至技术专家.这不,就有小伙伴 ...

随机推荐

  1. IO流实现文件及文件夹的复制

    TestCopyDocuments.java package com.sxt.parc; /* * 复制文件夹 包含文本 视频 音频 用字节流 */ import java.io.BufferedIn ...

  2. oracle函数 TO_DATE(X[,c2[,c3]])

    [功能]将字符串X转化为日期型 [参数]c2,c3,字符型,参照to_char() [返回]字符串 如果x格式为日期型(date)格式时,则相同表达:date x 如果x格式为日期时间型(timest ...

  3. Java练习 SDUT-1131_最大公约数与最小公倍数

    C/C++训练1---最大公约数与最小公倍数 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 输入两个正整数,求它们的最 ...

  4. Laravel 的HTTP请求#

    获取请求# 要通过依赖注入的方式来获取当前HTTP请求的实例,你应该在控制器方法中类型提示Illuminate\Http\Request 传入的请求的实例通过 服务容器自动注入: <?php n ...

  5. @loj - 3022@ 「CQOI2017」老 C 的方块

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 老 C 是个程序员. 作为一个懒惰的程序员,老 C 经常在电脑上 ...

  6. Android的headerView和emptyView共存问题

    今天做项目的时候,准备优化下ListView相关的东西,于是乎,需要做一个当列表无数据时,空的提醒页面.这个自然想到的是ListView的setEmptyView()方法,于是顺手就写了,可是,当我为 ...

  7. Android 开源库StickyListHeadersListView来实现ListView列表分组效果

    项目中有一新的需求,要求能像一些Android机带"联系人列表"一样,数据可以自动分组,且在列表滑动过程中,列表头固定在顶部,效果图如下: 下面就带大家实现上面的效果, 首先,我们 ...

  8. activiti工作流引擎之uel表达式

    qq讨论群:313032825本人做了一个微信公众号,用于分享各类视频学习资源和我多年学习经验,喜欢的可以关注哦! 有了前面几章,我们肯定有一定的困惑,activiti如何与实际业务整合,比如一条采购 ...

  9. oracle使用表的别名(Alias)

    当在SQL语句中连接多个表时, 请使用表的别名并把别名前缀于每个Column上.这样一来,就可以减少解析的时间并减少那些由Column歧义引起的语法错误.   (Column歧义指的是由于SQL中不同 ...

  10. xUtils框架的介绍(三)

    接上回,继续介绍xUtils的最后两个模块:DbUtils和HttpUtils.首先先介绍第一个SQLite数据库操纵的简单ORM框架,只要能理解xUtils为我们提供的api,相信你也能熟练的把Db ...