- jenkins打包,直接使用nginx镜像/node镜像
1.1 piplinepipeline { agent any options { timestamps() } environment { CRET_ID = '7a52' IMAGE_NAME = "harbor.xxx.xxx/xxx-pip/{JOB_NAME}-{PROJECT_ENV}" NAMESPACES = 'java-{PROJECT_ENV}' } stages { stage('清除工作空间') { steps { cleanWs() } } stage('Node build') { steps { script { sh "npm install --registry=https://registry.npmmirror.com/ --unsafe-perm " sh "npm run build" } } } stage('gitlab pull') { steps { sh "echo{IMAGE_NAME}" echo '正在拉取代码...' checkout([class: 'GitSCM', branches: [[name: Git_Tag]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[ credentialsId: '7a52b90e', url: Git_URL]] ]) } } stage('docker build') { steps { script { if ( PROJECT_ENV == 'prod' ) { IMAGE_NAME = "registry.cn.aliyuncs.com/xxx-pip/{JOB_NAME}-{PROJECT_ENV}" } sh "docker rmi{IMAGE_NAME}:`expr {BUILD_ID} - 2` || true" sh "docker build -t{IMAGE_NAME}:{BUILD_ID} ." sh "docker tag{IMAGE_NAME}:{BUILD_ID}{IMAGE_NAME}:latest" } } } stage('docker push') { steps { script { if ( PROJECT_ENV == 'prod' ) { IMAGE_NAME = "registry.cn.aliyuncs.com/xxx-pip/{JOB_NAME}-{PROJECT_ENV}" } sh "docker push {IMAGE_NAME}:{BUILD_ID} && docker push {IMAGE_NAME}:latest" } } } stage('kube apply') { steps { script{ if ( PROJECT_ENV == 'test' ) { sh "kubectl set image deploy{JOB_NAME} -n {NAMESPACES}{JOB_NAME}={IMAGE_NAME}:{BUILD_ID} --record" sh "kubedog rollout track -n {NAMESPACES} deployment{JOB_NAME}" }else{ IMAGE_NAME = "registry-vpc.cn.aliyuncs.com/xxx-pip/{JOB_NAME}-{PROJECT_ENV}" sh "kubectl --kubeconfig /root/.kube/config.xxx set image deploy {JOB_NAME} -n{NAMESPACES} {JOB_NAME}={IMAGE_NAME}:{BUILD_ID} --record" sh "kubedog --kube-config /root/.kube/config.xxx rollout track -n{NAMESPACES} deployment ${JOB_NAME}" } } } } } }
1.2 Dockerfile(使用Nginx)
FROM nginx LABEL maintainer="研发组 <[email protected]>" COPY ./dist/ /usr/share/nginx/html/ RUN chmod -R 777 /usr/share/nginx/html/ RUN chmod -R 777 /etc/nginx/conf.d/default.conf CMD ["nginx", "-g", "daemon off;"]
1.3 Dockerfile (使用node镜像)
FROM node:8.10.0-alpine ENV TZ=Asia/Shanghai RUN ln -snf /usr/share/zoneinfo/TZ /etc/localtime && echoTZ > /etc/timezone WORKDIR /app ADD . /app/ RUN npm i docsify-cli -g --registry=https://registry.npmmirror.com RUN docsify init ./ CMD ["docsify","serve","./"]
- 使用node镜像+Nginx镜像
2.1 pipline
pipeline { agent any options { timestamps() } environment { CRET_ID = '7a52' IMAGE_NAME = "harbor.xxx.xxx/xxx-pip/{JOB_NAME}-{PROJECT_ENV}" NAMESPACES = 'java-{PROJECT_ENV}' } stages { stage('清除工作空间') { steps { cleanWs() } } stage('gitlab pull') { steps { sh "echo{IMAGE_NAME}" echo '正在拉取代码...' checkout([class: 'GitSCM', branches: [[name: Git_Tag]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[ credentialsId: '7a52b90e', url: Git_URL]] ]) } } stage('docker build') { steps { script { if ( PROJECT_ENV == 'prod' ) { IMAGE_NAME = "registry.cn.aliyuncs.com/xxx-pip/{JOB_NAME}-{PROJECT_ENV}" } sh "docker rmi{IMAGE_NAME}:`expr {BUILD_ID} - 2` || true" sh "docker build -t{IMAGE_NAME}:{BUILD_ID} ." sh "docker tag{IMAGE_NAME}:{BUILD_ID}{IMAGE_NAME}:latest" } } } stage('docker push') { steps { script { if ( PROJECT_ENV == 'prod' ) { IMAGE_NAME = "registry.cn.aliyuncs.com/xxx-pip/{JOB_NAME}-{PROJECT_ENV}" } sh "docker push {IMAGE_NAME}:{BUILD_ID} && docker push {IMAGE_NAME}:latest" } } } stage('kube apply') { steps { script{ if ( PROJECT_ENV == 'test' ) { sh "kubectl set image deploy{JOB_NAME} -n {NAMESPACES}{JOB_NAME}={IMAGE_NAME}:{BUILD_ID} --record" sh "kubedog rollout track -n {NAMESPACES} deployment{JOB_NAME}" }else{ IMAGE_NAME = "registry-vpc.cn.aliyuncs.com/xxx-pip/{JOB_NAME}-{PROJECT_ENV}" sh "kubectl --kubeconfig /root/.kube/config.xxx set image deploy {JOB_NAME} -n{NAMESPACES} {JOB_NAME}={IMAGE_NAME}:{BUILD_ID} --record" sh "kubedog --kube-config /root/.kube/config.xxx rollout track -n{NAMESPACES} deployment ${JOB_NAME}" } } } } } }
2.2 Dockerfile
FROM node:18.20.2 as build COPY . /app WORKDIR /app ARG PUBLIC_PATH=/ ARG MODE RUN npm -v RUN npm install --registry=https://registry.npmmirror.com/ --unsafe-perm && npm run build FROM nginx:alpine ARG PUBLIC_PATH=/ COPY --from=build /app/build/ /usr/share/nginx/html${PUBLIC_PATH} EXPOSE 80