【问题标题】:Jenkins pipeline script global variableJenkins 管道脚本全局变量
【发布时间】:2021-10-19 20:44:54
【问题描述】:

我正在学习 jenkins,并且正在开发示例管道

pipeline {
agent any

stages {
    stage('Stage1') { 
        steps {
            bat  '''
                    cd C:/Users/roger/project/
            
                    python -u script1.py
                '''
        }
    }
stage('Stage2') { 
        steps {
            bat  '''
                    cd cd C:/Users/roger/project/abc/
            
                    python -u script2.py
                '''
        }
    }
stage('Stage3') { 
        steps {
            bat  '''
                    cd cd C:/Users/roger/project/abc/new_dir/
            
                    python -u demo.py
                '''
        }
    }
}
}

有没有办法将项目C:/Users/roger/project/ 的基本路径存储为变量,以便可以使用它来追加新路径而不是写入整个路径。

我怎么能写上面的阶段,这样我就不必每次都重复写相同的基本路径到每个阶段

【问题讨论】:

    标签: jenkins continuous-integration jenkins-pipeline continuous-deployment


    【解决方案1】:

    您有多种选择,最简单的方法是在 environment 指令 (read more) 中定义参数,这将使该参数可用于管道中的所有阶段,并将其加载到任何解释器步骤,例如 shbatpowershell,从而使您作为环境变量执行的脚本也可以使用该参数。
    另外environment 指令支持credential parameters,这非常有用。
    在您的情况下,它看起来像:

     pipeline {
         agent any
         environment {
             BASE_PATH = 'C:/Users/roger/project/'
         }
         stages {
             stage('Stage1') {
                 steps {
                     // Using the parameter as a runtime environment variable with bat syntax %%
                     bat  '''
                         cd %BASE_PATH%
                         python -u script1.py
                     '''
                 }
             }
             stage('Stage2') {
                 steps {
                     // Using groovy string interpolation to construct the command with the parameter value
                     bat  """
                         cd ${env.BASE_PATH}abc/
                         python -u script2.py
                     """
                 }
             }
         }
     }
    

    另一个选项是使用在管道顶部定义的全局变量,它的行为类似于任何常规变量,并且可用于管道中的所有阶段(但不适用于解释器步骤的执行环境)。
    比如:

     BASE_PATH = 'C:/Users/roger/project/'
    
     pipeline {
         agent any
         stages {
             stage('Stage1') {
                 steps {
                     // Using the parameter insdie a dir step to change directory 
                     dir(BASE_PATH) {
                         bat 'python -u script1.py'
                     }
                 }
             }
             stage('Stage2') {
                 steps {
                     // Using groovy string interpolation to construct the command with the parameter value
                     bat  """
                         cd ${BASE_PATH}abc/
                         python -u script2.py
                     """
                 }
             }
         }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-14
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多