如何将参数从Jenkins传递到Python

toiithl6  于 2022-11-02  发布在  Jenkins
关注(0)|答案(1)|浏览(453)

我得到了下面的文件,并希望使用参数代替。我如何在Python中传递它们?

import pandas as pd
import mysql.connector
from sqlalchemy import create_engine
from urllib.parse import quote
from io import StringIO
import smtplib, ssl
import mimetypes
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.application import MIMEApplication
from email import encoders
import logging

username = "xxxxx"
password = "yyyyyy"
port = 3306
database = "esxinfo"

engine = create_engine('mysql+mysqlconnector://test:%s@jenkins:3306/esxinfo' % quote('xxxxxx'))
sql = pd.read_sql('SELECT * FROM esx_info', engine)

df = pd.DataFrame(sql)
textStream = StringIO()
df.to_csv(textStream,index=False)

def sendmail():
    msg = MIMEMultipart()
    my_address ="mygmail@gmail.com"
    app_generated_password = "xxxxxxxxxxxx" 
    msg['Subject'] = "The Email Subject"
    msg["From"]= my_address
    msg['To'] = "yyyyyyyyyyy
    msg.add_header('Content-Type','text/html')
    #msg.set_content("This is the body of the email") 
    #msg.attach(MIMEText(message, 'html'))
    textStream = StringIO()
    df.to_csv(textStream,index=False)
    msg.attach(MIMEApplication(textStream.getvalue(), Name="test.csv"))
    with smtplib.SMTP_SSL("smtp.gmail.com", 465) as smtp:
            smtp.login(my_address,app_generated_password)    #login gmail account
            print("sending mail")
            smtp.send_message(msg)   #send message 
            print("mail has sent")

sendmail()

说我想通过

my_address ="mygmail@gmail.com"

我该怎么做而不需要输入代码呢?我想用Jenkins的参数运行这个程序。

8cdiaqws

8cdiaqws1#

Jenkins有一个Python脚本的plugin,你可以添加参数到你的“参数构建”中,例如添加一个字符串参数,然后在脚本中像%MY_ADDRESS%一样调用它们。
然而,参数构建的缺点是每次启动作业时都需要填写此信息......如果每次运行都是相同的,您当然可以潜在地自动化此信息。
例如,我有一个脚本,它需要每天晚上删除一个工作区进行清理。参数是“分支”,它传递我的分支名称,在脚本本身中,我通过C:\Path\To\%Branch%\调用路径

相关问题