pipeline流水线
jenkinsfile_docker
pipeline {
agent {
docker {
image 'cdp_build:1.8.0_211-maven_3.5.4'
args '-v /opt/workspace/m2:/data/lib -v /opt/workspace/deploy/dev:/deploy -v /root/.ssh:/root/.ssh'
}
}
environment {
DOCKER_REGISTRY = 'my-docker-registry.com'
DOCKER_CRED_ID = 'my-docker-credential'
COMMIT_ID = ''
TAG = ''
BRANCH = "home-develop"
app_name = 'cdp-admin-web'
app_dir = '/data/cdp/tomcat-admin-web'
repository_url = 'http://192.168.2.18/chinadatapay/cdp-base.git'
user_id = 'jluo@chinadatapay.com'
}
parameters {
choice choices: ['false','true'], description: '是否整体编译打包,默认模块打包,即为false。提示:如果打包失败,选择一次整体编译打包。', name: 'is_all_build'
}
stages {
stage('pull code '){
parallel{
stage('pulling code by jenkins'){
when{
expression{
env.gitlabBranch == null
}
}
steps{
git(changelog: true, poll: true, url: "${repository_url}", branch: "${BRANCH}", credentialsId: "${user_id}")
script{
COMMIT_ID = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()
TAG = BUILD_TAG + '-' + COMMIT_ID
println "Current branch is ${BRANCH}, Commit ID is ${COMMIT_ID}, Image TAG is ${TAG}"
}
}
}
}
}
stage('Build') {
steps {
sh """#!/bin/bash
if [ "${is_all_build}" == 'false' ];then
cd ${app_name}
pwd
echo "#########功能节点编译打包#############"
else
echo "#########整体节点编译打包#############"
pwd
fi
pwd
mvn -T 10 -T 1C clean install -Dmaven.test.skip=true -Ptest
"""
}
}
stage('deploy') {
steps {
sh """#!/bin/bash
echo "#################开始部署_${app_name}#################"
pwd
echo "#################停止${app_name}#################"
pssh -h /deploy/backend_${app_name}.ip -i "cd ${app_dir}/bin && ./stop.sh tomcat-admin-web"
echo "#################删除${app_name}#################"
pssh -h /deploy/backend_${app_name}.ip -i "rm -rf ${app_dir}/webapps/${app_name}/*"
echo "#################拷贝${app_name}#################"
pscp.pssh -h /deploy/backend_${app_name}.ip ./${app_name}/target/*.war ${app_dir}/webapps/
echo "#################解压${app_name}#################"
pssh -h /deploy/backend_${app_name}.ip -i "unzip ${app_dir}/webapps/*.war -d ${app_dir}/webapps/${app_name}/ && rm -rf ${app_dir}/webapps/*.war "
echo "#################启动${app_name}#################"
pssh -h /deploy/backend_${app_name}.ip -i "source /etc/profile && cd ${app_dir}/bin && ./startup.sh"
echo "#################完成部署_${app_name}#################"
"""
}
}
}
}
jenkinsfile_k8s
pipeline{
agent {
kubernetes {
cloud 'GZ-TEST-K8S'
slaveConnectTimeout 1200
workspaceVolume hostPathWorkspaceVolume(hostPath: "/opt/workspace", readOnly: false)
yaml '''
apiVersion: v1
kind: Pod
spec:
containers:
- args: [\'$(JENKINS_SECRET)\',\'$(JENKINS_NAME)\']
#image: 'registry.cn-beijing.aliyuncs.com/citools/jnlp:alpine'
image: 'docker.io/jenkins/jnlp-agent-alpine'
name: jnlp
imagePullPolicy: IfNotPresent
volumeMounts:
- mountPath: "/etc/localtime"
name: "localtime"
readOnly: false
# - mountPath: "/root/.ssh"
# name: "ssh"
# readOnly: false
- command:
- "cat"
env:
- name: "LANGUAGE"
value: "en_US:en"
- name: "LC_ALL"
value: "en_US.UTF-8"
- name: "LANG"
value: "en_US.UTF-8"
#image: "registry.cn-beijing.aliyuncs.com/citools/maven:3.5.3"
image: "192.168.6.20:80/library/jdk-maven:1.8.393-3.6.1"
imagePullPolicy: "IfNotPresent"
name: "build"
tty: true
volumeMounts:
- mountPath: "/etc/localtime"
name: "localtime"
- mountPath: "/root/.m2/"
name: "cachedir"
readOnly: false
- command:
- "cat"
env:
- name: "LANGUAGE"
value: "en_US:en"
- name: "LC_ALL"
value: "en_US.UTF-8"
- name: "LANG"
value: "en_US.UTF-8"
image: "registry.cn-beijing.aliyuncs.com/citools/kubectl:self-1.17"
imagePullPolicy: "IfNotPresent"
name: "kubectl"
tty: true
volumeMounts:
- mountPath: "/etc/localtime"
name: "localtime"
readOnly: false
- command:
- "cat"
env:
- name: "LANGUAGE"
value: "en_US:en"
- name: "LC_ALL"
value: "en_US.UTF-8"
- name: "LANG"
value: "en_US.UTF-8"
image: "registry.cn-beijing.aliyuncs.com/citools/docker:19.03.9-git"
imagePullPolicy: "IfNotPresent"
name: "docker"
tty: true
volumeMounts:
- mountPath: "/etc/localtime"
name: "localtime"
readOnly: false
- mountPath: "/var/run/docker.sock"
name: "dockersock"
readOnly: false
restartPolicy: "Never"
nodeSelector:
build: "true"
volumes:
- hostPath:
path: "/var/run/docker.sock"
name: "dockersock"
- hostPath:
path: "/usr/share/zoneinfo/Asia/Shanghai"
name: "localtime"
- name: "cachedir"
hostPath:
path: "/opt/m2"
'''
}
}
stages{
stage('pull code '){
parallel{
stage('pulling code by jenkins'){
when{
expression{
env.gitlabBranch == null
}
}
steps{
git(changelog: true, poll: true, url: "${repository_url}", branch: "${BRANCH}", credentialsId: "${user_id}")
script{
COMMIT_ID = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()
TAG = BUILD_TAG + '-' + COMMIT_ID
println "Current branch is ${BRANCH}, Commit ID is ${COMMIT_ID}, Image TAG is ${TAG}"
}
}
}
}
}
stage('building'){
steps{
container(name: 'build') {
sh """
cd dtp-backend
mvn clean
mvn package -Dmaven.test.skip=true -P${project_env} -am -U
#ls target/*
"""
}
}
}
stage('docker build for creating image'){
steps{
container(name: 'docker') {
//dtp-admin,dtp-op-admin,dtp-api-gateway,dtp-worker,dtp-trade-admin,dtp-big-screen
sh """
echo "###########################################打包后端镜像开始##############################################"
cd dtp-backend
dir=`pwd`
echo "\${dir}目录"
docker login -u ${HARBOR_USER_USR} -p ${HARBOR_USER_PSW} ${HARBOR_ADDRESS}
for item in ${params.services}
do
echo "#################开始构建 \${item}服务镜像#######################"
if [ "\${item}" = "dtp-admin" ];then
app_dir=dtp-admin
cd \${dir}/\${app_dir}
pwd
elif [ "\${item}" = "dtp-op-admin" ];then
app_dir=dtp-op-admin
pwd
cd \${dir}/\${app_dir}
elif [ "\${item}" = "dtp-api-gateway" ];then
app_dir=dtp-api-gateway
cd \${dir}/\${app_dir}
pwd
elif [ "\${item}" = "dtp-worker" ];then
app_dir=dtp-worker
cd \${dir}/\${app_dir}
pwd
elif [ "\${item}" = "dtp-trade-admin" ];then
app_dir=dtp-trade-admin
cd \${dir}/\${app_dir}
pwd
elif [ "\${item}" = "dtp-big-screen" ];then
app_dir=dtp-big-screen
cd \${dir}/\${app_dir}
pwd
fi
docker build -t ${HARBOR_ADDRESS}/${REGISTRY_DIR}/${env_dtp}_\${item}:${TAG} .
docker push ${HARBOR_ADDRESS}/${REGISTRY_DIR}/${env_dtp}_\${item}:${TAG}
done
echo "###########################################打包后端镜像结束##############################################"
"""
}
}
}
stage('deploying to k8s'){
environment {
MY_KUBECONFIG = credentials('k8s-admin')
}
steps {
container(name: 'kubectl'){
echo "部署服务到 K8S 集群"
sh """
echo 'pwd'
pwd
/usr/local/bin/kubectl --kubeconfig ${MY_KUBECONFIG} get node
"""
}
}
}
}
parameters {
extendedChoice(value: 'dtp-admin,dtp-op-admin,dtp-api-gateway,dtp-worker,dtp-trade-admin,dtp-big-screen', description: '请选择需要发布的服务', multiSelectDelimiter: ' ', name: 'services', quoteValue: false, type: 'PT_CHECKBOX', visibleItemCount: 6)
}
environment {
BRANCH = "project-lvliang"
COMMIT_ID = ""
HARBOR_USER_USR = "admin"
HARBOR_USER_PSW = "Harbor12345"
HARBOR_ADDRESS = "192.168.6.20:80"
REGISTRY_DIR = "lvliang"
project_env = "dev"
NAMESPACE = "lvliang"
TAG = ""
repository_url = 'http://192.168.2.18/chinadatapay/cdp_hb_dtp.git'
user_id = 'jluo@chinadatapay.com'
env_dtp = "lvliang_dev"
}
}