conn_id 未在 Airflow JenkinsJobTriggerOperator 中定义

The conn_id isn't defined in Airflow JenkinsJobTriggerOperator

我正在尝试使用 Airflow dag 在 Jenkins 中触发作业。我尝试了以下,

dag = DAG("test_jenkins", default_args=default_args, schedule_interval=None)

job_trigger = JenkinsJobTriggerOperator(
    dag=dag,
    task_id="trigger_job",
    job_name="generate-code-trigger",
    jenkins_connection_id="http://localhost:8080/"  # The connection must be configured first
)

def grab_artifact_from_jenkins(**context):
hook = JenkinsHook("http://localhost:8080/")
jenkins_server = hook.get_jenkins_server()
url = context['task_instance'].xcom_pull(task_ids='trigger_job')
url = url + "code-generator/pom.xml"  # Or any other artifact name
request = Request(url)
response = jenkins_server.jenkins_open(request)
return response  # We store the artifact content in a xcom variable for later use

artifact_grabber = PythonOperator(
    task_id='artifact_grabber',
    provide_context=True,
    python_callable=grab_artifact_from_jenkins,
    dag=dag)

artifact_grabber.set_upstream(job_trigger)

但这给了我一个错误,

The conn_id `http://localhost:8080/` isn't defined

这是我第一次使用 JenkinsJobTriggerOperator。周围也没有任何有用的例子。我怎样才能避免这个错误。

你对jenkins_connection_id有误解。

jenkins_connection_id="http://localhost:8080/"  # The connection must be configured first

您需要先通过气流 UI 创建与您的 jenkins 服务器的正确连接。您现在设置的是一个示例字符串。 它应该看起来像这样:

jenkins_connection_id="the_connection_id_name_which_you_gave_through_the_ui" 

您可以在此处阅读详细信息: https://airflow.apache.org/docs/stable/howto/connection/index.html

我试过类似的方法,它可以与 bash 运算符一起使用。

jenkins_iuser_id = Variable.get("jenkins_user_name")
jenkins_pswd = Variable.get("api_token")

cmand_to_exec ='curl -X POST --user ' + jenkins_iuser_id + ':' + jenkins_pswd + ' http://< IP >:8080/job/<JOBNAME>/build '
trigger_cleanup = BashOperator(
        task_id='trigger_jenkins_cleanup_jobs',
        bash_command=cmand_to_exec,
        dag=dag)
trigger_cleanup