Saltar al contenido

Uso de variables globales en Jenkins Pipeline

Te recomendamos que revises esta respuesta en un entorno controlado antes de enviarlo a producción, saludos.

Solución:

sh '''
'''

debiera ser

sh """
"""

con comillas simples, las variables no se procesan.

Intenta seguir y ver cómo va:

def ZIP_NODE
def CODE_VERSION
pipeline
    /*A declarative pipeline*/

    agent 
        /*Agent section*/ 
        // where would you like to run the code 
        label 'master' 
        
    options
        timestamps()
        
    parameters 
        choice(choices: ['dev'], description: 'Name of the environment', name: 'ENV')
        choice(choices: ['us-east-1', 'us-west-1','us-west-2','us-east-2','ap-south-1'], description: 'What AWS region?', name: 'AWS_DEFAULT_REGION')
        string(defaultValue: "", description: '', name: 'APP_VERSION')

        
    stages
        /*stages section*/
        stage('Initialize the variables') 
            // Each stage is made up of steps
            steps
                script
                    CODE_VERSION="$BUILD_NUMBER-$ENV"
                    ZIP_NODE="abcdefgh-0.0.$CODE_VERSION.zip"
                
                            
        
        stage ('code - Checkout') 
            steps
                println "checkout skipped"
                //checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'xxxxxxxxxxxxxxxxxxxxxxxxxx', url: 'http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.git']]]) 
              
        

        stage ('code - Build')
            steps
                sh "echo $JOB_NAME && pwd && echo $ZIP_NODE && echo 'remove alraedy existing zip files' && rm -rf *.zip && zip -r $ZIP_NODE . && chmod 777 $ZIP_NODE"
            
        
        stage('Deploy on Beanstalk')
            steps
                println "build job skipped"
                //build job: 'abcdefgh-PHASER' , parameters: [[$class: 'StringParameterValue', name: 'vpc', value: ENV], [$class: 'StringParameterValue', name: 'ZIP_NODE', value: ZIP_NODE], [$class: 'StringParameterValue', name: 'CODE_VERSION', value: CODE_VERSION], [$class: 'StringParameterValue', name: 'APP_VERSION', value: BUILD_NUMBER], [$class: 'StringParameterValue', name: 'AWS_DEFAULT_REGION', value: AWS_DEFAULT_REGION], [$class: 'StringParameterValue', name: 'ParentJobName', value: JOB_NAME]]
            
        
     

Obtuve la siguiente salida:

Started by user jenkins
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /Users/Shared/Jenkins/Home/workspace/test
[Pipeline] {
[Pipeline] timestamps
[Pipeline] 
[Pipeline] stage
[Pipeline]  (Initialize the variables)
[Pipeline] script
[Pipeline] 
[Pipeline] 
[Pipeline] // script
[Pipeline] 
[Pipeline] // stage
[Pipeline] stage
[Pipeline]  (code - Checkout)
[Pipeline] echo
21:19:06 checkout skipped
[Pipeline] 
[Pipeline] // stage
[Pipeline] stage
[Pipeline]  (code - Build)
[Pipeline] sh
21:19:06 [test] Running shell script
21:19:06 + echo test
21:19:06 test
21:19:06 + pwd
21:19:06 /Users/Shared/Jenkins/Home/workspace/test
21:19:06 + echo abcdefgh-0.0.17-dev.zip
21:19:06 abcdefgh-0.0.17-dev.zip
21:19:06 + echo 'remove alraedy existing zip files'
21:19:06 remove alraedy existing zip files
21:19:06 + rm -rf '*.zip'
21:19:06 + zip -r abcdefgh-0.0.17-dev.zip .
21:19:06 
21:19:06 zip error: Nothing to do! (try: zip -r abcdefgh-0.0.17-dev.zip . -i .)
[Pipeline] 
[Pipeline] // stage

Los entornos globales en jenkins deberían estar fuera de la tubería puede inicializarse/usarse dentro de los scripts y, por supuesto, conocerse en todos los ámbitos dentro de la canalización:

ejemplo:

def internal_ip

pipeline 
    agent  node  label "test"  
        stages 
            stage('init') 
                steps 
                    script 
                        def x
                        if(env.onHold.toBoolean() == true)
                            x=1
                        else
                            x=2
                        
                        internal_ip = sh (
                            script: "echo 192.168.0.$x",
                            returnStdout: true
                        ).trim()   
                    
                
            
            stage('test') 
                steps 
                    script 
                        echo  "my internal ip is: $internal_ip"
                    
                
            
            
    

Acuérdate de que tienes la capacidad de añadir una estimación acertada .

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *