[英]How to create Jenkins shared library that use python scripts?
I created a python script to publish messages on google cloud pubsub and I want to wrap it into a Jenkins shared library.我创建了一个 python 脚本来在谷歌云 pubsub 上发布消息,我想将它包装到一个 Jenkins 共享库中。
Here is my publisher python class这是我的出版商 python class
from google.cloud import pubsub_v1
from google.cloud.pubsub_v1 import futures
from concurrent.futures import TimeoutError
from typing import Mapping, Text
class Publisher:
def __init__(self, project_id: Text, topic_id: Text):
self._publisher = pubsub_v1.PublisherClient()
self._topic_path = self._publisher.topic_path(project_id, topic_id)
@staticmethod
def callback(future: futures.Future) -> None:
try:
message_id = future.result()
print(f"Successful published the message [{message_id}]")
except TimeoutError:
print(f"An error occurred: The request times out.")
except Exception as exception:
print(f"An exception occurred in publishing the message: {exception}")
def publish(self, message: Text, **kwargs: Mapping[Text, Text]) -> None:
data = message.encode("utf-8")
future = self._publisher.publish(topic=self._topic_path, data=data, **kwargs)
future.add_done_callback(self.callback)
And here is the script I want to run inside jenkins pipelines这是我想在 jenkins 管道中运行的脚本
import os
import argparse
from publisher import Publisher
if "__main__" == __name__:
parser = argparse.ArgumentParser(description="Publish a message to a topic in google cloud pubsub.")
parser.add_argument("--project_id", action="store", type=str, required=True, help="The google cloud project ID")
parser.add_argument("--topic_id", action="store", type=str, required=True, help="The google cloud pubsub topic ID")
parser.add_argument("--message", action="store", type=str, required=True,
help="The message you want to publish")
parser.add_argument("--table_name", action="store", type=str, required=False, help="The data table name")
parser.add_argument("--date", action="store", type=str, required=False, help="The date")
parser.add_argument("--test_instance", action="store", type=str, required=False,
default=os.environ.get("TEST_INSTANCE", "False"), help="Is test instance? Default: \"False\"")
args = parser.parse_args()
publisher = Publisher(project_id=args.project_id, topic_id=args.topic_id)
kwargs = {
key: getattr(args, key)
for key in ["table_name", "date", "test_instance"]
if getattr(args, key) is not None
}
publisher.publish(message=args.message, **kwargs)
Could someone helps me?有人可以帮助我吗? I'am new to Jenkins
我是 Jenkins 的新手
Install your scripts on the server, and make sure you can run them from the commandline.在服务器上安装您的脚本,并确保您可以从命令行运行它们。 You may need to set up a virtual environment and mess with permissions.
您可能需要设置一个虚拟环境并弄乱权限。
In jenkins you just add a shell script step that calls the script.在 jenkins 中,您只需添加一个调用脚本的 shell 脚本步骤。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.