IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> 通过Jenkins Script 获取token,往postman 传递环境变量 -> 正文阅读

[开发测试]通过Jenkins Script 获取token,往postman 传递环境变量

参考:??????Feature Request: read variables from OS environment · Issue #1603 · postmanlabs/postman-app-support · GitHub

maven - Jenkins CI Pipeline Scripts not permitted to use method groovy.lang.GroovyObject - Stack Overflow

1. 通过Jenkins Script 获取token

2. 在启动时将环境变量传递给postman test

#!groovy

timeout(time: 2, unit: 'HOURS') {
    podTemplate(label: label,
    	containers:
    	[
        		containerTemplate(name: 'newman', image: '*****/cibuildslave/newman_html:newman_html_cms', privileged: false, command: 'cat', ttyEnabled: true, alwaysPullImage: true),
                containerTemplate(name: 'python', image: '******/cibuildslave/python-http:python-http', privileged: false, command: 'cat', ttyEnabled: true, alwaysPullImage: true)
        ],
        volumes: [emptyDirVolume(mountPath: '/home/jenkins', memory: true)],
        imagePullSecrets: ['jfrog-authenticate']
    	){
        node(label) {
            
            collection_result1 = "SUCCESS"
            

            stage('Checkout GIT') {
                    git branch: 'fix/postman-vault', credentialsId: 'bitbucket-2', url: 'ssh://git@altssh.bitbucket.org:443/*******.git'
            }

            stage('Smoke Test') {
                container('newman') {

                    def responseCode,responseText

                    withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'vault_nonprod_access_qa', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
                        echo 'vault credentials'
                        echo env.USERNAME
                        echo env.PASSWORD

                        def vaultUrl = '**********'


                        def data = /{
                                    "role_id": "${env.USERNAME}",
                                    "secret_id": "${env.PASSWORD}"
                                }/





                        try {
                            def url = new URL(vaultUrl)
                            def conn = (HttpURLConnection) url.openConnection()
                            conn.setRequestMethod('POST')
                            conn.setDoOutput(true)
                            conn.setDoInput(true)
                            conn.setUseCaches(false)
                            conn.setRequestProperty('Content-Type', 'application/json')

                            try {
                                conn.connect()

                                OutputStream outputStream = conn.getOutputStream()
                                outputStream.write(data.getBytes())
                                outputStream.flush()
                                outputStream.close()

                                responseCode = conn.getResponseCode()
                                responseText = conn.getInputStream().getText()
                                echo 'responseCode: ' + responseCode
                                echo 'responseText: ' + responseText

                                //logger.info("ResponseCode is:" + responseCode)
                            } catch (exception) {
                                if (responseCode != 200 && responseCode != 204) {
                                    exception.printStackTrace()
                                }
                            }
                            finally {
                                if (conn != null) {
                                    conn.disconnect()
                                }
                            }
                        } catch (exception) {
                            print 'Exception happen in getVaultAuthorization, Exception:' + exception.message
                            Thread.sleep(1000 * 30) //wait 30 seconds and try again

                            //response = httpClient.post(vaultUrl, body, 'application/json')
                        } finally {
                            //if (response == null || response.code != 200) {
                              //  Thread.sleep(1000 * 30) //wait 30 seconds and try again
                                //response = httpClient.post(vaultUrl, body, 'application/json')
                            //}

                            print 'finally response.code=' + responseCode
                        }

                        //assert response.code == 200, "failed to get vault Authorization. response: " + response.data
                        assert responseCode == 200, "failed to get vault Authorization. responseText: " + responseText


                        def vaultResponseText = responseText
                        def jsonObj = new groovy.json.JsonSlurperClassic().parseText(vaultResponseText)
                        vaultToken = jsonObj['auth']['client_token']
                        echo 'Vault_token: ' + vaultToken
                        envVars=['VAULT_TOKEN='+vaultToken]
                    }

                    withEnv(envVars){
                        echo 'entering newman'
                        args = " -d ${userDefinedData} --global-var VAULT_TOKEN=$VAULT_TOKEN -e ${postmanEnvironment} --reporters cli,html --reporter-html-template ${postmanHTMLTemplate} --insecure"

                        try{
                            echo 'newman collection1 testing'
                            cmd= "newman run ${postmanCollection1}"+ args +" --reporter-html-export ${postmanHTMLResults}/${postmanHTMLResultsFile1}"
                            echo cmd
                            sh cmd
                        }catch(Exception e){
                            echo 'error with collection1'
                            print e
                            currentBuild.result = "UNSTABLE"
                            collection_result1 = "UNSTABLE"

                        }
                        
                        echo 'exit newman'


                    }

                }
            }

           

        }
    }
}







3. 上述script不可以运行在Jenkins Sandbox中,需要用Jenkins Script的方式运行。如下。

?

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2022-04-27 11:36:23  更:2022-04-27 11:36:31 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 5:59:49-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码