jenkins-1、创建pipeline及相关脚本编写

新创建一个pipeline项目。

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

然后写第一个脚本,例如:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'echo "Hello World"'
                sh '''
                    echo "Multiline shell steps works too"
                    ls -lah
                '''
            }
        }
    }
}

这里写图片描述

保存

这里写图片描述

构建,查看结果。

这里写图片描述

各个流程演示

pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                echo 'Checkout'

            }
        }        
        stage('Build') {
            steps {
                echo 'Building'
                echo 'sh \'mvn clean install\' -- 可以用自己的 mvn clean deploy + 参数替代 '
            }
        }
        stage('Test') {
            steps {
                echo 'Testing'
                echo  ' sh \'mvn clean verify sonar:sonar\' -- 此处可以使用mvn test替代,笔者这步是检测代码的质量同步到自己的代码质量检测平台。'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying'
                echo 'sh \'mvn clean deploy\' -- 此处调用脚本或者ansible、saltstak,部署到远程 '
            }
        }
    }
    post {
        always {
            echo 'This will always run'
        }
        success {
            echo 'This will run only if successful'
        }
        failure {
            echo 'This will run only if failed'
        }
        unstable {
            echo 'This will run only if the run was marked as unstable'
        }
        changed {
            echo 'This will run only if the state of the Pipeline has changed'
            echo 'For example, if the Pipeline was previously failing but is now successful'
        }
    }
}

构建结果。

这里写图片描述

这里写图片描述

demo1

演示pipeline参数化构建–(即添加输入输出界面)执行sh脚本及从svn拉取源代码

#!/usr/bin/env groovy
pipeline{
    agent none
    options{
        disableConcurrentBuilds()
        skipDefaultCheckout()
        timeout(time: 1, unit: 'HOURS')
        timestamps()
    }
    parameters{
        string(name: 'PERSON', defaultValue: 'among中文', description: '请输入中文')
        booleanParam(name: 'YESORNO', defaultValue: true, description: '是否发布')
        choice(name: 'Server_addr', choices: '10.114.24.192:7722\n10.129.14.147:7722\n10.114.4.208:6005\n127.0.0.1:6005', description: '被测系统ip及端口')
    }
    stages{
    stage('test stage')
    {
      agent
      {
          label 'master' 
      }
      steps
       {

    sh returnStatus: true, script: 'source /etc/profile && source ~/.bash_profile && env'


          echo 'Hello, stage1'
          echo "Hello ${params.PERSON}"
          echo "Hello ${env.PERSON}"
          echo "Hello ${params.choice}"
                script {
                    def browsers = ['chrome', 'firefox']
                    for (int i = 0; i < browsers.size(); ++i) {
                        echo "Testing the ${browsers[i]} browser"
                    }
                }  

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://你的svn的地址/FunFunLeBaseLib']], 
          workspaceUpdater: [$class: 'UpdateUpdater']])


         }
     }
        }
}

注意:

credentialsId 是你在jenkins添加的一个凭据,添加以后会有一个唯一标识给你的。

连续执行三次看结果:

构建前:

这里写图片描述

点击构建:

这里写图片描述

这里写图片描述

这里写图片描述

每次都会重新checkout一次源代码,执行成功,

然而, 假如是大项目的话这样一次构建估计光是checkout都要不少时间,所以本人不推荐每次都checkout的。

普通java app clean 打包 及发布到nexus上

经过不断调试和试错调整,终于将一份可以用的java的pipeline脚本完成,注意,用checkout的原因是,假如直接svn同步更新到我们的指定目录然后用该目录编译那么就会有–第一,需要删除jar,target文件,有权限的限制,第二,删除后会有冲突的问题。

#!/usr/bin/env groovy
pipeline{
    agent any
  environment {
    REVISION = "0.0.${env.BUILD_ID}"
  }
    options{
        disableConcurrentBuilds()
        skipDefaultCheckout()
        timeout(time: 1, unit: 'HOURS')
        timestamps()
    }
    parameters{
        string(name: 'PERSON', defaultValue: '测试-字段输入', description: '请输入中文')
        booleanParam(name: 'YESORNO', defaultValue: true, description: '测试-布尔值')
        choice(name: 'Server_addr', choices: '10.114.24.192:7722\n10.129.14.147:7722\n10.114.4.208:6005\n127.0.0.1:6005', description: '被测系统ip及端口')
    }
    stages{
        stage ('Initialize') {
          steps {
              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://您的svn/FunFunLeBaseLib']],
          workspaceUpdater: [$class: 'UpdateUpdater']])

          }
        }

    stage ('Build') {
      steps {
        echo '构建阶段....'
        sh 'mvn clean package -e -f FunFunLeBaseLib/pom.xml'
      }
    }
    stage ('Deploy') {
      steps {
      echo '发布阶段....'
        script {
          currentBuild.displayName = "${REVISION}"
        }
        sh 'mvn deploy  -Drevision=${REVISION} -e -f FunFunLeBaseLib/pom.xml'
      }
    }
    }

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

}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/cdnight/article/details/80857050