环境准备:
10.23.160.50 gitlab 安装完成
10.23.160.70 jenkins 安装完成 jdk1.8.0
10.23.160.71 部署服务器

1.创建自由风格的项目

2.添加凭证和所要拉去的代码的git地址

 

添加凭证

 

 

3.构建项目测试拉取结果 

 

 

 5.创建流水项目进行自动化封装和推送到远程docker仓库

 

 

 docker 封装镜像和推送到个人仓库,在这里是用的腾讯云私有仓库,也可以自己搭建harbor

pipeline {
    agent any

    stages {
        stage('pull code') {
            steps {
                git credentialsId: 'a7bca987-7181-4a80-8702-ceab5afda6ac', url: 'git@10.23.160.50:xrkmonitor/test.git' 
            }
        }
        stage('docker build images') {
            steps {
                sh 'docker build --tag=nginx:v3 . '
                sh 'docker tag nginx:v3 ccr.ccs.tencentyun.com/shiq/test:v3'
                sh 'docker tag nginx:v3 ccr.ccs.tencentyun.com/shiq/test:latest'
            }
        }
        stage('docker push images') {
            steps {
                sh 'sudo docker login --username=username --password=password ccr.ccs.tencentyun.com'
                sh 'docker push ccr.ccs.tencentyun.com/shiq/test:v3'
            }
        }
    }
}

测试结果

 

 

 6.进行远程项目部署

需要进行插件的安装:

 Publish Over SSH     远程连接
SSH Pipeline Steps   远程连接
Deploy to container  tomcat传输插件

SSH Pipeline Steps 常用参数可写成变量使用

远程连接---声明式测试

 

 

 

脚本远程连接
node {
  def remote = [:]
  remote.name = 'root'
  remote.host = '10.23.160.71'
  remote.user = 'root'
  remote.password = '123123'
  remote.allowAnyHosts = true
  stage('Remote SSH') {
    sshCommand remote: remote, command: "ls -lrt"
    sshCommand remote: remote, command: "for i in {1..5}; do echo -n \"Loop \$i \"; date ; sleep 1; done"
  }
}


声明远程连接

pipeline {
    agent any

    stages {
        stage('SSH---') {
            steps {
                script {
                  def remote = [:]
                  remote.name = 'root'
                  remote.host = '10.23.160.71'
                  remote.user = 'root'
                  remote.password = '123123'
                  remote.allowAnyHosts = true

                  sshCommand remote: remote, command: "ls -lrt"
                  sshCommand remote: remote, command: "for i in {1..5}; do echo -n \"Loop \$i \"; date ; sleep 1; done"
                  }
            }
        }
    }
}

 Publish Over SSH    

需要在系统配置中 先添加需要远程的用户

 

 

 找到流水线语法

 

 

 

 

 

 

sshPublisher(publishers: [sshPublisherDesc(configName: 'centos7-test', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'pwd', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                sshPublisher(publishers: [sshPublisherDesc(configName: 'centos7-test', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'pwd', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
            }
        }
    }
}

 

 

 

后面就是进行部署脚本编写了 

pipeline {
    agent any

    stages {
        stage('SSH---') {
            steps {
                script {
                  def remote = [:]
                  remote.name = 'root'
                  remote.host = '10.23.160.71'
                  remote.user = 'root'
                  remote.password = '123123'
                  remote.allowAnyHosts = true

                  sshCommand remote: remote, command: "docker login --username=username --password=password ccr.ccs.tencentyun.com"
                  sshCommand remote: remote, command: "docker pull ccr.ccs.tencentyun.com/shiq/test:v2"
                  
                  sshCommand remote: remote, command: "docker run -d -p 80:80 --name test_web ccr.ccs.tencentyun.com/shiq/test:v2"
                  }
            }
        }
    }
}

 

 

 

 

 

 

 

posted on 2021-09-08 16:43  宇小白  阅读(106)  评论(0编辑  收藏  举报