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"
         }
      }
   }
}


系统变量

参考:Using environment variables

pipeline {
    agent any

    tools {
        // Install the Maven version configured as "M3" and add it to the path.
        maven "maven"
    }
    parameters {
        string(description: 'branch name', name: 'branch')
    }
    stages {
        
        stage('envchecking'){
            steps{
                // 在jenkins中,build id和job name可以唯一标识一次构建
                // build id 
                echo "${env.BUILD_ID}"
                // job name
                echo "${env.JOB_NAME}"
            }
        }
        
        stage('Build') {
            
            steps {
                // 拉取指定分支的代码
                git branch: "${params.branch}", url: 'https://gitee.com/oneslideicywater/sample-jenkins-app.git'
                sh 'cat sample.txt'
                // Run Maven on a Unix agent.
                sh "mvn -Dmaven.test.failure.ignore=true clean package"

                // To run Maven on a Windows agent, use
                // bat "mvn -Dmaven.test.failure.ignore=true clean package"
            }

            post {
                // If Maven was able to run the tests, even if some of the test
                // failed, record the test results and archive the jar file.
                success {
                    junit '**/target/surefire-reports/TEST-*.xml'
                    archiveArtifacts 'target/*.jar'
                }
            }
        }
    }
}
Logo

更多推荐