利用jenkins的pipline实现CI/CD(java篇)

环境准备

1、jdk环境、maven环境、harbor、docker、docker-compose、jenkins(yum安装)

下图为各组件版本

要点(坑点):

1、docker

cat /etc/docker/daemon.json
{
  "registry-mirrors": ["https://pxr474dd.mirror.aliyuncs.com"]
}

#这个文件只放了镜像加速地址。网上说放私有镜像仓库地址(harbor),我试了不行,请大神指教。
我放在了docker启动文件docker.service
[root@localhost local]# cat /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --insecure-registry=192.168.115.128 --containerd=/run/containerd/containerd.sock #加入私有镜像仓库地址
#ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

# kill only the docker process, not all processes in the cgroup
KillMode=process

[Install]
WantedBy=multi-user.target

2、jenkins

问题:自己二进制安装的jdk可能jenkins找不到所以需要在配置文件添加

cat vim /etc/init.d/jenkins

只复制要改的地方
。。。。。。。。。。。。。
# Search usable Java as /usr/bin/java might not point to minimal version required by Jenkins.
# see http://www.nabble.com/guinea-pigs-wanted-----Hudson-RPM-for-RedHat-Linux-td25673707.html
candidates="
/etc/alternatives/java
/usr/lib/jvm/java-1.8.0/bin/java
/usr/lib/jvm/jre-1.8.0/bin/java
/usr/lib/jvm/java-1.7.0/bin/java
/usr/lib/jvm/jre-1.7.0/bin/java
/usr/lib/jvm/java-11.0/bin/java
/usr/lib/jvm/jre-11.0/bin/java
/usr/lib/jvm/java-11-openjdk-amd64
/usr/bin/java
/usr/local/jdk1.8.0_271/bin/java   #加入自己的java地址
"
for candidate in $candidates
do
  [ -x "$JENKINS_JAVA_CMD" ] && break
  JENKINS_JAVA_CMD="$candidate"
done
。。。。。。。。。。。。。

问题:在pipline里开头要使用tools,要不有可能找不到

pipeline {
  agent any
  tools {
    maven 'maven'
	jdk 'jdk' 
    }

3、harbor

问题:禁用https

hostname: www.myharbor.com

# http related config
http:
  # port for http, default is 80. If https enabled, this port will redirect to https port
  port: 80

# https related config
#https:
  # https port for harbor, default is 443  #注释掉这几行配置,如果使用请填写证书
#  port: 443
  # The path of cert and key files for nginx
#  certificate: /your/certificate/path
#  private_key: /your/private/key/path

# Uncomment external_url if you want to enable external proxy
# And when it enabled the hostname will no longer used
# external_url: https://reg.mydomain.com:8433

# The initial password of Harbor admin
# It only works in first time to install harbor
# Remember Change the admin password from UI after launching Harbor.
harbor_admin_password: Harbor12345

下面开始

pipline实例

blue ocean版

pipeline {
  agent any
  tools {
        maven 'maven'  #这个名字对应系统设置那里maven环境的名字
		jdk 'jdk'      #这个名字对应系统设置那里jdk环境的名字
    }
  stages {
    stage('Build') {
      steps {
		sh 'echo $PATH'
        sh 'mvn -B -DskipTests clean package'
      }
    }
    stage('Create Dockerfile') {
      steps {
         sh '''cat << EOF > Dockerfile
			FROM 192.168.115.128/java/java-base:8-jdk-oralc  #自制包
			WORKDIR /opt/panda
			COPY target/*.jar  .  #复制jar到WORLDIR目录
			ENTRYPOINT ["/bin/bash","/root/run.sh"]   #启动命令自制包里的
			'''
		 sh 'cat Dockerfile'
	  }
    }
	stage('Build Images') {    #这一段可以全局凭证来做替换
	  steps {
	     sh '''
		 docker build -t 192.168.115.128/java/java-damo:${BUILD_NUMBER} .
		 docker login -u admin -p 123456 192.168.115.128
		 docker push 192.168.115.128/java/java-damo:${BUILD_NUMBER}
		 '''
	  }
	}
	stage('Pull and Deploy') {
	  steps {
	     sh 'echo starting deploy'
	  }
	}
  }
}

普通流水线版

pipeline {
  agent any
  tools {
        maven 'maven'
		jdk 'jdk' 
    }
  stages {
    stage('checkout'){
      steps {
            checkout([$class: 'GitSCM', branches: [[name: '${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'cd59f48f-d48f-43fe-b533-dc94fad22eb7', url: 'https://github.com/pythonohtyp/simple-java-maven-app.git']]])   #拉取代码
        }
    }
    stage('Build') {
      steps {
		sh 'echo $PATH'
		echo "${env.JOB_NAME}"
		echo "$country"
        sh 'mvn -B -DskipTests clean package'
      }
    }
    stage('Create Dockerfile') {
      steps {
         sh '''cat << EOF > Dockerfile
			FROM 192.168.115.128/java/java-base:8-jdk-oralc
			WORKDIR /opt/panda
			COPY target/*.jar  .
			ENTRYPOINT ["/bin/bash","/root/run.sh"]
			'''
		 sh 'cat Dockerfile'
	  }
    }
	stage('Build Images') {
	  steps {
	     sh '''
		 docker build -t 192.168.115.128/java/java-damo:${BUILD_NUMBER} .
		 docker login -u admin -p 123456 192.168.115.128
		 docker push 192.168.115.128/java/java-damo:${BUILD_NUMBER}
		 '''
	  }
	}
	stage('Pull and Deploy') {
	  steps {
	     sh 'echo starting deploy'
	  }
	}
  }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值