tiger项目Jenkins前端项目pipeline

pipeline {
    agent any
	
	environment {
        dockerhub_credentials = credentials('ea8728ad-b76d-461a-aa86-4633d919baac')
		K8S_CONFIG = credentials('df50c584-6739-4bd4-ba4a-53e322729f04')
		image_tag = sh(returnStdout: true,script: 'echo `date +"%Y%m%d%H%M"_``git describe --tags --always`').trim()
    }
	
    parameters {
        string(name: 'project_name', defaultValue: 'platform-web', description: '项目名称')
		string(name: 'replicas', defaultValue: '1', description: '项目副本数')
        string(name: 'git_url', defaultValue: 'http://git.xxx.com/Tiger/operating-platform-web.git', description: '仓库地址')
        string(name: 'branch_name', defaultValue: 'dev_cj', description: 'git分支')
        string(name: 'profiles', defaultValue: 'prod', description: '环境')
    }
    stages {
        stage('检出代码') {
        	steps{
                // 检出代码
            	checkout([$class: 'GitSCM', branches: [[name: "*/${params.branch_name}"]], 
            	doGenerateSubmoduleConfigurations: false, 
            	extensions: [], 
            	submoduleCfg: [], 
                userRemoteConfigs: [[
                    credentialsId: 'd2183ac7-b4a7-4ef5-86ce-22190ae33526', 
                	url: "${params.git_url}"]]])
            }
        }
        stage('生成静态文件') {
            steps {
                sh "node -v"
                sh "npm -v"
                sh "yarn install"
                sh "cd ${env.WORKSPACE} && npm run build:${params.profiles}"
            }
        }
		stage('构建镜像|推送') {
            steps {
                sh "pwd"
                sh "docker login -u ${dockerhub_credentials_USR} -p ${dockerhub_credentials_PSW} 104.168.133.40"
                sh "docker build -t 104.168.133.40/xxx/${params.project_name}:${image_tag} ."
                sh "docker push 104.168.133.40/xxx/${params.project_name}:${image_tag}"
                sh "rm -rf dist"
            }
        }
		
		stage('生成发布模板') {
            steps {
            sh "sed -e 's#{{APP_NAME}}#${params.project_name}#g;s#{{IMAGE_TAG}}#${image_tag}#g;s#{{APP_REPLICAS}}#${params.replicas}#g;s#{{IMAGE_URL}}#104.168.133.40/xxx/${params.project_name}#g;' k8s.tpl > k8s.yaml"
            stash name: "k8s.yaml", includes: "k8s.yaml"
            sh "cat k8s.yaml"
            }
        }
        stage("发布到K8S"){
            agent {
                docker {
                    image '104.168.133.40/xxx/helm-kubectl-docker'
					args '-u root:root'
                }
            }
            steps {
                sh "mkdir -p ~/.kube"
                sh "echo ${K8S_CONFIG} | base64 -d > ~/.kube/config"
                unstash("k8s.yaml")
                sh "kubectl apply -f k8s.yaml && sleep 6 && kubectl get po,svc,ing -ntiger|grep ${params.project_name}"
            }
		}
    }
}