0、 Pipeline的几个基本概念

Stage: 阶段,一个Pipeline可以划分为若干个Stage,每个Stage代表一组操作。注意,Stage是一个逻辑分组的概念,可以跨多个Node。
Node: 节点,一个Node就是一个Jenkins节点,或者是Master,或者是Agent,是执行Step的具体运行期环境。
Step: 步骤,Step是最基本的操作单元,小到创建一个目录,大到构建一个Docker镜像,由各类Jenkins Plugin提供。

1、pipeline语法如下:

node {                    #这里可以定义在node节点运行jenki服务
   stage('拉取代码') { 
    git credentialsId: 'b25e5382-9949-468d-a101-4b40cc479a38', url: 'http://139.159.232.80:3429/root/icp-web-interface.git'        #代码的拉取地址
   }
   stage('Build') {                   #()小括号填写执行步骤的名称
        env.PATH = "/usr/java/jdk1.8.0_151/bin:/usr/local/maven/bin:$PATH"    #定义环境变量
            sh "/usr/local/maven/bin/mvn clean compile"          # “sh” 后面跟需要执行的命令
  }
  stage('单元测试'){
      sh "/usr/local/maven/bin/mvn test"
  }
    stage('质量扫描'){
      sh '/usr/local/sonar-scanner/bin/sonar-scanner'
  }
   stage('构建打包'){
            sh "/usr/local/maven/bin/mvn package"
   }
}

2、pipeline语法如下:

node {
  stage('拉取代码') { 
    git credentialsId: 'b25e5382-9949-468d-a101-4b40cc479a38', url: 'http://139.159.232.80:3429/root/icp-service-tzclaim.git'
   }
  stage('Build') {
        env.PATH = "/usr/java/jdk1.8.0_151/bin:/usr/local/maven/bin:$PATH"
            sh "/usr/local/maven/bin/mvn clean compile"
  }
  stage('单元测试'){
      sh "/usr/local/maven/bin/mvn test"
  }
  stage('质量扫描'){
    withSonarQubeEnv('SonarQube'){             #定义此行可以在jenkins中直接跳转到sonarqube
      sh '/usr/local/sonar-scanner/bin/sonar-scanner'
       }
    }
  stage('构建打包'){
            sh "/usr/local/maven/bin/mvn package"
   }
  stage('Build and push Image'){         #此部分将构建好的项目打包成镜像然后上传到harbor仓库
sh '''
cat > Dockerfile << EOF                  #构建一个Dockerfile
FROM tomcat:8                            #基础镜像信息
ADD target/icp-service-tzclaim /usr/local/tomcat/webapps    
EOF
docker build -t 192.168.2.20/library/icp-service-tzclaim:$Tag .
docker login -u test -p 12345 192.168.2.20
docker push 192.168.1.202/library/icp-service-tzclaim:$Tag
'''
   }
}

3、pipeline语法如下:

node {
   stage('拉取代码') { // for display purposes
    git credentialsId: 'b25e5382-9949-468d-a101-4b40cc479a38', url: 'http://139.159.232.80:3429/root/hello.git'
   }
   stage('Build') {
        env.PATH = "/usr/java/jdk1.8.0_151/bin:/usr/local/maven/bin:$PATH"
            sh "/usr/local/maven/bin/mvn clean compile"
  }
   stage('单元测试'){
      sh "/usr/local/maven/bin/mvn test"
  }
   stage('构建打包'){
            sh "/usr/local/maven/bin/mvn package"
   }
    stage('Build and push Image'){
sh '''
cat > Dockerfile << EOF
FROM tomcat:8
ADD target/hello.war /usr/local/tomcat/webapps
EOF
docker build -t 192.168.1.202/library/hello:${Tag} .
docker login -u admin -p Harbor12345 192.168.1.202
docker push 192.168.1.202/library/hello:${Tag}
'''
   }
   stage('Deploying k8s'){
       sh 'ssh root@192.168.1.203 -C kubectl set image deploy/hello hello=192.168.1.202/library/hello:${Tag}'
   }
   
}

4、pipeline语法如下:

node {
   stage('拉取代码') { // for display purposes
    git credentialsId: 'b25e5382-9949-468d-a101-4b40cc479a38', url: 'http://139.159.232.80:3429/root/icp-web.git'
   }
   stage('Build') {
        env.PATH = "/usr/java/jdk1.8.0_151/bin:/usr/local/maven/bin:$PATH"
            sh "/usr/local/maven/bin/mvn clean compile"
  }
  stage('单元测试'){
      sh "/usr/local/maven/bin/mvn test"
  }
  stage('代码扫描') {
     sh "/usr/local/sonar-scanner/bin/sonar-scanner \
     -Dsonar.projectKey=icp-web \
     -Dsonar.projectName=icp-web \
     -Dsonar.sources=. \
     -Dsonar.host.url=http://192.168.1.201:9000 \
     -Dsonar.language=java \
     -Dsonar.java.binaries=. \
     -Dsonar.sourceEncoding=UTF-8"
    }
   stage('构建打包'){
            sh "/usr/local/maven/bin/mvn package"
   }
}

5、pipeline语法如下:

node {
    stage('拉取代码'){
        git credentialsId: '026ccbda-0ec8-4922-b6a5-6a6dabad9afb', url: 'git@192.168.5.2:root/test.git'
    }
    stage('复制文件'){
        sh "cp /var/lib/jenkins/workspace/test1/index.jsp /docker/index.jsp"
    }
    stage('制作镜像'){
        sh '''
cd /docker
cat >Dockerfile <<EOF
FROM tomcat:8
RUN rm -rf /usr/local/tomcat/webapps/ROOT/index.jsp
ADD index.jsp /usr/local/tomcat/webapps/ROOT/
EOF
        '''
    }
    stage('构建'){
        sh '''
cd /docker
docker build -t tomcat:${TIME} .
        '''
    }
    stage('上传到镜像仓库'){
        sh '''
docker tag tomcat:${TIME} 192.168.5.4:5000/tomcat:${TIME}
docker push 192.168.5.4:5000/tomcat:${TIME}
        '''
    }
    stage('停止上一版本容器'){
    sh '''
ssh 192.168.5.2 "sh /root/docker_delete.sh"
        '''
    }

    stage('运行新容器'){
        sh "ssh 192.168.5.2 'docker run -d -p 8083:8080 192.168.5.4:5000/tomcat:${TIME}'" 
    }
}

6、pipeline效果图

在这里插入图片描述
在这里插入图片描述
说明:红色的表示失败,可以通过日志查看具体失败原因

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐