Jenkinsfile
版本1
def POD_LABEL = "vue-pod-${UUID.randomUUID().toString()}"
podTemplate (
label: POD_LABEL, cloud: 'kubernetes',
containers: [
containerTemplate(name: 'jnlp', image: 'jenkins/jnlp-agent-docker', args: '${computer.jnlpmac} ${computer.name}'),
containerTemplate(name: 'nodejs', image: "node:14-alpine", ttyEnabled: true, command: 'cat')
],
volumes: [
hostPathVolume(hostPath: '/var/run/docker.sock', mountPath:'/var/run/docker.sock')
]
) {
node(POD_LABEL) {
stage('code-checkout') {
container('nodejs') {
checkout scm
}
// script {
// build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
// }
def packageJSON = readJSON file: 'package.json'
build_tag = packageJSON.version
}
stage('code-build') {
container('nodejs') {
// sh 'npm config set registry https://registry.npm.taobao.org'
sh "npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/"
sh 'npm install --unsafe-perm --registry=https://registry.npm.taobao.org'
sh 'npm run build'
}
}
stage('docker-build') {
sh "docker build -t 192.168.1.207:6543/com.newpwr/genoany-web:${build_tag} ."
}
stage('docker-push') {
withCredentials([usernamePassword(credentialsId: 'localDocker', passwordVariable: 'localDockerPassword', usernameVariable: 'localDockerUser')]) {
sh "docker login 192.168.1.207:6543 -u ${localDockerUser} -p ${localDockerPassword}"
sh "docker push 192.168.1.207:6543/test/test-web:${build_tag}"
}
}
stage('deploy') {
// if (env.BRANCH_NAME == 'master') {
// input "是否部署到线上环境?"
// }
input "是否部署到线上环境?"
}
}
}
版本2
def POD_LABEL = "jnlp-agent-genoany-web-${UUID.randomUUID().toString()}"
def BUILD_VERSION = ''
podTemplate (
label: POD_LABEL, cloud: 'kubernetes',
containers: [
containerTemplate(name: 'jnlp', image: 'jenkins/jnlp-agent-docker', args: '${computer.jnlpmac} ${computer.name}'),
containerTemplate(name: 'nodejs', image: "node:14-alpine", ttyEnabled: true, command: 'cat')
],
volumes: [
hostPathVolume(hostPath: '/var/run/docker.sock', mountPath:'/var/run/docker.sock')
]
) {
node(POD_LABEL) {
stage('code-checkout') {
container('nodejs') {
checkout scm
}
}
stage('build-setting') {
def packageJSON = readJSON file: 'package.json'
userInput = input(
id: 'userInput', message: '构建设置', parameters: [
[$class:'TextParameterDefinition', defaultValue: "${packageJSON.version}", description: '版本号', name: 'version'],
[$class:'ChoiceParameterDefinition',defaultValue:"NO", choices: "NO\nYES", description:"立即部署",name: 'deploy']
])
BUILD_VERSION = userInput['version']
CAN_DEPLOY = userInput['deploy']
echo "填入的版本号为:${BUILD_VERSION}"
echo "立即部署:${CAN_DEPLOY}"
}
stage('code-build') {
container('nodejs') {
sh "npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/"
sh 'npm install --unsafe-perm --registry=https://registry.npm.taobao.org'
sh 'npm run build'
}
}
stage('docker-build') {
sh "docker build -t 192.168.1.207:6543/com.newpwr/genoany-web:${BUILD_VERSION} ."
}
stage('docker-push') {
withCredentials([usernamePassword(credentialsId: 'localDocker', passwordVariable: 'localDockerPassword', usernameVariable: 'localDockerUser')]) {
sh "docker login 192.168.1.207:6543 -u ${localDockerUser} -p ${localDockerPassword}"
sh "docker push 192.168.1.207:6543/com.newpwr/genoany-web:${BUILD_VERSION}"
}
}
stage('deploy') {
if(CAN_DEPLOY == "YES"){
}
}
}
}
版本3
def POD_LABEL = "jnlp-agent-genoany-web-${UUID.randomUUID().toString()}"
def BUILD_VERSION = ''
def IS_SUCCESS = true
properties(
[
parameters(
[
booleanParam(defaultValue: true, description: '是否部署到研发环境', name: 'DEPLOY_DEV'),
booleanParam(defaultValue: false, description: '是否部署到产品开发环境', name: 'DEPLOY_PRO_DEV')
]
)
]
)
podTemplate (
label: POD_LABEL, cloud: 'kubernetes',
containers: [
containerTemplate(name: 'jnlp', image: 'jenkins/jnlp-agent-docker', args: '${computer.jnlpmac} ${computer.name}'),
// containerTemplate(name: 'sonarscanner', image: 'sonarsource/sonar-scanner-cli:latest', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'nodejs', image: "node:14-alpine", ttyEnabled: true, command: 'cat')
],
volumes: [
hostPathVolume(hostPath: '/var/run/docker.sock', mountPath:'/var/run/docker.sock')
]
) {
node(POD_LABEL) {
try{
stage('code-checkout') {
container('nodejs') {
checkout scm
}
build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
}
// stage('code-sonar') {
// container('sonarscanner') {
// echo "代码质量分析"
// sh """
// sonar-scanner \
// -Dsonar.projectKey=newpwrplatform.cloud \
// -Dsonar.sources=src \
// -Dsonar.language=js \
// -Dsonar.sourceEncoding=UTF-8 \
// -Dsonar.host.url=http://sonarqube:9000 \
// -Dsonar.login=5b09781993c58c204a3c4f26eaf3495ea4bf40e0
// """
// }
// dingtalk (
// robot: '9dd3f2d9-aae0-4145-bd64-fc1701d4a4a8',
// type: 'MARKDOWN',
// title: '你有新的消息,请注意查收',
// text: [
// "# [${env.JOB_NAME}](${env.JOB_URL})",
// '',
// "- 任务: [#${env.BUILD_NUMBER}](${env.BUILD_URL})",
// "- 分支: ${build_tag}",
// "- 状态: 代码质量分析完毕",
// "- 结果: [http://192.168.1.36:30910/dashboard?id=newpwrplatform.cloud](http://192.168.1.36:30910/dashboard?id=newpwrplatform.cloud)"
// ],
// at: []
// )
// }
stage('build-setting') {
// def packageJSON = readJSON file: 'package.json'
BUILD_VERSION="1.5.1."+"${env.BUILD_NUMBER}"
echo "版本号为:${BUILD_VERSION}"
def timeStamp = Calendar.getInstance().getTime().format('yyyy-MM-dd HH:mm:ss',TimeZone.getTimeZone('Asia/Shanghai'))
// def time=new Date().format('yyyy-MM-dd HH:mm:ss')
def versionJSON = readJSON file: 'public/version.json'
versionJSON.version = BUILD_VERSION
versionJSON.publishTime = timeStamp
writeJSON file: 'public/version.json', json: versionJSON
currentBuild.displayName = "v${BUILD_VERSION}"
// userInput = input(
// id: 'userInput', message: '构建设置', parameters: [
// [$class:'ChoiceParameterDefinition', choices: "NO\nYES", description:"立即部署",name: 'deploy']
// ])
CAN_DEPLOY = "YES"
echo "立即部署:${CAN_DEPLOY}"
dingtalk (
robot: '9dd3f2d9-aae0-4145-bd64-fc1701d4a4a8',
type: 'MARKDOWN',
title: '你有新的消息,请注意查收',
text: [
"# [${env.JOB_NAME}](${env.JOB_URL})",
'',
"- 任务: [#${env.BUILD_NUMBER}](${env.BUILD_URL})",
"- 分支: ${build_tag}",
"- 状态: 构建中",
"- 版本: ${BUILD_VERSION}",
"- 是否立即发布: ${CAN_DEPLOY}",
],
at: []
)
}
stage('code-build') {
container('nodejs') {
sh "npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/"
sh 'npm install genoany-g6-v2'
sh 'npm install --unsafe-perm --registry=https://registry.npm.taobao.org'
sh 'npm run build'
}
}
stage('docker-build') {
sh "docker build -t 192.168.1.207:6543/com.newpwr/genoany-web:${BUILD_VERSION} ."
}
stage('docker-push') {
withCredentials([usernamePassword(credentialsId: 'localDocker', passwordVariable: 'localDockerPassword', usernameVariable: 'localDockerUser')]) {
sh "docker login 192.168.1.207:6543 -u ${localDockerUser} -p ${localDockerPassword}"
sh "docker push 192.168.1.207:6543/com.newpwr/genoany-web:${BUILD_VERSION}"
}
}
stage('deploy') {
if(Boolean.valueOf(DEPLOY_DEV)){
}
if(Boolean.valueOf(DEPLOY_PRO_DEV)){
}
}
}catch(Exception ex){
IS_SUCCESS= false
throw ex
} finally {
if(IS_SUCCESS){
dingtalk (
robot: '9dd3f2d9-aae0-4145-bd64-fc1701d4a4a8',
type: 'MARKDOWN',
title: '你有新的消息,请注意查收',
text: [
"# [${env.JOB_NAME}](${env.JOB_URL})",
'',
"- 任务: [#${env.BUILD_NUMBER}](${env.BUILD_URL})",
"- 分支: ${build_tag}",
"- 状态: <font color=green>成功</font>",
"- 版本: ${BUILD_VERSION}",
"- 是否立即发布: ${CAN_DEPLOY}",
],
at: []
)
} else {
dingtalk (
robot: '9dd3f2d9-aae0-4145-bd64-fc1701d4a4a8',
type: 'MARKDOWN',
title: '你有新的消息,请注意查收',
text: [
"# [${env.JOB_NAME}](${env.JOB_URL})",
'',
"- 任务: [#${env.BUILD_NUMBER}](${env.BUILD_URL})",
"- 分支: ${build_tag}",
"- 状态: <font color=red>失败</font>",
"- 版本: ${BUILD_VERSION}",
"- 是否立即发布: ${CAN_DEPLOY}",
],
at: []
)
}
}
}
}
读取package.json文件里的版本信息
readJSON需要jenkins插件 Pipeline Utility Steps
def packageJSON = readJSON file: 'package.json'
def packageJSONVersion = packageJSON.version
echo packageJSONVersion
sh 'VERSION=${packageJSONVersion}_${BUILD_NUMBER}_${BRANCH_NAME} npm run build'
npm-pv-pvc.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: npm-pv
spec:
capacity:
storage: 20Gi
accessModes:
- ReadWriteMany
nfs:
server: 192.168.1.36
path: "/nfs/npm"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: npm-pvc
namespace: devops
spec:
accessModes: ["ReadWriteMany"]
resources:
requests:
storage: 20Gi