栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

通过Jenkins Script 获取token,往postman 传递环境变量

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过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的方式运行。如下。

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/838004.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号