一、提交代码自动构建

当开发人员在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. ansible基础☞安装方法

    一 需要安装些什么 Ansible默认通过 SSH 协议管理机器. 安装Ansible之后,不需要启动或运行一个后台进程,或是添加一个数据库.只要在一台电脑(可以是一台笔记本)上安装好,就可以通过这台 ...

  2. Python基础:02数字

    1:Python标准整数类型等价于C的(有符号)长整型.整数一般以十进制表示,但是Python也支持八进制或十六进制来表示整数.八进制整数以数字“0”开始, 十六进制整数则以“0x”或“0X”开始. ...

  3. CF1238E.Keyboard Purchase 题解 状压/子集划分DP

    作者:zifeiy 标签:状压DP,子集划分DP 题目链接:https://codeforces.com/contest/1238/problem/E 题目大意: 给你一个长度为 \(n(n \le ...

  4. 薪资管理系统(Java面向对象思想)

    package com.test3; import java.util.*; import java.io.*; /** * @author qingfeng * 重要思想:面向对象思想(添加员工管理 ...

  5. MapReduce数据流-输入

  6. CODE FESTIVAL 2017 qual B C 3 Steps(补题)

    总感觉这题是个题意杀,理解错题目了,看了好久才发现题目意思:操作是让,只要两点没有直接相连,而且只要有一条路的距离3,就可以把这两点连接起来. 按照题解中讲的,可以把图分为二分图和非二分图来解.不过题 ...

  7. pip安装软件包报Could not fetch URL

    报这个错误的原因是python.org已经不支持TLSv1.0和TLSv1.1了.更新pip可以解决这个问题,但是你不能用命令 pip install --upgrade pip 做更新,因为TLS证 ...

  8. 数据存储在哪里? Java是值传递还是引用传递?

    寄存器 : 最快的存储区,位于处理器中,寄存器会按需求自行分配空间,java不能控制寄存器,所以在程序中感觉不到它的存在 栈(stack) : 位于RAM(内存)中,速度仅次于寄存器,存储对象的引用( ...

  9. Java JDBC学习实战(一): JDBC的基本操作

    一.JDBC常用接口.类介绍 JDBC提供对独立于数据库统一的API,用以执行SQL命令.API常用的类.接口如下: DriverManager,管理JDBC驱动的服务类,主要通过它获取Connect ...

  10. 2015年NOIP普及组复赛题解

    题目涉及算法: 金币:入门题: 扫雷游戏:入门题: 求和:简单数学推导: 推销员:贪心. 金币 题目链接:https://www.luogu.org/problem/P2669 入门题,直接开一个循环 ...