Jenkins动态参数

parameters

能不能在构建时候传入一些参数进去呢?

pipeline {
   agent any
   // 动态参数,看这里!!!!
   parameters {
       // Boolean值
        booleanParam(defaultValue: false,description: 'test run?', name: 'testRun')
        // 下拉列表,默认值是第一个
        choice(choices: 'Windows-1\nLinux-2', description:'Which platform?', name: 'platform')
        //file(description: 'Select the file to upload', name: 'sample')
        // 多行文本
        text(defaultValue: 'No message', description:'Enter your message', name: 'userMsg')
        // 密码
        password(defaultValue: "userpass1", description:'User password?', name: 'pass')
        // 字符串
        string(defaultValue: "Linux",description: 'What platform?', name: 'platform-noduplicate')
    }
   stages {
      stage('Build') {         
         steps {
            // Get some code from a GitHub repository
            git 'https://github.com/oneslideicywater/common.git'
            // 查看设置的参数
            echo "$params.testRun"
            echo "$params.platform"
            echo "$params.userMsg"
            echo "$params"
         }
      }
   }
}

你设置了上面的pipeline.parameters中设置构建参数之后,每次构建可以在里面输入参数覆盖掉默认值。
在这里插入图片描述

小bug

你添加新的构建参数之后,运行一次之后,需要再重试构建一次才可以看到上面的那些参数。

environment

另外,environment变量也可以定义运行时参数,不过这个一般保留的都是静态配置。通过env.xxx来访问。

pipeline {
   agent any
   tools {
      maven 'maven'
   }
    parameters {
        booleanParam(defaultValue: false,description: 'test run?', name: 'testRun')
        choice(choices: 'Windows-1\nLinux-2', description:'Which platform?', name: 'platform')
        //file(description: 'Select the file to upload', name: 'sample')
        text(defaultValue: 'No message', description:'Enter your message', name: 'userMsg')
        password(defaultValue: "userpass1", description:'User password?', name: 'pass')
        string(defaultValue: "Linux",description: 'What platform?', name: 'platform-noduplicate')
    }
    environment{
     // 静态参数,看这里!!!!
      SAMPLE_SERVER="http://10.0.0.1:8080"
      SAMPLE_API="$SAMPLE_SERVER/api"
    }
   stages {
      stage('Build') {              
         steps {
            // Get some code from a GitHub repository
            git 'https://github.com/oneslideicywater/common.git'
            echo "$params.testRun"
            echo "$params.platform"
            echo "$params.userMsg"
            echo "$params"
             // 打印静态参数
            echo "------------------------below are env---------------------"
            echo "$env.SAMPLE_API"
         }
      }
   }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章