Loading

jenkins-pipline

jenkins pipline

pipline试用

pipeline { 
    agent any
    stages { 
        stage('Build') { 
            steps { 
                echo 'Building..' 
                } 
            } 
            stage('Test') { 
                steps { 
                    echo 'Testing..' 
                } 
            } 
            stage('Deploy') { 
                steps { 
                    echo 'Deploying....' 
                } 
            } 
        }
    }


##################
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'
        }
    }
}

通过 scm 获取 Jenkinsfile

在代码仓库的根目录下创建一个jenkinsfile ,从仓库获获取代码后,Jenkins 会将这个文件取出来,然后按照这个jenkins文件编译执行

设置部分:进入配置页面,点击 Pipeline 选项卡,下拉到pipeline 部分,选择从 scm 获取 pipeline script

进入到 scm 配置页面,选择从 git 仓库获取

进入到 git 仓库配置页面,输入仓库地址,配置认证,选择分支等,然后点击保存

在仓库根目录下的jenkins file 名字

https://www.cnblogs.com/mingerlcm/p/12799362.html

pipeline+gitlab+ansible简单部署案例

https://www.cnblogs.com/FRESHMANS/p/8184874.html#_label2

https://blog.csdn.net/diantun00/article/details/81075007

https://blog.csdn.net/wh211212/article/details/77482138

构建插件 系统管理--->管理插件--->可选插件--->搜索extended choice parameter---->点击直接安装

添加参数,使用参数

配置Jenkins的节点:

节点的用法,仅在构建job 指定标签匹配节点时使用 及 尽可能的使用该节点

配置节点的标签:

Jenkins 节点与主节点时间保持同步

标签: dev cpu

在pipline 里指定执行的节点

pipeline {
    // 注释
    //agent 使用了标签,在配置node 时对节点设置了标签
    //}
    agent { label 'dev' }
    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'
        }
    }
}

什么是Jenkinsfile

Jenkinsfile 是 Jenkins 2.x 核心特性 Pipeline 的脚本,由Groovy语言实现。Jenkinsfile一般是放在项目根目录,随项目一起受源代码管理软件控制,无需像创建“自由风格"(Jenkins FreeStyle)项目一样,每次可能需要拷贝很多设置到新项目,提供了一些直接的好处:

  • Pipeline上的代码审查/迭代
  • Pipeline的审计跟踪
  • Pipeline的唯一真实来源,可以由项目的多个成员查看和编辑。

Pipeline支持:Declarative(在Pipeline 2.5中引入)和Scripted Pipeline两种格式。两者都支持建立Pipeline,两者都可以用于在Web UI中定义一个流水线Jenkinsfile,将Jenkinsfile文件创建并检查到源代码控制库中通常被认为是最佳做法

声明式Pipline

pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

脚本式Pipline

node {
    stage('Example') {
        if (env.BRANCH_NAME == 'master') {
            echo 'I only execute on the master branch'
        } else {
            echo 'I execute elsewhere'
        }
    }

安装maven 必须放在/usr/local/下 见博客

测试脚本

node {
   def mvnHome
   stage('Preparation') { // for display purposes
      // Get some code from a GitHub repository
      git 'https://github.com/jglick/simple-maven-project-with-tests.git'
      // Get the Maven tool.
      // ** NOTE: This 'M3' Maven tool must be configured
      // **       in the global configuration.           
      mvnHome = tool 'M3'
   }
   stage('Build') {
      // Run the maven build
      withEnv(["MVN_HOME=$mvnHome"]) {
         if (isUnix()) {
            sh '"$MVN_HOME/bin/mvn" -Dmaven.test.failure.ignore clean package'
         } else {
            bat(/"%MVN_HOME%\bin\mvn" -Dmaven.test.failure.ignore clean package/)
         }
      }
   }
   stage('Results') {
      junit '**/target/surefire-reports/TEST-*.xml'
      archiveArtifacts 'target/*.jar'
   }
}

http://blog.didispace.com/jenkins-pipeline-top-10-action/

https://www.jianshu.com/p/f1167e8850cd

https://www.tuicool.com/articles/rumiaeb

https://zhuanlan.zhihu.com/p/51533506 完善脚本实验

https://blog.csdn.net/cdnight/article/details/80857050 demo1

https://xuanwo.io/2019/08/30/jenkins-pipeline-intro/

posted @ 2020-06-24 14:59  Lust4Life  阅读(172)  评论(0编辑  收藏  举报