Pipline:

pipeline {

agent {

kubernetes {

inheritFrom 'nodejs base'

containerTemplate {

name 'nodejs'

image 'node:14.19.0'

}

}

parameters {

string(name: 'PROJECT_VERSION', defaultValue: 'v1.0', description: '')

string(name: 'PROJECT_NAME', defaultValue: '', description: '')

}

}

stages {

stage('代码拉取') {

agent none

steps {

container('nodejs') {

git(url: 'https://.git', credentialsId: 'gitlab-id', branch: 'master', changelog: true, poll: false)

}

}

}

stage('run npm install') {

agent none

steps {

container('nodejs') {

sh 'npm config set registry https://registry.npm.taobao.org'

sh 'npm install'

}

}

}

stage('项目编译') {

agent none

steps {

container('nodejs') {

sh 'npm run build:prod'

}

}

}

stage('构建镜像') {

agent none

steps {

container('base') {

sh 'docker build -t $IMAGES:$PROJECT_VERSION -f Dockerfile .'

}

}

}

stage('推送镜像') {

agent none

steps {

container('base') {

withCredentials([usernamePassword(credentialsId : 'huaweiswr-id' ,passwordVariable : 'HUAWEISWR_PASSWORD' ,usernameVariable : 'HUAWEISWR_USERNAME' ,)]) {

sh 'echo "$HUAWEISWR_PASSWORD" | docker login $REGISTRY -u "$HUAWEISWR_USERNAME" --password-stdin'

sh 'docker tag $IMAGES:$PROJECT_VERSION $REGISTRY/$HUAWEISWR_NAMESPACE/$IMAGES:$PROJECT_VERSION'

sh 'docker push $REGISTRY/$HUAWEISWR_NAMESPACE/$IMAGES:$PROJECT_VERSION'

}

}

}

}

stage('部署到生产环境') {

agent none

steps {

container('base') {

withCredentials([kubeconfigFile(credentialsId : env.KUBECONFIG_CREDENTIAL_ID ,variable : 'KUBECONFIG' ,)]) {

sh 'envsubst < deploy.yaml | kubectl apply -f -'

sh 'docker rmi -f $IMAGES:$PROJECT_VERSION'

}

}

}

}

stage('检查状态') {

agent none

steps {

container('base') {

withCredentials([kubeconfigFile(credentialsId : env.KUBECONFIG_CREDENTIAL_ID ,variable : 'KUBECONFIG' ,)]) {

sh 'kubectl rollout status deployment $IMAGES -n $DEPLOYMENT_NAMESPACE'

}

}

}

}

}

environment {

REGISTRY = ''

SWR_CREDENTIAL_ID = 'huaweiswr-id'

HUAWEISWR_NAMESPACE = ''

KUBECONFIG_CREDENTIAL_ID = ''

JAR_PORD = '80'

IMAGES = ''

DEPLOYMENT_NAMESPACE = ''

}

}

Dockerfile:

FROM nginx
COPY dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
 

deploymen.yaml

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: $IMAGES
  name: $IMAGES
  namespace: $DEPLOYMENT_NAMESPACE
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  selector:
    matchLabels:
      app: $IMAGES
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 25%
  template:
    metadata:
      labels:
        app: $IMAGES
    spec:
      imagePullSecrets:
        - name: huaweiswr-secret
      containers:
        - image: '$REGISTRY/$HUAWEISWR_NAMESPACE/$IMAGES:$PROJECT_VERSION'
          name: app
          ports:
            - containerPort: $JAR_PORD
              protocol: TCP
          resources:
            limits:
              cpu: '0.3'
              memory: 500Mi
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
---
kind: Service
apiVersion: v1
metadata:
  name: $IMAGES
  namespace: $DEPLOYMENT_NAMESPACE
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80  
  selector:
    app: $IMAGES
  type: ClusterIP
 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐