本文介绍了在Jenkins中执行管道期间如何将动态值传递到环境块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与我之前问过的一个问题有关:

This is related to one question I asked before: Using groovy to parse JSON object in shell scripts for Jenkin

基本上,我将需要将从sh脚本返回的动态值传递给环境块,以便随后的阶段可以重新使用该值,并将该版本作为标签传递给名为Xray的JIRA插件.但是我知道在管道执行期间无法将动态值传递给环境块.因此,我想我需要尝试其他方法,不确定是否有人可以给我一些提示?

basically I will need to pass a dynamic value as returned from sh scripts to an environment block, so that the following stage can re-use that value and pass the version as a label to JIRA plugin called Xray. But I aware that I cannot pass dynamic values to an environment block during the pipeline execution. So, I think I am going to need try a different route for that, not sure if anyone could give me some tips please?

def setLatestAppVersionLabel() {
  def response = sh(script: "curl --silent ${APP_ARTIFACTORY_URL}/${XRAY_PLATFORM}/builds/latest.json", returnStdout: true).trim() as String
  def jsonResponse = readJSON text: response
  LATEST_VERSION = jsonResponse.id
  echo "LATEST_VERSION -> ${LATEST_VERSION}"
}

JSON响应如下所示:

JSON response looks like that:

{"id":"1.0.0-6",
"version":"1.0.0",
"build":6,
"tag":"android-v1.0.0-6",
"commitHash":"5a78c4665xxxxxxxxxxe1b62c682f84",
"dateCreated":"2020-03-02T08:11:29.912Z"}

有一个环境块,我想将值传递给其中定义的变量之一@

and there is an environment block where I would like to pass the value to one of the variable defined there@

environment {
        AWS_DEFAULT_REGION = 'uk-xxx'
        XRAY_ENVIRONMENT = 'e2e'
        VERSION_KEY = 'id'
        XRAY_PLATFORM = 'Android'
        APP_ARTIFACTORY_URL = 'https://artifactory.example.com/mobile'
        LATEST_VERSION = ''
}

如果该路径不起作用,我还能使用什么?想要重新使用从JSON响应中获取的最新版本,以供管道中的下一阶段使用.下一步看起来像这样:

If this path not working, what else could I use? Want to re-use the latest version taken from JSON response for the next stage in the pipeline to use.Next stage looks like this:

stage('Import Result to Xray') {
            when {
                expression { return fileExists('xxx-executor/target/AndroidxxxxE2EResults/cucumber-reports/Cucumber.json')}
            }
            steps {
                xrayResultsImport('xxx-executor/target/AndroidxxxxxE2EResults/cucumber-reports/Cucumber.json', 'xxx_ANDROID_E2E_xxxxxxx_Tests', XRAY_LABELS, ['E2E', 'Android', LATEST_VERSION], env.BUILD_URL)
            }
        }

对不起,由于项目机密性,我不得不输入xxxx来使这个问题更笼统.

Sorry I have to put xxxx to make this question general due to project confidentiality.

推荐答案

简单来说,您想要使用从JSON响应中获取的版本,并希望在Jenkins管道的所有阶段中使用它.

To put it simple, you want to use the version you fetched from a JSON response and want to use it in all stages of your Jenkins pipeline.

确保在jenkins代理中安装了jq实用程序.

Ensure you've jq utility installed in your jenkins agent.

pipeline {
   agent any  
   environment {
     XRAY_LATEST_VERSION = ''
   }
   stages {
        stage(‘Get Version ') {
            steps {
                script {
                    XRAY_LATEST_VERSION = sh(script: 'curl -s ${APP_ARTIFACTORY_URL}/${XRAY_PLATFORM}/builds/latest.json | jq .version | sed \'s/"//g\'', returnStdout: true).trim()
                  }
             }
        } 
        stage('Print'){
            steps{
                echo "${XRAY_LATEST_VERSION}"
            }
        }
    }     
}

您可以在所需的任何阶段使用变量${XRAY_LATEST_VERSION},并且值将呈现出来.

You can use the variable ${XRAY_LATEST_VERSION} in any stages you want the and the value will be rendered across.

这篇关于在Jenkins中执行管道期间如何将动态值传递到环境块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 19:21