简体   繁体   English

如何创建使用 python 脚本的 Jenkins 共享库?

[英]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.

相关问题 Jenkins共享库:如何从包中包含的python模块运行代码? - Jenkins shared library: How to run code from a python module contained in a package? 如何配置 Jenkins 以运行 Python 脚本? - How to configure Jenkins to run Python scripts? 如何在 Jenkins UI 中执行本地 python 脚本 - How to execute local python scripts in Jenkins UI Chicken for python:使用共享库扩展python - Chicken for python: Extending python with the use of a shared library 如何创建可可库并在python中使用 - How to create a Cocoa library and use it in python 用C ++创建一个共享库以与Python集成 - Create a shared library in C++ to integrate with Python 使用Jenkins运行Python脚本 - Running Python Scripts with Jenkins 为Python脚本创建共享消息流的最佳方法是什么? - What is the best way to create shared message stream for Python scripts? 如何创建和使用共享 memory 项的两个 Python 迭代,并在使用过程中维护共享 memory? - How to create and use two Python iterables of shared memory items, and maintain the shared memory during usage? 如何在Python中编译,创建共享库和导入C ++ Boost模块 - How to compile, create shared library, and import c++ boost module in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM