CICD之Jenkins使用

CICD之Jenkins使用一、Jenkins1、Docker安装Jenkins1.docker安装dockerrun\-uroot\-d\-p8080:8080\-p50000:50000\-vjenkins-data:/var/jenkins_home\-v/var/run/docker.sock:/var/run/docker.sock\jenkinsci/blueocean2.可选镜像jenkins/jenkins:lts#可选镜像jenkin

大家好,又见面了,我是你们的朋友全栈君。

一、Jenkins

1、Docker安装Jenkins

1.docker安装

docker run \
  -u root \
  -d \
  -p 8080:8080 \
  -p 50000:50000 \
  -v jenkins-data:/var/jenkins_home \
  -v /var/run/docker.sock:/var/run/docker.sock \
  jenkinsci/blueocean

2.可选镜像 jenkins/jenkins:lts

#可选镜像 jenkins/jenkins:lts
 
docker service create --name jenkins \
-u root \
-p 8080:8080 -p 50000:50000 \
--mount type=volume,source=jenkins-data,destination=/var/jenkins_home \
--mount type=bind,source=/var/run/docker.sock,destination=/var/run/docker.sock \
--constraint node.role==manager \
jenkinsci/blueocean

#把Jenkins调度manager。jenkins帮我们部署服务,docker service,docker stack
#jenkins是java写。docker方式安装的jenkins。jenkins容器中默认就有java环境啦
#密码
jenkinsci/blueocean比  jenkins/jenkins:lts多了blueocean
blueocean?可视化的CICD。jenkinsfile不会写。通过可视化界面,可视化的创建流水线。
blueocean也可以自己再装。

 
rancher;
参数化构建.....
#插件下载加速
注意:配置国内的镜像
官方下载插件慢 更新下载地址

cd { 
   你的Jenkins工作目录}/updates  #进入更新配置位置
sed -i 's/http:\/\/updates.jenkins-ci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json && sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json

这是直接修改的配置文件,如果前边Jenkins用sudo启动的话,那么这里的两个sed前均需要加上sudo
重启Jenkins,安装插件


https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

JAVA环境是好的,docker命令也是可用

使用jenkins的最佳实战。

  • 1、编写服务的jenkinsfile描述文件。成为Pipeline。流水线文件。解耦(不用我们每次手动配置jenkins的每一步做什么事情,以前都得一步一步自己配置流水线的流程。)
  • 2、jenkins只要拿到这个项目,发现了这个jenkinsfile文件,就能自动化的执行整个流程。

jenkins把项目拉倒jenkins服务器,放到workspace(一般我们的源代码都在这里),开始进行流水线处理。

自动拉取代码

  • 定时轮询
  • 远程触发,github会给我们jenkins(webhook(钩子程序))发送请求{jenkins需要公网能访问}。gitlab。
  • 手动构建(手动立即构建)

编写代码完–>jenkins全部流水执行完—>发送邮件/发短信。

1、安装blueocean插件

2、详细教程可以参加下面的介绍

用 Maven 构建 Java 应用

用 Blue Ocean 创建流水线

2、使用

1、初试pipeline

pipeline { 
   
    agent { 
    docker 'maven:3.3.3' } #代理,
    stages { 
     #阶段。
        stage('build') { 
    #每一个阶段
            steps { 
      #每一个步骤
                sh 'mvn --version'
            }
        }
        stage('test') { 
   
           steps { 
   
              sh 'echo success'
           }
        }
    }
}
#每一个流水线工程最终都在
/var/jenkins_home/workspace/流水线工程名

#agent { docker 'maven:3.3.3' }
整个流水线工程
docker run -t -d -u 0:0 -w /var/jenkins_home/workspace/helloworld --volumes-from cd270036b30eada48bb123338864c1451ee8c7d30997a3b856c6d0174b1be2e2 -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** maven:3.3.3 cat
整个流水线的文件夹是在maven容器内运行的

2、pipeline语法介绍

#1、多步骤。steps{ sh ```多命令 ```}
#2、agent:jenkins接下来的流水线运行在哪个环境
	https://www.jenkins.io/doc/book/pipeline/syntax/#agent
	 agent any:任意环境(立刻就能运行,不挑环境),
	 agent none:顶级整个流水线环境,每个阶段stage,要定义自己的agent环境
	 agent { 
    label 'my-defined-label' }
	 agent { 
    node { 
    label 'labelName' } } ;和agent { 
    label 'labelName' }一个意思,
	 agent { 
   
        docker { 
   
            image 'maven:3-alpine'
            label 'my-defined-label'
            args  '-v /tmp:/tmp'
        }
     } #指定当前流水线或者stage运行在使用docker下载来的这个环境下,用完就删了。如果这些环境有些数据需要永久保存我们就应该挂载出来。
     
#3、环境变量,jenkins整个流水线过程中,我可以把经常要用的一些值,抽取为环境变量,在下面方便引用。
 environment { 
   
        DISABLE_AUTH = 'true'
        DB_ENGINE    = 'sqlite'
    }
 
#4、后置处理
    post { 
   
        always { 
   
            junit 'build/reports/**/*.xml'
        }
        aborted: { 
   
           sh 'echo 666'
        }
    }
always:完成

changed:阶段发生变化执行

fixed:

regression
Only run the steps in post if the current Pipeline’s or stage’s run’s status is failure, unstable, or aborted and the previous run was successful.

aborted:手工停止

failure
Only run the steps in post if the current Pipeline’s or stage’s run has a "failed" status, typically denoted by red in the web UI.

success
Only run the steps in post if the current Pipeline’s or stage’s run has a "success" status, typically denoted by blue or green in the web UI.

unstable
Only run the steps in post if the current Pipeline’s or stage’s run has an "unstable" status, usually caused by test failures, code violations, etc. This is typically denoted by yellow in the web UI.

unsuccessful
Only run the steps in post if the current Pipeline’s or stage’s run has not a "success" status. This is typically denoted in the web UI depending on the status previously mentioned.

cleanup
Run the steps in this post condition after every other post condition has been evaluated, regardless of the Pipeline or stage’s status.

示例:构建失败的时候邮件通知
post { 
   
    failure { 
   
        mail to: 'team@example.com',
             subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
             body: "Something is wrong with ${env.BUILD_URL}"
    }
}
#5、人工确认
 input "Does the staging environment look ok?"
#6、总结:Pipeline语法
pipeline { 
   
   agent any
   stages { 
   
      stage('第一阶段') { 
   
         //
         steps { 
   
           sh 'echo 111'
           emailext body: '哈哈', subject: '测试', to: '22222@qq.com'
         }
      }
   }
}
#所有不会写的语法,全部参照 流水线工程里面的 语法生成器。
片段生成器--》可以生产step里面不会写的内容
Declarative Directive Generator--》生成哪些指令不会用
post { 
   
  always { 
   
    // One or more steps need to be included within each condition's block. } aborted { // One or more steps need to be included within each condition's block.
  }
  success { 
   
    // One or more steps need to be included within each condition's block. } failure { // One or more steps need to be included within each condition's block.
  }
}

3、pipeline指定镜像源

    agent { 
   
        docker { 
   
            image 'maven:3-alpine'  #docker下载来的maven作为接下来的环境,容器用完就没了
            args '-v /root/.m2:/root/.m2'  #mvn从网上下载jar包。下载来的东西都挂载到linux的/root/.m2
        }
    }
    #默认使用docker的maven环境下载很慢。如何下载快的问题
    #1、改配置文件。进入linux的/root/.m2。修改settings-docker.xml
    #2、指定maven的配置。 
    		2.1:先给项目里面放maven-setting.xml文件,配置好mirror,profile等。
    		2.2:jenkins流水线,mvn -gs maven-setting.xml

4、远程构建访问

#远程的github代码提交了,jenkins流水线自动触发构建。jenkins只要公网能访问就行
#远程构建即使配置了github 的webhook,默认会403.我们应该使用用户进行授权
1、创建一个用户
2、一定随便登陆激活一次
3、生成一个apitoken
http://gitops:11d89e84d79bee5b3e0fdd852c3201697e@121.89.193.184:8080/job/simple-java-maven-app/build?token=helloworld
4、github的webhook地址如下
http://用户id:apiToken@111.229.61.54/job/simple-node-js-react-npm-app/build?token=hellojenkins
每个项目是自己的

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XkR4kxw2-1590243264780)(02%E3%80%81CICD&Jenkins.assets/image-20200510214151984.png)]

远程触发: JENKINS_URL/job/simple-java-maven-app/build?token=TOKEN_NAME 请求即可

5、动态变量参照

http://121.89.193.184:8080/job/icoding-java-custom-jenkins/pipeline-syntax/globals

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/139452.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 做直流逆变中用到的全桥逆变电路测试mos管好坏的方法

    做直流逆变中用到的全桥逆变电路测试mos管好坏的方法1)用测电阻法判别结型场效应管的电极根据场效应管的PN结正、反向电阻值不一样的现象,可以判别出结型场效应管的三个电极。具体方法:将万用表拨在R×1k档上,任选两个电极,分别测出其正、反向电阻值。当某两个电极的正、反向电阻值相等,且为几千欧姆时,则该两个电极分别是漏极D和源极S。因为对结型场效应管而言,漏极和源极可互换,剩下的电极肯定是栅极G。也可以将万用表的黑表笔(红表笔也行)任意接触一个

    2022年6月21日
    33
  • 陕西驾驶员考试

    陕西驾驶员考试

    2021年7月27日
    70
  • fstream 中文路径_gradle files have changed

    fstream 中文路径_gradle files have changed在C++的标准库中,std::fstream是个挺好用的文件读写流,操作文件很方便,因为是C++标准库,所以没有其它的环境依赖。在使用fstream过程中,有个打开中文路径文件会失败的问题,自己的代码中一直没处理好,这几天终于有点闲心,把这里改透。涉及很多知识点,也是个遗留已久的问题,特此做个记录。在最后用了个一劳永逸的解决此问题方法:将fstream、FILE再包装下。中文路径使用fstream调试程序过程中,发现打开含中文路径的文件时,会打开失败。查了一些资料,说在VS2008、vs200..

    2026年1月26日
    3
  • pda手持终端软件下载_成为pda

    pda手持终端软件下载_成为pdaPDAF点亮主要参考《MT6763_MT6757_PDAF_Driver_and_Buf_mgr_Porting_Guide.pdf》1、什么是PDAF1.1PDAF的原理在了解相位对焦PDAF,PhaseDetectionAutoFocus之前,在网上肯定会搜到其他对焦方式。比如对比度对焦(反差对焦)CDAF,ContrastDetectionAutoFocus、激光对焦LDAF,LaserDetectionAutoFocus、双核对焦等。…

    2025年9月23日
    10
  • 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-K ( ZOJ 3829 ) Known Notation

    2014ACM/ICPC亚洲区域赛牡丹江站现场赛-K ( ZOJ 3829 ) Known Notation

    2021年12月15日
    46
  • .sln文件

    .sln文件VisualStudio.NET采用两种文件类型(.sln和.suo)来存储特定于解决方案的设置,它们总称为解决方案文件。为解决方案资源管理器提供显示管理文件的图形接口所需的信息,从而在每次继续开发任务时,不会因开发环境而分散精力;*.sln:(VisualStudio.Solution)通过为环境提供对项目、项目项和解决方案项在磁盘上位置的引用,可将它们组织到解决方案中。比如是生成Debug模式,还是Release模式,是通用CPU还是专用的等.ps:…

    2022年4月28日
    77

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号