shell 如何在Jenkins中获取sh部分之外的变量值?

juzqafwq  于 2023-04-21  发布在  Shell
关注(0)|答案(2)|浏览(187)

我试图将for循环的输出存储在varble中,并在sh部分之外使用它。
我已经将管道外的lambda_deployers声明为
def lambda_deployers=""

stage('Fetch lambda paths') {
            steps {
                
                sh '''
                        cd $WORKDIR/$BASE_DIR/
                        for k in $(find . -type f -name 'package.json')
                        do
                            FILE_PATH=$(echo "${k}" | cut -c 2-)
                            DIR_PATH=$(dirname "$FILE_PATH" | cut -c 2-)
                            lambda_deployers+="/$BASE_DIR/$DIR_PATH"
                        done
                     echo $lambda_deployers
                        
                     ''' 
                     sh """echo $lambda_deployers"""
                 
                
                
            }
        }

sh '''''''内部的第一个echo可以工作,但sh ''''''外部的echo不能工作
还尝试将其声明为env变量并在脚本下添加sh

environment {
       
        LAMBDA_BASE_DIR="lambda"
        lambda_deployers=""
     
     
    }
stage('Fetch lambda paths') {
            steps {
                script{
                
                sh '''
                        cd $WORKDIR/$BASE_DIR/
                        for k in $(find . -type f -name 'package.json')
                        do
                            FILE_PATH=$(echo "${k}" | cut -c 2-)
                            DIR_PATH=$(dirname "$FILE_PATH" | cut -c 2-)
                            lambda_deployers+="/$BASE_DIR/$DIR_PATH"
                        done
                        echo $lambda_deployers
                  
                     ''' 
                       echo "${env.lambda_deployers}"
                 
                }

sh '''外部的echo显示为null

8ftvxx2r

8ftvxx2r1#

你不能从Shell块中为Pipeline变量赋值。有几种不同的方法可以实现你所需要的,将输出写入一个文件,然后在你想要使用变量时从该文件读取。或者从sh块中返回标准值并将其赋值给变量。如下所示。

lambda_deployers = sh (
    script: '''
          cd $WORKDIR/$BASE_DIR/
          for k in $(find . -type f -name 'package.json')
          do
              FILE_PATH=$(echo "${k}" | cut -c 2-)
              DIR_PATH=$(dirname "$FILE_PATH" | cut -c 2-)
              lambda_deployers+="/$BASE_DIR/$DIR_PATH"
          done
          echo $lambda_deployers
        ''',
    returnStdout: true
).trim()

更新流水线已满

pipeline {
  agent any
  stages {
    stage('Wrapper') {
      steps {
        script {
            lambda_deployers = sh (
                    script: '''
                          cd $WORKDIR/$BASE_DIR/
                          for k in $(find . -type f -name 'package.json')
                          do
                              FILE_PATH=$(echo "${k}" | cut -c 2-)
                              DIR_PATH=$(dirname "$FILE_PATH" | cut -c 2-)
                              lambda_deployers+="/$BASE_DIR/$DIR_PATH"
                          done
                          echo "this is test content"
                        ''',
                    returnStdout: true
                ).trim()
            
            echo "$lambda_deployers"
        }
      }
    }
  }
}
ldioqlga

ldioqlga2#

在这样的步骤中添加脚本

environment {
    lambda_deployers=""
}
stage('Fetch lambda paths') {
    steps {
        script{
            sh '''
                cd $WORKDIR/$BASE_DIR/
                for k in $(find . -type f -name 'package.json')
                do
                    FILE_PATH=$(echo "${k}" | cut -c 2-)
                    DIR_PATH=$(dirname "$FILE_PATH" | cut -c 2-)
                    lambda_deployers+="/$BASE_DIR/$DIR_PATH"
                done
                echo $lambda_deployers
                        
            ''' 
            sh """echo $lambda_deployers"""
        }  
     
    }
}

相关问题