-
安装k3s
官方文档一键安装
修改k3s.service配置文件,使用docker
jenkins版本:2.542.2
-
我们使用gitee插件
![](https://i-blog.csdnimg.cn/blog_migrate/07eecb26aec0c89f2643769a099c4e02.png)
3.配置k3s构建
![](https://i-blog.csdnimg.cn/blog_migrate/a0b5fa029b790a122903512eb76e6b5a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/bffb354c293e7a52c89f3619edb636a0.png)
![](https://i-blog.csdnimg.cn/blog_migrate/4663868c6f0489d3feb66aa8ddda9afb.png)
Pod template settings
4.编写Jenkinsfile (声明式)(java)(注:钉钉通知只能用在声明式里,不能用在脚本式里)
pipeline{
agent {
kubernetes{
cloud 'kubernetes'
yaml '''apiVersion: "v1"
kind: "Pod"
metadata:
namespace: "default"
spec:
containers:
- command:
- "cat"
image: "maven:3.8.5-jdk-8"
imagePullPolicy: "IfNotPresent"
name: "maven"
tty: true
volumeMounts:
- mountPath: "/opt/maven/specialdisease"
name: "volume-1"
readOnly: false
- mountPath: "/root/.docker"
name: "volume-3"
readOnly: false
- mountPath: "/root/.m2"
name: "volume-2"
readOnly: false
- mountPath: "/var/run/docker.sock"
name: "volume-0"
readOnly: false
- mountPath: "/home/jenkins/agent"
name: "workspace-volume"
readOnly: false
- command:
- "cat"
image: "docker:latest"
imagePullPolicy: "IfNotPresent"
name: "dnd"
tty: true
volumeMounts:
- mountPath: "/opt/maven/specialdisease"
name: "volume-1"
readOnly: false
- mountPath: "/root/.docker"
name: "volume-3"
readOnly: false
- mountPath: "/root/.m2"
name: "volume-2"
readOnly: false
- mountPath: "/var/run/docker.sock"
name: "volume-0"
readOnly: false
- mountPath: "/home/jenkins/agent"
name: "workspace-volume"
readOnly: false
restartPolicy: "Never"
volumes:
- hostPath:
path: "/var/run/docker.sock"
name: "volume-0"
- hostPath:
path: "/opt/maven/.m2"
name: "volume-2"
- hostPath:
path: "/opt/maven/specialdisease"
name: "volume-1"
- emptyDir:
medium: ""
name: "workspace-volume"
- hostPath:
path: "/root/.docker"
name: "volume-3"
'''
}
}
stages {
stage('gitee checkout') {
steps{
git branch: "${env.giteeBranch}", credentialsId: 'gitee-kk', url: "${env.giteeSourceRepoHttpUrl}"
echo "giteeBranch ${env.giteeBranch}"
echo "giteeSourceBranch ${env.giteeSourceBranch}"
echo "giteeActionType ${env.giteeActionType}"
echo "giteeUserName ${env.giteeUserName}"
echo "giteeUserEmail ${env.giteeUserEmail}"
echo "giteeSourceRepoURL ${env.giteeSourceRepoURL}"
echo "giteeSourceRepoName ${env.giteeSourceRepoName}"
}
}
stage('maven build') {
steps {
container('maven') {
script {
if("${env.giteeBranch}" == 'dev' && "${env.giteeActionType}" == 'PUSH'){
sh 'mvn -e clean deploy --batch-mode -U -Dmaven.test.skip=true -s /opt/maven/specialdisease/settings.xml'
}else if("${env.giteeBranch}" == 'test' && "${env.giteeActionType}" == 'PUSH'){
sh 'sed -i "s/-SNAPSHOT/-Alpha/" pom.xml'
sh 'mvn -e clean deploy --batch-mode -U -Dmaven.test.skip=true -s /opt/maven/specialdisease/settings.xml'
}else if("${env.giteeBranch}" == 'master' && "${env.giteeActionType}" == 'PUSH'){
sh 'sed -i "s/-SNAPSHOT/-Beta/" pom.xml'
sh 'mvn -e clean deploy --batch-mode -U -Dmaven.test.skip=true -s /opt/maven/specialdisease/settings.xml'
}else if("${env.giteeBranch}" == 'releases' && "${env.giteeActionType}" == 'PUSH'){
sh 'sed -i "s/-SNAPSHOT/-RELEASES/" pom.xml'
sh 'mvn -e clean deploy --batch-mode -U -Dmaven.test.skip=true -s /opt/maven/specialdisease/settings.xml'
}
}
}
}
}
stage('docker build and push') {
steps {
container('dnd') {
script {
if("${env.giteeBranch}" == 'dev' || "${env.giteeBranch}" == 'test' || "${env.giteeBranch}" == 'master' || "${env.giteeBranch}" == 'releases'){
withCredentials([usernamePassword(credentialsId: 'harbor', passwordVariable: 'password', usernameVariable: 'username')]) {
sh 'echo "$password" | docker login -u $username --password-stdin harbor.xxxxxx.cn'
sh "docker build -t harbor.xxxxxx.cn/specialdisease/${env.giteeSourceRepoName}-${env.giteeBranch}:latest ."
sh "docker push harbor.xxxxxx.cn/specialdisease/${env.giteeSourceRepoName}-${env.giteeBranch}:latest"
}
}
}
}
}
}
stage('k8s-deploy') {
steps {
script {
def remote = [:]
def cert
def run_opts
def run_profile
remote.allowAnyHosts = true
remote.name = "test_remote"
remote.host = '192.168.31.61'
remote.port = 22
cert='master-61'
withCredentials([usernamePassword(credentialsId: cert, passwordVariable: 'password', usernameVariable: 'userName')]){
remote.user = "${userName}"
remote.password = "${password}"
}
if("${env.giteeBranch}" == 'test'){
sshCommand failOnError: false, remote: remote, command: "kubectl rollout restart deploy ${env.giteeSourceRepoName} -n specialdisease-test"
}else if ("${env.giteeBranch}" == 'dev'){
sshCommand failOnError: false, remote: remote, command: "kubectl rollout restart deploy ${env.giteeSourceRepoName} -n specialdisease-dev"
}
}
}
post {
failure {
dingtalk (robot:'20271ef1-6b9e-429c-9682-ab10dda25ddf',
type:'TEXT',
text: [
"构建失败",
"仓库: ${env.giteeSourceRepoName}",
"触发事件: ${env.giteeActionType}",
"目标分支: ${env.giteeBranch}",
"发起人: ${env.giteeUserName}",
"详情:${env.giteeSourceRepoURL}"
]
)
}
success {
dingtalk (robot:'20271ef1-6b9e-429c-9682-ab10dda25ddf',
type:'TEXT',
text: [
"构建成功",
"仓库: ${env.giteeSourceRepoName}",
"触发事件: ${env.giteeActionType}",
"目标分支: ${env.giteeBranch}",
"发起人: ${env.giteeUserName}",
"详情:${env.giteeSourceRepoURL}"
]
)
}
}
}
}
}
5.编写Jenkinsfile (脚本式)(java)
podTemplate(containers: [
containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', ttyEnabled: true, command: 'cat'),
containerTemplate(name: 'dnd', image: 'docker:latest', ttyEnabled: true, command: 'cat')
],
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock',hostPath: '/var/run/docker.sock'),
hostPathVolume(mountPath: '/opt/maven/specialdisease', hostPath: '/opt/maven/specialdisease'),
hostPathVolume(mountPath: '/root/.m2', hostPath: '/opt/maven/.m2'),
hostPathVolume(mountPath: '/root/.docker', hostPath: '/root/.docker')
])
{
node(POD_LABEL) {
stage('gitee checkout') {
git branch: "${env.giteeBranch}", credentialsId: 'gitee-kk', url: "${env.giteeSourceRepoHttpUrl}"
echo "giteeBranch ${env.giteeBranch}"
echo "giteeSourceBranch ${env.giteeSourceBranch}"
echo "giteeActionType ${env.giteeActionType}"
echo "giteeUserName ${env.giteeUserName}"
echo "giteeUserEmail ${env.giteeUserEmail}"
echo "giteeSourceRepoURL ${env.giteeSourceRepoURL}"
echo "giteeSourceRepoName ${env.giteeSourceRepoName}"
}
stage('maven build') {
container('maven') {
script {
if("${env.giteeBranch}" == 'dev' && "${env.giteeActionType}" == 'PUSH'){
sh 'mvn -e clean deploy --batch-mode -U -Dmaven.test.skip=true -s /opt/maven/specialdisease/settings.xml'
}else if("${env.giteeBranch}" == 'test' && "${env.giteeActionType}" == 'PUSH'){
sh 'sed -i "s/-SNAPSHOT/-Alpha/" pom.xml'
sh 'mvn -e clean deploy --batch-mode -U -Dmaven.test.skip=true -s /opt/maven/specialdisease/settings.xml'
}else if("${env.giteeBranch}" == 'master' && "${env.giteeActionType}" == 'PUSH'){
sh 'sed -i "s/-SNAPSHOT/-Beta/" pom.xml'
sh 'mvn -e clean deploy --batch-mode -U -Dmaven.test.skip=true -s /opt/maven/specialdisease/settings.xml'
}else if("${env.giteeBranch}" == 'releases' && "${env.giteeActionType}" == 'PUSH'){
sh 'sed -i "s/-SNAPSHOT/-RELEASES/" pom.xml'
sh 'mvn -e clean deploy --batch-mode -U -Dmaven.test.skip=true -s /opt/maven/specialdisease/settings.xml'
}
}
}
}
stage('docker build and push') {
container('dnd') {
script {
if("${env.giteeBranch}" == 'dev' || "${env.giteeBranch}" == 'test' || "${env.giteeBranch}" == 'master' || "${env.giteeBranch}" == 'releases'){
withCredentials([usernamePassword(credentialsId: 'harbor', passwordVariable: 'password', usernameVariable: 'username')]) {
sh 'echo "$password" | docker login -u $username --password-stdin harbor.xxxxxx.cn'
sh "docker build -t harbor.xxxxxx.cn/specialdisease/${env.giteeSourceRepoName}-${env.giteeBranch}:latest ."
sh "docker push harbor.xxxxxx.cn/specialdisease/${env.giteeSourceRepoName}-${env.giteeBranch}:latest"
}
}
}
}
}
stage('k8s-deploy') {
script {
def remote = [:]
def cert
def run_opts
def run_profile
remote.allowAnyHosts = true
remote.name = "test_remote"
remote.host = '192.168.31.61'
remote.port = 22
cert='master-61'
withCredentials([usernamePassword(credentialsId: cert, passwordVariable: 'password', usernameVariable: 'userName')]){
remote.user = "${userName}"
remote.password = "${password}"
}
if("${env.giteeBranch}" == 'test'){
sshCommand failOnError: false, remote: remote, command: "kubectl rollout restart deploy ${env.giteeSourceRepoName} -n specialdisease-test"
}else if ("${env.giteeBranch}" == 'dev'){
sshCommand failOnError: false, remote: remote, command: "kubectl rollout restart deploy ${env.giteeSourceRepoName} -n specialdisease-dev"
}
}
post {
failure {
dingtalk (robot:'20271ef1-6b9e-429c-9682-ab10dda25ddf',
type:'TEXT',
text: [
"构建失败",
// "仓库: ${REPO_NAME}",
// "触发事件: ${env.giteeActionType}",
// "目标分支: ${env.giteeBranch}",
// "发起人: ${env.giteeUserName}",
// "详情:${env.BUILD_URL}"
]
)
}
success {
dingtalk (robot:'20271ef1-6b9e-429c-9682-ab10dda25ddf',
type:'TEXT',
text: [
"构建成功",
// "仓库: ${REPO_NAME}",
// "触发事件: ${env.giteeActionType}",
// "目标分支: ${env.giteeBranch}",
// "发起人: ${env.giteeUserName}",
// "详情:${env.BUILD_URL}"
]
)
}
}
}
}
}
6.编写Jenkinsfile (声明式)(nodejs)
pipeline{
agent {
kubernetes{
cloud 'kubernetes'
yaml '''apiVersion: "v1"
kind: "Pod"
metadata:
namespace: "default"
spec:
containers:
- command:
- "cat"
image: "node:16.17.1"
imagePullPolicy: "IfNotPresent"
name: "node"
tty: true
volumeMounts:
- mountPath: "/root/.docker"
name: "volume-3"
readOnly: false
- mountPath: "/var/run/docker.sock"
name: "volume-0"
readOnly: false
- mountPath: "/home/jenkins/agent"
name: "workspace-volume"
readOnly: false
- command:
- "cat"
image: "docker:latest"
imagePullPolicy: "IfNotPresent"
name: "dnd"
tty: true
volumeMounts:
- mountPath: "/root/.docker"
name: "volume-3"
readOnly: false
- mountPath: "/var/run/docker.sock"
name: "volume-0"
readOnly: false
- mountPath: "/home/jenkins/agent"
name: "workspace-volume"
readOnly: false
restartPolicy: "Never"
volumes:
- hostPath:
path: "/var/run/docker.sock"
name: "volume-0"
- emptyDir:
medium: ""
name: "workspace-volume"
- hostPath:
path: "/root/.docker"
name: "volume-3"
'''
}
}
stages {
stage('gitee checkout') {
steps{
git branch: "${env.giteeBranch}", credentialsId: 'gitee-kk', url: "${env.giteeSourceRepoHttpUrl}"
echo "giteeBranch ${env.giteeBranch}"
echo "giteeSourceBranch ${env.giteeSourceBranch}"
echo "giteeActionType ${env.giteeActionType}"
echo "giteeUserName ${env.giteeUserName}"
echo "giteeUserEmail ${env.giteeUserEmail}"
echo "giteeSourceRepoName ${env.giteeSourceRepoName}"
echo "giteeSourceRepoURL ${env.giteeSourceRepoURL}"
echo "giteeSourceRepoHttpUrl ${env.giteeSourceRepoHttpUrl}"
}
}
stage('npm build') {
steps {
container('node') {
sh 'npm install'
script {
if("${env.giteeBranch}" == 'dev' && "${env.giteeActionType}" == 'PUSH'){
sh 'npm run sit'
}else if("${env.giteeBranch}" == 'test' || "${env.giteeBranch}" == 'test-drug' || "${env.giteeBranch}" == 'test-settle'){
sh 'npm run test'
}else if("${env.giteeBranch}" == 'releases' && "${env.giteeActionType}" == 'PUSH'){
sh 'npm run build'
}
}
}
}
}
stage('docker build and push') {
steps {
container('dnd') {
withCredentials([usernamePassword(credentialsId: 'harbor', passwordVariable: 'password', usernameVariable: 'username')]) {
sh 'echo "$password" | docker login -u $username --password-stdin harbor.xxxxxx.cn'
sh "docker build -t harbor.xxxxxx.cn/specialdisease/${env.giteeSourceRepoName}-${env.giteeBranch}:latest ."
sh "docker push harbor.xxxxxx.cn/specialdisease/${env.giteeSourceRepoName}-${env.giteeBranch}:latest"
}
}
}
}
stage('k8s-deploy') {
steps {
script {
def remote = [:]
def cert
def run_opts
def run_profile
remote.allowAnyHosts = true
remote.name = "test_remote"
remote.host = '192.168.31.61'
remote.port = 22
cert='master-61'
withCredentials([usernamePassword(credentialsId: cert, passwordVariable: 'password', usernameVariable: 'userName')]){
remote.user = "${userName}"
remote.password = "${password}"
}
if("${env.giteeBranch}" == 'test' || "${env.giteeBranch}" == 'test-drug' || "${env.giteeBranch}" == 'test-settle'){
sshCommand failOnError: false, remote: remote, command: "kubectl rollout restart deploy specialdisease-${env.giteeSourceRepoName} -n specialdisease-test"
}else if ("${env.giteeBranch}" == 'dev'){
sshCommand failOnError: false, remote: remote, command: "kubectl rollout restart deploy specialdisease-${env.giteeSourceRepoName} -n specialdisease-dev"
}
}
}
post {
failure {
dingtalk (robot:'20271ef1-6b9e-429c-9682-ab10dda25ddf',
type:'TEXT',
text: [
"构建失败",
"仓库: ${env.giteeSourceRepoName}",
"触发事件: ${env.giteeActionType}",
"目标分支: ${env.giteeBranch}",
"发起人: ${env.giteeUserName}",
"详情:${env.giteeSourceRepoURL}"
]
)
}
success {
dingtalk (robot:'20271ef1-6b9e-429c-9682-ab10dda25ddf',
type:'TEXT',
text: [
"构建成功",
"仓库: ${env.giteeSourceRepoName}",
"触发事件: ${env.giteeActionType}",
"目标分支: ${env.giteeBranch}",
"发起人: ${env.giteeUserName}",
"详情:${env.giteeSourceRepoURL}"
]
)
}
}
}
}
}
7.编写Jenkinsfile (脚本式)(nodejs)
podTemplate(containers: [
containerTemplate(name: 'node', image: 'node:14', ttyEnabled: true, command: 'cat'),
containerTemplate(name: 'dnd', image: 'docker:latest', ttyEnabled: true, command: 'cat')
],
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock',hostPath: '/var/run/docker.sock'),
hostPathVolume(mountPath: '/root/.docker', hostPath: '/root/.docker')
])
{
node(POD_LABEL) {
stage('gitee checkout') {
git branch: "${env.giteeBranch}", credentialsId: 'gitee-kk', url: "${env.giteeSourceRepoHttpUrl}"
echo "giteeBranch ${env.giteeBranch}"
echo "giteeSourceBranch ${env.giteeSourceBranch}"
echo "giteeActionType ${env.giteeActionType}"
echo "giteeUserName ${env.giteeUserName}"
echo "giteeUserEmail ${env.giteeUserEmail}"
echo "giteeSourceRepoName ${env.giteeSourceRepoName}"
echo "giteeSourceRepoURL ${env.giteeSourceRepoURL}"
echo "giteeSourceRepoHttpUrl ${env.giteeSourceRepoHttpUrl}"
}
stage('npm build') {
container('node') {
sh 'npm install -g cnpm --registry=https://registry.npmmirror.com'
sh 'cnpm -v'
sh 'npm install'
script {
if("${env.giteeBranch}" == 'dev' && "${env.giteeActionType}" == 'PUSH'){
sh 'cnpm run sit'
}else if("${env.giteeBranch}" == 'test' && "${env.giteeActionType}" == 'PUSH'){
sh 'npm run test'
}else if("${env.giteeBranch}" == 'master' && "${env.giteeActionType}" == 'PUSH'){
sh 'npm run build'
}
}
}
}
stage('docker build and push') {
container('dnd') {
withCredentials([usernamePassword(credentialsId: 'harbor', passwordVariable: 'password', usernameVariable: 'username')]) {
sh 'echo "$password" | docker login -u $username --password-stdin harbor.xxxxxx.cn'
sh "docker build -t harbor.xxxxxx.cn/drug/${env.giteeSourceRepoName}-${env.giteeBranch}:latest ."
sh "docker push harbor.xxxxxx.cn/drug/${env.giteeSourceRepoName}-${env.giteeBranch}:latest"
}
}
}
// stage('deploy') {
// script {
// def remote = [:]
// def cert
// def run_opts
// def run_profile
// remote.allowAnyHosts = true
// if("${env.giteeBranch}" == 'dev' && "${env.giteeActionType}" == 'PUSH'){
// remote.name = "dev_remote"
// remote.host = '192.168.31.39'
// remote.port = 22
// cert='dev-39'
// }else if("${env.giteeBranch}" == 'test' && "${env.giteeActionType}" == 'PUSH'){
// remote.name = "test_remote"
// remote.host = '192.168.31.40'
// remote.port = 22
// cert='dev-39'
// }
// if("${env.giteeBranch}" == 'dev' || "${env.giteeBranch}" == 'test' ){
// withCredentials([usernamePassword(credentialsId: cert, passwordVariable: 'password', usernameVariable: 'userName')]){
// remote.user = "${userName}"
// remote.password = "${password}"
// }
// sshCommand failOnError: false, remote: remote, command: "docker stop ${env.giteeSourceRepoName}"
// sshCommand failOnError: false, remote: remote, command: "docker rm ${env.giteeSourceRepoName}"
// sshCommand failOnError: false, remote: remote, command: "docker rmi harbor.xxxxxx.cn/drug/${env.giteeSourceRepoName}-${env.giteeBranch}"
// sshCommand failOnError: false, remote: remote, command: "docker run -d --name ${env.giteeSourceRepoName} -p 8082:80 harbor.xxxxxx.cn/drug/${env.giteeSourceRepoName}-${env.giteeBranch}"
// }
// }
// }
stage('k8s-deploy') {
script {
def remote = [:]
def cert
def run_opts
def run_profile
remote.allowAnyHosts = true
remote.name = "test_remote"
remote.host = '192.168.31.61'
remote.port = 22
cert='master-61'
withCredentials([usernamePassword(credentialsId: cert, passwordVariable: 'password', usernameVariable: 'userName')]){
remote.user = "${userName}"
remote.password = "${password}"
}
if("${env.giteeBranch}" == 'test'){
sshCommand failOnError: false, remote: remote, command: "kubectl rollout restart deploy drug-patient-h5 -n drug-test"
}else if ("${env.giteeBranch}" == 'dev'){
sshCommand failOnError: false, remote: remote, command: "kubectl rollout restart deploy drug-patient-h5 -n drug-dev"
}
}
}
}
}