Jenkins多分支流水线Blue Ocean的安装和使用
Posted 一掬净土
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jenkins多分支流水线Blue Ocean的安装和使用相关的知识,希望对你有一定的参考价值。
一、前置环境准备:
1、jdk
下载 jdk-8u171-linux-x64.tar.gz(也可以是其他版本,一样)
解压 /usr/local/jdk1.8.0_171
配置环境变量,在/etc/profile最后面添加如下2行
export JAVA_HOME=/usr/local/jdk1.8.0_171
export PATH=$JAVA_HOME/bin:$PATH
生效命令:source /etc/profile
2、maven
下载 apache-maven-3.8.2-bin.tar.gz(也可以是其他版本,一样)
解压 /usr/local/maven3.8.2 (我这边解压后重命名了下,一样)
配置环境变量,在/etc/profile最后面添加如下2行
export MAVEN_HOME=/usr/local/maven3.8.2
export CLASSPATH=$CLASSPATH:$MAVEN_HOME/lib
export PATH=$PATH:$MAVEN_HOME/bin
生效命令:source /etc/profile
3、git
查看是否安装git: rpm -qa | grep git
[root@nb1 harbor]# yum install git
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 git.x86_64.0.1.8.3.1-23.el7_8 将被 安装
--> 正在处理依赖关系 perl-Git = 1.8.3.1-23.el7_8,它被软件包 git-1.8.3.1-23.el7_8.x86_64 需要
--> 正在处理依赖关系 perl(Term::ReadKey),它被软件包 git-1.8.3.1-23.el7_8.x86_64 需要
--> 正在处理依赖关系 perl(Git),它被软件包 git-1.8.3.1-23.el7_8.x86_64 需要
--> 正在处理依赖关系 perl(Error),它被软件包 git-1.8.3.1-23.el7_8.x86_64 需要
--> 正在检查事务
---> 软件包 perl-Error.noarch.1.0.17020-2.el7 将被 安装
---> 软件包 perl-Git.noarch.0.1.8.3.1-23.el7_8 将被 安装
---> 软件包 perl-TermReadKey.x86_64.0.2.30-20.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
==============================================================================================================
Package 架构 版本 源 大小
==============================================================================================================
正在安装:
git x86_64 1.8.3.1-23.el7_8 base 4.4 M
为依赖而安装:
perl-Error noarch 1:0.17020-2.el7 base 32 k
perl-Git noarch 1.8.3.1-23.el7_8 base 56 k
perl-TermReadKey x86_64 2.30-20.el7 base 31 k
事务概要
==============================================================================================================
安装 1 软件包 (+3 依赖软件包)
总下载量:4.5 M
安装大小:22 M
Is this ok [y/d/N]: y
Downloading packages:
(1/4): perl-Error-0.17020-2.el7.noarch.rpm | 32 kB 00:00:00
(2/4): perl-TermReadKey-2.30-20.el7.x86_64.rpm | 31 kB 00:00:00
(3/4): perl-Git-1.8.3.1-23.el7_8.noarch.rpm | 56 kB 00:00:01
(4/4): git-1.8.3.1-23.el7_8.x86_64.rpm | 4.4 MB 00:00:01
--------------------------------------------------------------------------------------------------------------
总计 2.5 MB/s | 4.5 MB 00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : 1:perl-Error-0.17020-2.el7.noarch 1/4
正在安装 : perl-TermReadKey-2.30-20.el7.x86_64 2/4
正在安装 : perl-Git-1.8.3.1-23.el7_8.noarch 3/4
正在安装 : git-1.8.3.1-23.el7_8.x86_64 4/4
验证中 : git-1.8.3.1-23.el7_8.x86_64 1/4
验证中 : 1:perl-Error-0.17020-2.el7.noarch 2/4
验证中 : perl-Git-1.8.3.1-23.el7_8.noarch 3/4
验证中 : perl-TermReadKey-2.30-20.el7.x86_64 4/4
已安装:
git.x86_64 0:1.8.3.1-23.el7_8
作为依赖被安装:
perl-Error.noarch 1:0.17020-2.el7 perl-Git.noarch 0:1.8.3.1-23.el7_8 perl-TermReadKey.x86_64 0:2.30-20.el7
完毕!
[root@nb1 harbor]# git --version
git version 1.8.3.1
4、docker
直通车:https://blog.csdn.net/wdy_2099/article/details/77367107
5、Jenkins安装
我这边有个war包,直接放到tomcat下,很简单,基本一直下一步就ok了。网上也很多文章,搜索新点的文章基本都ok。
jenkins.war下载链接:https://www.aliyundrive.com/s/yiTpLXP7Syg
二、安装流水线插件BlueOcean
如果安装Jenkins后在左侧列表里没有看到“Open Blue Ocean”的菜单,则需要安装BlueOcean插件。
安装方法就是,如下图:点击Manager Jenkins–> Manager Plugins
搜索"blue ocean", 说明,正常是可以看到名称为:“Blue Ocean”的插件的,我是已经安装了,故跑到已安装Tab里了哈。选中—> 点击左下角的Install without restart(立即安装)即可。
安装完成后,回到jenkins首页,就可以看到下面第1张图的左侧菜单里有了“打开Blue Ocean”的按钮, 点击该按钮,则切换到了BlueOcean展示方式。如下第2张图:就表示安装成功了。
接下来,创建一个项目的流水线:这里我用gitee作为示例,也可以是gitlab、github等。
第一次创建,是需要选择 Create new credential 的,将你gitee的用户名、密码,然后点击创建证书就可以了。
最后,点击下面的“创建流水线”按钮完成创建。
由此分支流水线Blue Ocean的安装就完成了。
创建好流水线后,需要回到 任务详情中, 点击“ 立刻Scan 多分支流水线 ” 才可以自动扫描根目录中带有Jenkinsfile文件的分支了。我这里在dev分支里加了Jenkinsfile文件。
附项目目录结构:可以看到有Jenkinsfile文件。
Jenkinsfile 文件内容如下:这里加了一些说明,用的时候,需要去掉哈,或者改成//的注释方式。注意缩进。
# 由于这边是单机版Jenkins,所以直接使用master节点:
node('master') {
#加入一些环境变量,供下面调用,也通用,只需改变本变量即可自动部署,
# APP: 项目打包后的名称,你mvn clean package一下,生成的jar包全名就是它的值。
# APP_NAME: 部署到K8s中的工作负载名称,以及在镜像仓库中的仓库名称
# START_COMMOND: 是Dockerfile添加的CMD命令,当容器被启动时会被执行(这里是java应用,故为此例,springboot的一些命令都可以加到此,比如-DNACOS_SERVER -DNACOS_NAMESPACE --server.port 等等)。
withEnv(['APP=k8stest-1.0.jar','APP_NAME=k8snb', 'START_COMMOND="CMD java -Xms512M -Xmx2048M -jar app.jar --server.port=8080 -Dclient.logRoot=/app/logs"']) {
# 这是一个流水线的stage, 帮助在blueOcean中部署项目时,选择诸如部署的环境,部署项目的配置文件的active等
stage('EnvSelect') {
echo "0.EnvSelect Stage"
script {
env.NAME_SPACE = input message: 'Which Env To Deploy', ok: 'YES', parameters: [choice(name: 'NAME_SPACE', choices: ['k8snb', 'k8stest-blueocean'], description: 'Which Env To Deploy')]
env.PROFILE = input message: 'Which Profile To Active', ok: 'YES', parameters: [string(defaultValue: env.NAME_SPACE, description: 'Which Profile To Active', name: 'PROFILE', trim: false)]
}
}
# 顾名思义,克隆项目
stage('Clone') {
echo "1.Clone Stage"
sh "date"
checkout scm
script {
build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
if (env.BRANCH_NAME != 'master') {
build_tag = "${env.BRANCH_NAME}-${build_tag}"
}
}
}
# 顾名思义,用maven项目,这边加了jenkins部署主机的maven全路径执行clean package 并跳过测试
stage('Compile') {
echo "2.Compile Stage"
sh "/usr/local/apache-maven-3.8.2/bin/mvn clean package -DskipTests"
sh "mv target/$APP ."
}
# 配置Dockerfile,将通用的Dockerfile 复制到工作目录,然后sed替换里面的<APP>变量。
stage('Config') {
echo "3.Config Stage"
# 这里pwd其实就是jenkins的工作空间目录
sh "pwd"
sh "cp -r /usr/local/script/Dockerfile ./Dockerfile"
sh "sed -i 's/<APP>/$APP/' Dockerfile"
}
# 构建镜像,打tag,上传镜像仓库,我这边用的hub.docker.com测试,故弄的公开镜像,生产中得搭建私有镜像仓库,或者使用阿里云、华为云等的组件(花钱买说白了就是)
#以下4个sh分别表示1、登录镜像仓库;2、构建本地镜像;3、推送镜像到远程镜像仓库;4、推送完删除本地镜像(可以去掉本命令不自动删,自己手动删也是也一样的,但一般本地不留存,因为远程镜像仓库都有了);
stage('Build') {
echo "4.Build Stage"
withCredentials([usernamePassword(credentialsId: 'dockerhub', passwordVariable: 'dockerhubPassword', usernameVariable: 'dockerhubUser')]) {
sh "sudo docker login -u ${dockerhubUser} -p ${dockerhubPassword} docker.io"
sh "sudo docker build -t docker.io/mydockerhubname/$APP_NAME:${build_tag} ."
sh "sudo docker push docker.io/mydockerhubname/$APP_NAME:${build_tag}"
sh "sudo docker rmi mydockerhubname/$APP_NAME:${build_tag}"
}
}
# 部署镜像到k8s容器中:这里也是复制通用的k8s.yaml文件,替换其中的3个变量,并执行。最后输出查看svc的命令
# 这里我补充一下:这里是我部署k8s的命令,并不适合所有人。如果大家是自己玩,比如启动jar的方式启动程序的话。可以将下面6个sh命令换一下即可,比如写个.sh脚本(里面写jar的启动方式,启动之前替换下端口号),然后再在这边执行一下。再比如docker镜像的方式启动,那就直接docker run命令启动就可以了。
# 这边的思路就是sed命令替换脚本里的内容,使得脚本变得通用了,都是linux上的一些命令,没什么出奇的地方。
stage('Deploy') {
echo "5. Deploy Stage"
if (env.BRANCH_NAME == 'master') {
input "确认要部署线上环境吗?"
}
sh "cp -r /usr/local/script/k8s.yaml ."
sh "sed -i 's/<APP_NAME>/$APP_NAME/' k8s.yaml"
sh "sed -i 's/<NAMESPACE>/$NAME_SPACE/' k8s.yaml"
sh "sed -i 's/<APP_TAG>/${build_tag}/' k8s.yaml"
sh "sudo kubectl apply -f k8s.yaml --record"
sh "sudo kubectl get svc -A | grep $APP_NAME"
}
}
}
上述Jenkinfile里用到的通用的Dockerfile、 k8s.yaml如下:
- Dockerfile
FROM openjdk:8
ARG app
ADD <APP> app.jar
RUN ["/bin/bash", "-c", "chmod 777 app.jar "]
CMD java -Xms512M -Xmx2048M -jar /app.jar --server.port=8080
- k8s.yaml
apiVersion: v1
kind: Service
metadata:
name: <APP_NAME>
namespace: <NAMESPACE>
spec:
type: NodePort
ports:
- name: <APP_NAME>
port: 8080
# nodePort: 30004
targetPort: 8080
protocol: TCP
selector:
k8s.kuboard.cn/layer: web
k8s.kuboard.cn/name: <APP_NAME>
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: <APP_NAME>
namespace: <NAMESPACE>
spec:
selector:
matchLabels:
k8s.kuboard.cn/layer: web
k8s.kuboard.cn/name: <APP_NAME>
replicas: 1
template:
metadata:
labels:
k8s.kuboard.cn/layer: web
k8s.kuboard.cn/name: <APP_NAME>
spec:
containers:
- name: <APP_NAME>
image: mydockerhubname/<APP_NAME>:<APP_TAG>
# 解决Back-off restarting failed container异常,加上后,会将替代镜像中的默认 ENTRYPOINT 或 CMD。需要特别注意!
# command: [ "/bin/bash", "-ce", "tail -f /dev/null" ]
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
# 这里指定创建的密钥
imagePullSecrets:
- name: dockerhub
三、Jenkins里另外一个配置:全局凭证
上述Jenkins文件中,有推送镜像仓库的命令,如下:
withCredentials([usernamePassword(credentialsId: 'dockerhub', passwordVariable: 'dockerhubPassword', usernameVariable: 'dockerhubUser')]) {
sh "sudo docker login -u ${dockerhubUser} -p ${dockerhubPassword} docker.io"
sh "sudo docker build -t docker.io/mydockerhubname/$APP_NAME:${build_tag} ."
sh "sudo docker push docker.io/mydockerhubname/$APP_NAME:${build_tag}"
sh "sudo docker rmi mydockerhubname/$APP_NAME:${build_tag}"
}
这里的withCredentials块,是读取Jenkins的全局凭据数据的,故需要建立全局凭据,我这里选择的是dockehub作为镜像仓库,故需要把dockehub的用户名密码填写进去。
然后,在全局凭据列表里就可以看到刚建的dockerhub凭据了。
最后,回到刚刚安装Blue Ocean完成后的时刻, 再次点击“ 立刻Scan 多分支流水线 ” 就可以自动扫描根目录中带有Jenkinsfile文件的分支了。我这里在dev分支里加了Jenkinsfile文件(重要,又说了一次,承接)。然后点击“Open Blue Ocean”,即可查看到可以部署的分支列表了:
点击列表右边的运行按钮,即可一键部署项目,等各个步骤执行完成后,如下效果:绿色,表示Jenkins流程执行过程无问题。
如上图,点击右上角的“制品”按钮,整个流水线的日志如下:
完整一次的日志内容如下:
Started by user wangdy
> /usr/bin/git rev-parse --resolve-git-dir /root/.jenkins/caches/git-50b55b75288b192967baef28206c0de9/.git # timeout=10
Setting origin to https://gitee.com/wangdy0909/k8stest.git
> /usr/bin/git config remote.origin.url https://gitee.com/wangdy0909/k8stest.git # timeout=10
Fetching origin...
Fetching upstream changes from origin
> /usr/bin/git --version # timeout=10
> git --version # 'git version 1.8.3.1'
> /usr/bin/git config --get remote.origin.url # timeout=10
using GIT_ASKPASS to set credentials Git username/password for https://gitee.com/wangdy0909/k8stest.git
> /usr/bin/git fetch --tags --progress origin +refs/heads/*:refs/remotes/origin/* # timeout=10
Seen branch in repository origin/dev
Seen branch in repository origin/master
Seen 2 remote branches
Obtained Jenkinsfile from d60bf74bde11408b401b521e25c064517f4ca062
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/k8stest-blueocean_dev
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (EnvSelect)
[Pipeline] echo
0.EnvSelect Stage
[Pipeline] script
[Pipeline] {
[Pipeline] input
Input requested
Approved by wangdy
[Pipeline] input
Input requested
Approved by wangdy
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Clone)
[Pipeline] echo
1.Clone Stage
[Pipeline] sh
+ date
2021年 09月 08日 星期三 15:11:27 CST
[Pipeline] checkout
Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
using credential git:8943d25551d635f0bbdb6d92118c5cc53e8425f031ba989d7b051c2c53d30b26
> /usr/bin/git rev-parse --resolve-git-dir /root/.jenkins/workspace/k8stest-blueocean_dev/.git # timeout=10
Fetching changes from the remote Git repository
> /usr/bin/git config remote.origin.url https://gitee.com/wangdy0909/k8stest.git # timeout=10
Cleaning workspace
> /usr/bin/git rev-parse --verify HEAD # timeout=10
Resetting working tree
> /usr/bin/git reset --hard # timeout=10
> /usr/bin/git clean -fdx # timeout=10
Fetching without tags
Fetching upstream changes from https://gitee.com/wangdy0909/k8stest.git
> /usr/bin/git --version # timeout=10
> git --version # 'git version 1.8.3.1'
using GIT_ASKPASS to set credentials Git username/password for https://gitee.com/wangdy0909/k8stest.git
> /usr/bin/git fetch --no-tags --progress https://gitee.com/wangdy0909/k8stest.git +refs/heads/*:refs/remotes/origin/* # timeout=10
Checking out Revision d60bf74bde11408b401b521e25c064517f4ca062 (dev)
> /usr/bin/git config core.sparsecheckout # timeout=10
> /usr/bin/git checkout -f d60bf74bde11408b401b521e25c064517f4ca062 # timeout=10
> /usr/bin/git branch -a -v --no-abbrev # timeout=10
> /usr/bin/git branch -D dev # timeout=10
> /usr/bin/git checkout -b dev d60bf74bde11408b401b521e25c064517f4ca062 # timeout=10
Commit message: " sh "cp -r /usr/local/script/Dockerfile ./Dockerfile""
> /usr/bin/git rev-list --no-walk d60bf74bde11408b401b521e25c064517f4ca062 # timeout=10
Cleaning workspace
> /usr/bin/git rev-parse --verify HEAD # timeout=10
Resetting working tree
> /usr/bin/git reset --hard # timeout=10
> /usr/bin/git clean -fdx # timeout=10
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ git rev-parse --short HEAD
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Compile)
[Pipeline] echo
2.Compile Stage
[Pipeline] sh
+ /usr/local/apache-maven-3.8.2/bin/mvn clean package -DskipTests
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.wdy:k8stest >---------------------------
[INFO] Building k8stest 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ k8stest ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ k8stest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /root/.jenkins/workspace/k8stest-blueocean_dev/src/main/resources
[INFO] skip non existing resourceDirectory /root/.jenkins/workspace/k8stest-blueocean_dev/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ k8stest ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /root/.jenkins/workspace/k8stest-blueocean_dev/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ k8stest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /root/.jenkins/workspace/k8stest-blueocean_dev/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ k8stest ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ k8stest ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ k8stest ---
[INFO] Building jar: /root/.jenkins/workspace/k8stest-blueocean_dev/target/k8stest-1.0.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.4.RELEASE:repackage (default) @ k8stest ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.500 s
[INFO] Finished at: 2021-09-08T15:11:43+08:00
[INFO] ------------------------------------------------------------------------
[Pipeline] sh
+ mv target/k8stest-1.0.jar .
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Config)
[Pipeline] echo
3.Config Stage
[Pipeline] sh
+ pwd
/root/.jenkins/workspace/k8stest-blueocean_dev
[Pipeline] sh
+ cp -r /usr/local/script/Dockerfile ./Dockerfile
[Pipeline] sh
+ sed -i 's/<APP>/k8stest-1.0.jar/' Dockerfile
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] echo
4.Build Stage
[Pipeline] withCredentials
Masking supported pattern matches of $dockerhubPassword
[Pipeline] {
[Pipeline] sh
Warning: A secret was passed to "sh" using Groovy String interpolation, which is insecure.
Affected argument(s) used the following variable(s): [dockerhubPassword]
See https://jenkins.io/redirect/groovy-string-interpolation for details.
+ sudo docker login -u mydockerhubname -p **** docker.io
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[Pipeline] sh
+ sudo docker build -t docker.io/mydockerhubname/k8snb:dev-d60bf74 .
Sending build context to Docker daemon 15.45MB
Step 1/5 : FROM openjdk:8
---> a8035e0cce92
Step 2/5 : ARG app
---> Running in 080cdf06aa49
Removing intermediate container 080cdf06aa49
---> 79b5c1a15b51
Step 3/5 : ADD k8stest-1.0.jar app.jar
---> c9b90d9cf71a
Step 4/5 : RUN ["/bin/bash", "-c", "chmod 777 app.jar "]
---> Running in 13557b0d124b
Removing intermediate container 13557b0d124b
---> abe4f649c21c
Step 以上是关于Jenkins多分支流水线Blue Ocean的安装和使用的主要内容,如果未能解决你的问题,请参考以下文章