一、拉取镜像和发布应用
1.安装 Publish Over SSH 插件
Publish Over SSH 插件可以实现远程发送Shell命令,安装完需要重启 Jenkins
2. 配置远程部署服务器
拷贝公钥,在Jenkins服务器上拷贝到生产服务器上
[root@jenkins ~]# cd .ssh/
[root@jenkins .ssh]# ls
id_rsa id_rsa.pub known_hosts
[root@jenkins .ssh]# ssh-copy-id 192.168.74.12
#上述地址是web生产服务器地址
查看生产服务器的凭证
[root@web1 ~]# cd .ssh/
[root@web1 .ssh]# ls
authorized_keys
[root@web1 .ssh]# cat authorized_keys
3.在Jenkins 系统配置中添加远程服务器
Dashboard——>Configure System
3. 修改Jenkinsfile 构建脚本生成远程调用模板代码
粘贴到jenkinsfile脚本中
目前Jenkinsfile脚本内容
//git的凭证
def git_auth="0aa6d8b5-b2c8-4a66-8b76-a9d7d16c5bd5"
//git的URL
def git_url="git@192.168.74.11:kgc/tensquare_back.git"
//镜像标签
def tag="latest"
//harbor的url地址
def harbor_url="192.168.74.7:85"
//镜像仓库名
def harbor_name="tensquare"
//定义harbor的凭证
def harbor_auth="49a8e73a-5c5f-483f-b6f1-78c15a1adf7f"
node {
stage('pull code') {
//切换成变量,字符串符号使用双引号
checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]])
}
stage('check code') {
//定义SonarQubeScanner工具
def scannerHome = tool 'sonar-scanner'
//引用SonarQube系统环境
withSonarQubeEnv('sonarqube') {
sh """
cd ${project_name}
${scannerHome}/bin/sonar-scanner
"""