从 jenkins 凭证管理器读取凭证并与 python 脚本集成

问题描述 投票:0回答:2

我有一个 python 脚本,它进行几个 api 调用并通过电子邮件将响应返回给我。我想通过詹金斯管道作业运行这个脚本。 我有一个令牌,我已将其作为秘密文本存储在詹金斯凭证管理器中。 问题是我不确定如何在 python 脚本中获取这个令牌。我尝试过寻找多种解决方案,但所有这些都让我感到困惑。 这就是我的詹金斯管道的样子:

pipeline {
    agent {
        node {
            label 'node1'
        }
    }
    environment {
        deva_stross_token=credentials('devadrita-stross') //i have saved the credential with id 'devadrita-stross', and this I understand, is fetching it for my pipeline
    }
    stages {
        stage('running python script') {
            steps {
                script {
                    bat """
                    python -u C://Users//Administrator//Desktop//stross//stross-script.py
                    """
                }
            }       
        }
    }
}

但是我应该做哪些更改才能将其提取到我的脚本中? 这是Python脚本。

import requests
import urllib3
import json
import time
import os

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

def initiateScan():
    url = ""

    payload={}
    files=[
    ('source',('amail.zip',open('C:/Users/Administrator/Desktop/stross/amail.zip','rb'),'application/zip')),
    ('metadata',('metadata.json',open('C:/Users/Administrator/Desktop/stross/metadata.json','rb'),'application/json'))
    ]
    headers = {
    'Authorization': ' Bearer **<token required here>**'
    }

    response = requests.request("POST", url, headers=headers, data=payload, files=files, verify=False)

    resp=response.json()
    print(resp)
    
    jobId=resp["job_id"]
    return(jobId)
    
def main():
   jobIdFromInitiate=initiateScan()

main()

提前感谢您的帮助!

编辑- 我知道有几种方法可以做到这一点,一种是使用凭据(),另一种是使用环境变量。问题是我将这两种方法混合在一起,并且无法通过代码实现这两种方法。所以请帮忙编写代码。

编辑1- 将 @rok 的答案分为要遵循的步骤- “(1) 将 withCredentials 块添加到您的 Jenkinsfile 中以读取凭据,并 (2) 将它们绑定到环境变量。然后,(3) 将环境变量的值作为参数传递给您的 python 脚本。”

pipeline {
    agent {
        node {
            label 'node1'
        }
    }
    environment {
        deva_stross_token=credentials('devadrita-stross')
    }
    withCredentials([string(credentialsId: 'devadrita-stross', variable: 'deva-stross-token')]) {
    // some block
    }
    stages {
        stage('saving stross token and printing') {
            steps {
                script {
                    bat """
                    python -u C://Users//Administrator//Desktop//stross//stross-script.py
                    """
                }
            }       
        }
    }
}

(1) 添加了 withCredentials 块用于读取凭证 (2) 凭证与环境变量绑定是什么意思? withCredentials 块是否将令牌绑定到环境变量“deva-stross-token”?我的环境块在这里有用或需要吗? (3) 如何将环境变量的值传递给我的python脚本?

我觉得我应该补充一点,我对编码还比较陌生,所以如果我的问题看起来非常基本,请耐心等待。

python batch-file environment-variables jenkins-plugins credential-manager
2个回答
1
投票

使用凭证绑定插件。将

withCredentials
块添加到 Jenkinsfile 中,用于读取凭据并将其绑定到环境变量。然后,将环境变量的值作为参数传递给您的 python 脚本。

Jenkins docs 列出支持的凭证类型和各种绑定。


0
投票

这对我有用: 詹金斯文件:

stages {
        stage('run stross script') {
            steps{
                withCredentials([string(credentialsId: 'devadrita-stross', variable: 'my_stross_token')]) {
                echo "My stross token is '${my_stross_token}'!"
                bat "python C:\\Users\\Administrator\\Desktop\\stross\\stross-script.py "
                }
            }
        }
    }

这会将我的凭据绑定到指定的环境变量。 然后,我将该变量导入到我的 python 脚本中,并像任何其他变量一样使用它,即将它传递给函数等。 这是一个代码片段:

def main():
    token = os.environ['my_stross_token']
    print(token)
    jobIdFromInitiate=initiateScan(token)
    getStatus(jobIdFromInitiate, token)
© www.soinside.com 2019 - 2024. All rights reserved.