jenkins发布pipeline项目

1、安装流水线相关插件

jenkins发布pipeline项目

2、创建流水线项目

jenkins发布pipeline项目

3、对流水线项目进行配置,对相碰变量进行参数化

jenkins发布pipeline项目

4、编辑流水线脚本

jenkins发布pipeline项目

脚本内容如下

def label = "chench-jnlp"
podTemplate(label: label, containers: [
  containerTemplate(name: 'maven', image: 'maven:3.6-alpine',  command: 'cat', ttyEnabled: true),
  containerTemplate(name: 'docker', image: 'docker',  command: 'cat', ttyEnabled: true),
  containerTemplate(name: 'kubectl', image: 'cnych/kubectl',  command: 'cat', ttyEnabled: true)
], volumes: [
  hostPathVolume(mountPath: '/root/.m2', hostPath: '/root/.m2'),
  hostPathVolume(mountPath: '/root/.kube', hostPath: '/root/.kube'),
  hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock')
], envVars: [podEnvVar(key: 'JENKINS_TUNNEL', value: 'jenkins-agent:50000')]) {
node(label) {
    stage('Clone') {
        echo "1.Clone Stage"
        checkout([$class: 'GitSCM', branches: [[name: '*/master']], userRemoteConfigs: [[credentialsId: '03ae1cf6-179f-4eab-8bf9-c6e0703c8f8e', url: 'http://xxx.xxx.xxx.xxx:3000/Wehgc-Lucky/lucky-etc-adapter.git']]])
        sh "sed -i 's/10.1.1.233:9006/xxx.xxx.xxx.xxx:9006/g' $WORKSPACE/pom.xml"
        sh "sed -i 's#url: jdbc:mysql://.*#url: jdbc:mysql://mysql-mysqlha-0.mysql-mysqlha:3306/lucky_etc_adapter?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2B8#g'  $WORKSPACE/$project/src/main/resources/application-dev.yml"
        sh "sed -i 's/server-addr:.*/server-addr: nacos-0.nacos-headless.wehgc-xm.svc.cluster.local:8848/g' $WORKSPACE/$project/src/main/resources/bootstrap.yml"
        sh "sed -i 's/hczd365/Hczd365!!/g' $WORKSPACE/$project/src/main/resources/application-dev.yml"
        sh "sed -i 's/ host: 10.1.1.234/ host: redis/g' $WORKSPACE/$project/src/main/resources/application-dev.yml"
        sh "sed -i '26s/password: Hczd365!!/password: 1qVI5Rsw7Y/g ' $WORKSPACE/$project/src/main/resources/application-dev.yml"
    }
    stage('Compile') {
    container('maven') {
      echo "2.compile and  package"
      sh ' cd $project && mvn clean package -Dmaven.test.skip=true'
    }
    }
    stage('Build') {
    container('docker') {
        echo "3.Build Docker Image Stage"
        sh "cd $project && docker build -t ${registry}/$project:$BUILD_ID.1 ."
    }
    }
    stage('Push') {
    container('docker') {
        echo "4.Push Docker Image Stage"
        withCredentials([[$class: 'UsernamePasswordMultiBinding',
        credentialsId: 'dockerhub',
        usernameVariable: 'DOCKER_HUB_USER',
        passwordVariable: 'DOCKER_HUB_PASSWORD']]) {
            sh "docker login -u ${DOCKER_HUB_USER} -p ${DOCKER_HUB_PASSWORD} ${registry}"
            sh "docker push ${registry}/$project:$BUILD_ID.1"
    }
    }
    }
    stage('Deploy') {
    container('kubectl') {
        echo "5. Deploy Stage"
        sh "sed -i 's#latest#$BUILD_ID.1#g' $project/$project.yaml"
        sh "sed -i 's#_project_#$project#g' $project/Dockerfile"
        sh "sed -i 's#_project_#$project#g' $project/startup.sh"
        sh "kubectl -n wehgc-xm set image deployment $project $project=${registry}/$project:$BUILD_ID.1"
        sh "sed -i 's#$BUILD_ID.1#latest#g' $project/$project.yaml"
    }
}
}
}

其中credentialsId及后面的git仓库地址这些是由流水线语法生成的

jenkins发布pipeline项目

jenkins发布pipeline项目

docker仓库这块的用户和密码{DOCKER_HUB_USER} 和${DOCKER_HUB_PASSWORD}是事先定义在凭据里面的

jenkins发布pipeline项目

jenkins发布pipeline项目


jenkins发布pipeline项目

上一篇:第五次SDN实验


下一篇:WebDriver多线程并发