IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> Jenkins pipeline -> 正文阅读

[系统运维]Jenkins pipeline

多分支流水线

gitlab管理的UI项目下细分为2个模块admin、mobile,每个模块为1个目录。gitlab的触发只能针对UI整个项目,为了让子模块admin的改动不触发mobile的构建,在commits中获取模块名字,然后让正则表达式去匹配。

pipeline {
   agent any
   //禁止并行构建
   options {
       disableConcurrentBuilds()
        }   

   //自动触发构建,并按不同模块触发
    triggers {
    GenericTrigger(
     genericVariables: [
      [key: 'ref', value: '$.ref'],
      [key: 'commits', value: '$.commits[*].[\'modified\',\'added\',\'removed\'][*]']   //获取commits中的变更内容
     ],

     causeString: 'Triggered on $ref',

     token: 'UI',      //不同项目填写不同token,让gitlab知道触发哪个项目

     printContributedVariables: true,
     printPostContent: true,

     regexpFilterText: '$ref $commits',
     regexpFilterExpression: 'refs/heads/'+env.BRANCH_NAME+' '+'.*admin.*'   //如果commits中包含admin,则触发
    )
  }

 

   stages {

        stage('env') {
            steps {
                sh 'printenv'     //打印构建过程中的变量
            }
          }

        stage('deploy to dev') {
            when {
                branch 'dev*'
            }
            steps {
                echo 'dev'
            }
          }

        stage('deploy to staging') {
            when {
                branch 'staging*'
            }
            steps {
                echo 'staging'
            }

        }

        stage('deploy to prd') {
            when {
                branch 'master'
            }
            steps {
                echo 'master'
            }
          }
        }
}

SSH命令

基于密钥登陆

def getRemoteHost(ip,user){
    def remote = [:]
    remote.name = ip
    remote.host = ip
    remote.user = user
    remote.identityFile = identity
    remote.port = 22
    remote.allowAnyHosts = true
    return remote
}

pipeline {
   agent any
   
      environment {
      //远程服务器信息
         ssh_ip = 'x.x.x.x'
         ssh_user= "root"
         ssh_key_uid = "server_privatekey"  //Jenkins “凭据”-“系统”-”全局凭据“ 中添加”Secret text“,ID为server_privatekey
   }
   
   stages {
     stage('远程服务器操作'){
            steps {
                //SSH操作服务器
            withCredentials([sshUserPrivateKey(credentialsId: "${ssh_key_uid}", keyFileVariable: 'identity')]){
                sshCommand remote: getRemoteHost(ssh_ip,ssh_user), command: "echo"  //在远程服务器执行echo命令
                sshPut remote: getRemoteHost(ssh_ip,ssh_user), from: "/tmp/1.txt", into: "/tmp"  //将本地/tmp/1.txt传送到远程服务器的/tmp目录下
                   }
             }
         }
   }
}    

基于用户名密码登陆

def getRemoteHost(ip,user,password){
    def remote = [:]
    remote.name = ip
    remote.host = ip
    remote.user = user
    remote.password = password
    remote.port = 22
    remote.allowAnyHosts = true
    return remote
}

pipeline {
   agent any
   
      environment {
      //远程服务器信息
         ssh_name = 'x.x.x.x'
        // 目标服务器信息
         ssh_ip = 'x.x.x.x'
         ssh_port = 22
         SSH_CREDS = credentials('serverXXX')  
         ssh_user= "${SSH_CREDS_USR}"
         ssh_password = "${SSH_CREDS_PSW}"  //Jenkins “凭据”-“系统”-”全局凭据“ 中添加”Username with password“,ID为serverXXX
   }
   
   stages {
     stage('远程服务器操作'){
            steps {
                //SSH操作服务器
                sshCommand remote: getRemoteHost(ssh_ip,ssh_user,ssh_password), command: "echo"  //在远程服务器执行echo命令
                sshPut remote: getRemoteHost(ssh_ip,ssh_user,ssh_password), from: "/tmp/1.txt", into: "/tmp"  //将本地/tmp/1.txt传送到远程服务器的/tmp目录下
             }
         }
   }
}    
  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2021-07-11 16:57:23  更:2021-07-11 16:59:45 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/5 18:24:12-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码