node-service/Jenkinsfile

67 lines
1.9 KiB
Plaintext
Raw Normal View History

2024-09-02 09:36:31 +08:00
pipeline {
agent any
environment {
// 镜像名称
IMAGE_NAME = "node-service"
// 工作目录
WS = "${WORKSPACE}"
// 自定义构建参数
PROFILE = "prod"
// docker网络
DOCKER_NETWORK = "inspur"
}
// 定义流水线的加工流程
stages {
// 流水线的所有阶段
stage('1.环境检查与初始化') {
steps {
sh 'pwd && ls -alh'
sh 'printenv'
sh 'docker version'
sh 'git --version'
}
}
stage('2.编译') {
agent {
docker {
image 'node:20-slim'
}
}
steps {
sh 'pwd && ls -alh'
sh 'node -v'
2024-09-02 10:44:02 +08:00
sh 'cd ${WS} && npm config set registry https://registry.npmmirror.com && yarn -v'
2024-09-02 09:36:31 +08:00
sh 'yarn config set registry https://registry.npmmirror.com && yarn'
}
}
stage('3.打包') {
2024-09-02 10:54:30 +08:00
agent {
docker {
image 'node:20-slim'
}
}
2024-09-02 09:36:31 +08:00
steps {
sh 'pwd && ls -alh'
2024-09-02 10:47:23 +08:00
sh 'node -v'
sh 'npm run build:webpack'
2024-09-02 11:09:08 +08:00
sh 'docker build --build-arg PROFILE=${PROFILE} -t ${IMAGE_NAME} .'
2024-09-02 09:36:31 +08:00
}
}
stage('4.部署') {
// 删除容器和虚悬镜像
steps {
sh 'pwd && ls -alh'
sh 'docker rm -f ${IMAGE_NAME} || true && docker rmi $(docker images -q -f dangling=true) || true'
sh 'docker network list | grep "${DOCKER_NETWORK}" && echo "docker network ${DOCKER_NETWORK} is exist" || docker network create ${DOCKER_NETWORK}'
sh 'docker run -d --name ${IMAGE_NAME} --network ${DOCKER_NETWORK} ${IMAGE_NAME}'
}
}
}
}