简体   繁体   English

Jenkins作业无法在构建期间将整数参数传递给python脚本

[英]Jenkins job unable to pass integer parameters into python script during build

I have a python script that I tested on an EC2 instance, which works perfectly fine on the server but when I try to pass the same parameters from Jenkins job as shown below. 我有一个在EC2实例上测试过的python脚本,该脚本在服务器上可以正常运行,但是当我尝试从Jenkins作业传递相同的参数时,如下所示。 I get the error message: 我收到错误消息:

Security Group Created sg-ca09bcae in vpc vpc-d79691b9 . 安全组在vpc vpc-d79691b9中创建了sg -ca09bcae Traceback (most recent call last): File "./create_sg.py", line 32, in 'FromPort': int(FROM_PORT_1.strip("")), marked build as failure Finished: FAILURE> ValueError: invalid literal for int() with base 10: 'within' Build step 'Execute shell' 追溯(最近一次呼叫最近):文件“ ./create_sg.py”,位于'FromPort'中的第32行:int(FROM_PORT_1.strip(“”))),标记为构建失败完成:FAILURE> ValueError:int的无效文字()以10为基数:“在”构建步骤“执行外壳”中

詹金斯职位参数

I'm pretty sure the error is because of the string parameter that I'm passing through Jenkins parameters but there isn't an option to send both From and To ports as integers in jenkins parameters. 我很确定该错误是由于我通过Jenkins参数传递的字符串参数引起的,但是在jenkins参数中没有选择将From和To端口都作为整数发送。

How can I set the parameters to an integer within Jenkins build job? 如何在Jenkins构建作业中将参数设置为整​​数?

Python code to create SG: 创建SG的Python代码:

#!/usr/bin/env python

import sys
import boto3
from botocore.exceptions import ClientError
region = "us-west-1"

VPC_ID=sys.argv[1]
SECURITY_GROUP_NAME=sys.argv[2]
DESCRIPTION=sys.argv[3]
IP_PROTOCOL_1=sys.argv[4]
FROM_PORT_1=sys.argv[5]
TO_PORT_1=sys.argv[6]
CIDR_IP_1=sys.argv[7]


ec2 = boto3.client('ec2')

response = ec2.describe_vpcs()

vpc_id = VPC_ID

try:
    response = ec2.create_security_group(GroupName=SECURITY_GROUP_NAME,Description=DESCRIPTION,VpcId=VPC_ID)
    security_group_id = response['GroupId']
    print('Security Group Created %s in vpc %s.' % (security_group_id, vpc_id))

    data = ec2.authorize_security_group_ingress(
        GroupId=security_group_id,
        IpPermissions=[
            {'IpProtocol': IP_PROTOCOL_1,
             'FromPort': int(FROM_PORT_1),
             'ToPort': int(TO_PORT_1),
             'IpRanges': [{'CidrIp': CIDR_IP_1}]}
        ]
    )
    print('Ingress Successfully Set %s' % data)
except ClientError as e:
    print(e)

@nosklo, Suggested I try the following: @nosklo,建议我尝试以下操作:

VPC_ID=sys.argv[1] 
SECURITY_GROUP_NAME=sys.argv[2]
DESCRIPTION=' '.join(sys.argv[3:-4])
IP_PROTOCOL_1=sys.argv[-4]
FROM_PORT_1=sys.argv[-3]
TO_PORT_1=sys.argv[-2] 
CIDR_IP_1=sys.argv[-1]

With which the ports are listed as 0 vs the values that I passed in the parameters. 与端口相对于我在参数中传递的值一起列出的0。

I haven't any way to test this, but the error message shows the invalid int value as the word "within". 我没有任何测试方法,但是错误消息将无效的int值显示为单词“ inside”。 That suggests that the Description parameter is being substituted directly onto a command line without any quoting, so that sys.argv[5] would indeed be the string "within". 这表明Description参数直接被替换为命令行而没有任何引号,因此sys.argv[5]的确是字符串“ within”。 You might try including quotes around the description: `"Security group within dev environment". 您可以尝试在描述周围加上引号:`“开发环境中的安全组”。

I think what you're looking for is os.getenv : 我认为您正在寻找的是os.getenv

import os

VPC_ID = os.getenv('VPC_ID')

print(VPC_ID)

should work. 应该管用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM