Quantcast
Channel: Active questions tagged bash - DevOps Stack Exchange
Viewing all articles
Browse latest Browse all 48

Reuse block string without processing it on the fly

$
0
0

I have a block of code that work fine at the moment. But reuse that code is a little ugly.

pipeline {    agent any    stages {        stage('Stage 1') {            steps {                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'''                    }                }            }        }    }}

The problem is when I apply if statements to use diferents scripts bash inside sh() block. The script ./scripts/awesome _script.sh need that variables exported. Therefore the code looks like this.

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

Very ugly, right ? :/ :(

What I am looking is something similar like to this, but with correct syntax:

script {                    env.STRING_BLOCK_WITHOUT_PROCESSING = '''                        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                        ${STRING_BLOCK_WITHOUT_PROCESSING}                        ./scripts/awesome_script.sh'''                        if(env.APP_ENV == 'testing'){                            sh '''#!/bin/bash                            ${STRING_BLOCK_WITHOUT_PROCESSING}                            ./scripts/awesome_script_2.sh'''                        }                    }                }

Thanks for all help you can give me. <3


Viewing all articles
Browse latest Browse all 48

Latest Images

Trending Articles



Latest Images