在Jenkins中重复使用代码块而不进行即时处理

48次阅读
没有评论

问题描述

在Jenkins中有一段代码块,目前运行良好。但是重复使用该代码有些不太优雅。问题在于当用户对代码块内的语句应用if语句时,需要使用不同的bash脚本。脚本./scripts/awesome_script.sh需要已导出的变量。因此,代码如下:

script {
    withCredentials([
        gitUsernamePassword(credentialsId: 'jenkins-credentials', gitToolName: 'Default', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')
    ]) {
        sh '''
            #!/bin/bash
            export GIT_USERNAME=${GIT_USERNAME};
            export GIT_PASSWORD=${GIT_PASSWORD};
            export PROYECT_DIRECTORY=${PROYECT_DIRECTORY};
            export CHECKOUT_POINT=${CHECKOUT_POINT};
            export GIT_HTTPS_REPO_DEPLOY=${GIT_HTTPS_REPO_DEPLOY};
            export MARIADB_HOSTNAME=${MARIADB_HOSTNAME};
            ./scripts/awesome_script.sh
        '''
        if(env.APP_ENV == 'testing') {
            sh '''
                #!/bin/bash
                export GIT_USERNAME=${GIT_USERNAME};
                export GIT_PASSWORD=${GIT_PASSWORD};
                export PROYECT_DIRECTORY=${PROYECT_DIRECTORY};
                export CHECKOUT_POINT=${CHECKOUT_POINT};
                export GIT_HTTPS_REPO_DEPLOY=${GIT_HTTPS_REPO_DEPLOY};
                export MARIADB_HOSTNAME=${MARIADB_HOSTNAME};
                ./scripts/awesome_script_2.sh
            '''
        }
    }
}

上述代码实现起来比较繁琐,用户希望有一种更优雅的方式来达到相同的效果。

解决方案

以下操作可能因版本差异而有所不同,请在执行前做好备份。

方案1:使用字符串块进行处理

在Jenkins的Groovy脚本中,可以使用三引号(""")来定义多行字符串块,并进行字符串插值,以达到更优雅地重用代码块的效果。以下是实现的步骤:

  1. 在需要重用的代码块前定义一个多行字符串变量,用于存放需要导出的变量信息。
  2. 使用withCredentials定义代码块,使用sh来执行脚本,通过${}进行字符串插值。

下面是基于此方案的示例代码:

script {
    def bashVars = """
        export GIT_USERNAME=${GIT_USERNAME};
        export GIT_PASSWORD=${GIT_PASSWORD};
        export PROYECT_DIRECTORY=${PROYECT_DIRECTORY};
        export CHECKOUT_POINT=${CHECKOUT_POINT};
        export GIT_HTTPS_REPO_DEPLOY=${GIT_HTTPS_REPO_DEPLOY};
        export MARIADB_HOSTNAME=${MARIADB_HOSTNAME};
    """

    withCredentials([
        gitUsernamePassword(credentialsId: 'jenkins-credentials', gitToolName: 'Default', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')
    ]) {
        sh """
            #!/bin/bash
            ${bashVars}
            ./scripts/awesome_script.sh
        """
        if(env.APP_ENV == 'testing') {
            sh """
                #!/bin/bash
                ${bashVars}
                ./scripts/awesome_script_2.sh
            """
        }
    }
}

在上面的示例中,我们首先定义了一个名为bashVars的多行字符串变量,其中存放了需要导出的变量信息。然后,在sh脚本中使用${}进行字符串插值,将变量值插入脚本中,从而实现了更优雅的代码块重用。

方案2:使用条件控制流

另一种优雅的方式是通过使用when条件来控制流程,根据不同的环境变量值执行不同的步骤。以下是基于此方案的示例代码:

pipeline {
    agent any
    stages {
        stage('Stage Testing') {
            when {
                environment name: 'APP_ENV', value: 'testing'
            }
            environment {
                PROYECT_DIRECTORY = '<replace this with testing value>'
                CHECKOUT_POINT = '<replace this with testing value>'
                GIT_HTTPS_REPO_DEPLOY = '<replace this with testing value>'
                MARIADB_HOSTNAME = '<replace this with testing value>'
            }
            steps {
                script {
                    withCredentials([
                        gitUsernamePassword(credentialsId: 'jenkins-credentials', gitToolName: 'Default', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')
                    ]) {
                        sh '''
                            #!/bin/bash
                            ./scripts/awesome_script_2.sh
                        '''
                    }
                }
            }
        }
        stage('Stage Production') {
            when {
                environment name: 'APP_ENV', value: 'production'
            }
            environment {
                PROYECT_DIRECTORY = '<replace this with production value>'
                CHECKOUT_POINT = '<replace this with production value>'
                GIT_HTTPS_REPO_DEPLOY = '<replace this with production value>'
                MARIADB_HOSTNAME = '<replace this with production value>'
            }
            steps {
                script {
                    withCredentials([
                        gitUsernamePassword(credentialsId: 'jenkins-credentials', gitToolName: 'Default', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')
                    ]) {
                        sh '''
                            #!/bin/bash
                            ./scripts/awesome_script.sh
                        '''
                    }
                }
            }
        }
    }
}

在上述示例中,我们使用了when条件来控制在不同环境下执行不同的步骤。根据环境变量APP_ENV的值,我们设置了相应的变量值,并使用sh来执行相应的脚本。

通过以上两种方案,你可以更优雅地重用代码块,使Jenkins Pipeline更加清晰易读。

正文完