demo pipeline脚本

#!/usr/bin/env groovy
pipeline{
    agent any
  environment {
    REVISION = "0.0.${env.BUILD_ID}"
  }
    options{
        disableConcurrentBuilds()
        skipDefaultCheckout()
        timeout(time: 1, unit: 'HOURS')
        timestamps()
    }
    parameters{
        choice(name: 'build_env', choices: 'dev\ntest\nproduct\n', description: '请选择构建的环境')
    }
    stages{
        stage ('Initialize') {

          steps {
        script {
          currentBuild.displayName = "${REVISION}"
        }
              sh '''
              echo "任务初始化..."
              echo "构建版本revision:${REVISION}"
              '''
              sh '''
              echo "项目检出...."
              '''
checkout([$class: 'SubversionSCM',
          additionalCredentials: [],
          excludedCommitMessages: '',
          excludedRegions: '',
          excludedRevprop: '',
          excludedUsers: '',
          filterChangelog: false,
          ignoreDirPropChanges: false,
          includedRegions: '',
          locations: [[credentialsId: 'e522721e-4a9a-467c-b154-acb803d8afb0',
                       depthOption: 'infinity',
                       ignoreExternalsOption: true,
                       remote: 'svn://127.0.0.1:3690/MicroBaseModule']],
          workspaceUpdater: [$class: 'UpdateUpdater']])


          }
        }

    stage ('Build') {

      steps {
        echo "您选择了:${params.build_env}"
        echo '构建阶段....'
        echo '构建部署 MicroBaseApi子项目...'
        sh "mvn clean deploy -e -f MicroBaseModule/MicroBaseApi/pom.xml -P ${params.build_env}"
        echo '构建打包MicroBaseApp子项目...'
        sh "mvn clean package -e -f MicroBaseModule/MicroBaseApp/pom.xml -P ${params.build_env}"
      }
    }
    stage ('Deploy') {
      steps {
      echo '发布阶段....'

       script
            {
              if (params.build_env == 'test') {
                  echo '=====》》》测试环境进行远程发布。'
                  sh '''
                  sudo su -s /bin/bash jenkins
                  scp /var/lib/jenkins/workspace/MicroBase/MicroBaseModule/MicroBaseApp/target/MicroBaseApp-Build.zip root@x.x.x.x(你的远程机器地址):/data/packages/test/MicroBaseApp/MicroBaseApp-Build-${REVISION}.zip
                  '''

              }
              if(params.build_env=='product'){
                    echo '=====》》》生产环境进行远程发布。'
                sh '''
                sudo su -s /bin/bash jenkins
                  scp /var/lib/jenkins/workspace/MicroBase/MicroBaseModule/MicroBaseApp/target/MicroBaseApp-Build.zip root@x.x.x.x(你的远程机器地址):/data/packages/product/MicroBaseApp/MicroBaseApp-Build-${REVISION}.zip
                  '''
              }
              else {
                  echo '=====》》》 默认(开发)环境进行远程发布。'
                sh '''
                sudo su -s /bin/bash jenkins
                  scp /var/lib/jenkins/workspace/MicroBase/MicroBaseModule/MicroBaseApp/target/MicroBaseApp-Build.zip root@x.x.x.x(你的远程机器地址):/data/packages/dev/MicroBaseApp/MicroBaseApp-Build-${REVISION}.zip
                  '''                  
              }

              }
      }
    }

    }

post {
        always {
            echo '构建结束...'
        }
        success {
            echo '恭喜您,构建成功!!!'
        }
        failure {
            echo '抱歉,构建失败!!!'
        }
        unstable {
            echo '该任务已经被标记为不稳定任务....'
        }
        changed {
            echo ''
        }
    }

}

在stage下面用script标签然后用sh执行脚本,
ps:
在执行时候请切换一下角色,sudo su -s /bin/bash jenkins 譬如这个。

假如你要远程到目标机器执行shell,那么下面这个命令帮到你:

ssh -t  -p 22 root@x.x.x.x "ls /"
Logo

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

更多推荐