【问题标题】:How to programmatically set the github repo inside jenkins job configuration page如何以编程方式在 jenkins 作业配置页面中设置 github repo
【发布时间】:2016-01-16 20:23:50
【问题描述】:

我想在每次生成新的 Jenkins 容器时通过代码在 http://host-ip:8080/job/my_project/configurejenkins 页面中设置 Github 存储库 url。

我读到这可以通过替换 config.xml 使用 python-jenkins 的 reconfig_job function 来完成。

那我该怎么做呢?

【问题讨论】:

    标签: python github jenkins docker


    【解决方案1】:

    你在“How can I update a jenkins job using the api?”中有一些线索

    例如,由于您生成了一个新的 Jenkins 容器,您可以docker cp将更新后的config.xml 发送到该容器(在该作业的正确路径上)

    OP Kostas Demiris 确认 in the comments 在 git bash 中运行时有效)

    您也可以使用Jenkins API libraries 之一,但请检查simple curl is enough first

    #Get the current configuration and save it locally
    curl -X GET http://user:password@jenkins.server.org/job/myjobname/config.xml -o mylocalconfig.xml
    
    #Update the configuration via posting a local configuration file
    curl -X POST http://user:password@jenkins.server.org/job/myjobname/config.xml --data-binary "@mymodifiedlocalconfig.xml"
    

    updated Jenkins doc mentions(仅用于更新现有配置作业中的一个参数):

    Simple example - sending "String Parameters":
    
    curl -X POST JENKINS_URL/job/JOB_NAME/build \
      --data token=TOKEN \
      --data-urlencode json='{"parameter": [{"name":"id", "value":"123"}, {"name":"verbosity", "value":"high"}]}'
    
    Another example - sending a "File Parameter":
    
    curl -X POST JENKINS_URL/job/JOB_NAME/build \
      --user USER:PASSWORD \
      --form file0=@PATH_TO_FILE \
      --form json='{"parameter": [{"name":"FILE_LOCATION_AS_SET_IN_JENKINS", "file":"file0"}]}'
    

    【讨论】:

    • 使用此命令:curl -X POST username:password@host.com:8686/job/MyJob/config.xml --data-binary "@new_config.xml" from the directory of new_config PowerShell 抛出“Invoke-WebRequest:找不到与参数匹配的参数名称'X''
    • @KostasDemiris 太棒了!我已将您的评论包含在答案中以提高知名度。
    猜你喜欢
    • 2014-06-26
    • 2014-03-01
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 2011-10-02
    • 2011-04-06
    相关资源
    最近更新 更多