如何使用curl和API令牌触发参数化的Jenkins作业

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用curl和API令牌触发参数化的Jenkins作业相关的知识,希望对你有一定的参考价值。

我试图使用curl触发Jenkins作业生成SSL证书。 Jenkins作业正在玩具Jenkinsfile上运行,如下所示:

pipeline {
    agent any

    parameters {
        string(name: 'TARGET_ENV', defaultValue: 'dev', description: 'The environment your targeting for the keys to be in.')
        choice(name: 'DRY_RUN', choices: ['true', 'false'], description: 'This will run as dry run to not hit limit.')
    }

    stages {
        stage('Trigger') {
            steps {
                script {
                    println("triggering the pipeline from a rest call...")
                }
            }
        }
        stage("foo") {
            steps {
                echo "flag: ${params.userFlag}"
            }
        }

    }
}

然后我使用的curl命令是

curl https://Ming:API_Token@jenkins.secure.care/job/certificates/job/development/buildWithParameters?DRY_RUN=true&TARGET_ENV=dev --request POST

我得到的错误是关于SSL证书问题,这是我预期的错误,因为我们的SSL证书无效,因此我添加了标志-k以忽略SSL证书错误。

curl -k https://Ming:API_Token@jenkins.secure.care/job/certificates/job/development/buildWithParameters?DRY_RUN=true&TARGET_ENV=dev --request POST

现在我得到了很长的错误

 <!DOCTYPE html><html><head resURL="/static/61cb6928" data-rooturl="" data-resurl="/static/61cb6928">


    <title>Jenkins [Jenkins]</title><link rel="stylesheet" href="/static/61cb6928/css/layout-common.css" type="text/css" /><link rel="stylesheet" href="/static/61cb6928/css/style.css" type="text/css" /><link rel="stylesheet" href="/static/61cb6928/css/color.css" type="text/css" /><link rel="stylesheet" href="/static/61cb6928/css/responsive-grid.css" type="text/css" /><link rel="shortcut icon" href="/static/61cb6928/favicon.ico" type="image/vnd.microsoft.icon" /><link color="black" rel="mask-icon" href="/images/mask-icon.svg" /><script>var isRunAsTest=false; var rootURL=""; var resURL="/static/61cb6928";</script><script src="/static/61cb6928/scripts/prototype.js" type="text/javascript"></script><script src="/static/61cb6928/scripts/behavior.js" type="text/javascript"></script><script src='/adjuncts/61cb6928/org/kohsuke/stapler/bind.js' type='text/javascript'></script><script src="/static/61cb6928/scripts/yui/yahoo/yahoo-min.js"></script><script src="/static/61cb6928/scripts/yui/dom/dom-min.js"></script><script src="/static/61cb6928/scripts/yui/event/event-min.js"></script><script src="/static/61cb6928/scripts/yui/animation/animation-min.js"></script><script src="/static/61cb6928/scripts/yui/dragdrop/dragdrop-min.js"></script><script src="/static/61cb6928/scripts/yui/container/container-min.js"></script><script src="/static/61cb6928/scripts/yui/connection/connection-min.js"></script><script src="/static/61cb6928/scripts/yui/datasource/datasource-min.js"></script><script src="/static/61cb6928/scripts/yui/autocomplete/autocomplete-min.js"></script><script src="/static/61cb6928/scripts/yui/menu/menu-min.js"></script><script src="/static/61cb6928/scripts/yui/element/element-min.js"></script><script src="/static/61cb6928/scripts/yui/button/button-min.js"></script><script src="/static/61cb6928/scripts/yui/storage/storage-min.js"></script><script src="/static/61cb6928/scripts/hudson-behavior.js" type="text/javascript"></script><script src="/static/61cb6928/scripts/sortable.js" type="text/javascript"></script><script>crumb.init("", "");</script><link rel="stylesheet" href="/static/61cb6928/scripts/yui/container/assets/container.css" type="text/css" /><link rel="stylesheet" href="/static/61cb6928/scripts/yui/assets/skins/sam/skin.css" type="text/css" /><link rel="stylesheet" href="/static/61cb6928/scripts/yui/container/assets/skins/sam/container.css" type="text/css" /><link rel="stylesheet" href="/static/61cb6928/scripts/yui/button/assets/skins/sam/button.css" type="text/css" /><link rel="stylesheet" href="/static/61cb6928/scripts/yui/menu/assets/skins/sam/menu.css" type="text/css" /><meta name="ROBOTS" content="INDEX,NOFOLLOW" /><meta name="viewport" content="width=device-width, initial-scale=1" /><script src="/adjuncts/61cb6928/org/kohsuke/stapler/jquery/jquery.full.js" type="text/javascript"></script><script>var Q=jQuery.noConflict()</script><script src="/static/61cb6928/jsbundles/page-init.js" type="text/javascript"></script></head><body data-model-type="hudson.model.Hudson" id="jenkins" class="yui-skin-sam two-column jenkins-2.141" data-version="2.141"><a href="#skip2content" class="skiplink">Skip to content</a><div id="page-head"><div id="header"><div class="logo"><a id="jenkins-home-link" href="/"><img src="/static/61cb6928/images/headshot.png" alt="[Jenkins]" id="jenkins-head-icon" /><img src="/static/61cb6928/images/title.png" alt="Jenkins" width="139" id="jenkins-name-icon" height="34" /></a></div><div class="login"> <a href="/securityRealm/commenceLogin?from=%2Fjob%2Fneb-certificates%2Fjob%2Fdevelopment%2FbuildWithParameters"><b>log in</b></a></div><div class="searchbox hidden-xs"><form role="search" method="get" name="search" action="/search/" style="position:relative;" class="no-json"><div id="search-box-minWidth"></div><div id="search-box-sizer"></div><div id="searchform"><input role="searchbox" name="q" placeholder="search" id="search-box" class="has-default-text" /> <a href="https://jenkins.io/redirect/search-box"><img src="/static/61cb6928/images/16x16/help.png" style="width: 16px; height: 16px; " class="icon-help icon-sm" /></a><div id="search-box-completion"></div><script>createSearchBox("/search/");</script></div></form></div></div><div id="breadcrumbBar"><tr id="top-nav"><td id="left-top-nav" colspan="2"><link rel='stylesheet' href='/adjuncts/61cb6928/lib/layout/breadcrumbs.css' type='text/css' /><script src='/adjuncts/61cb6928/lib/layout/breadcrumbs.js' type='text/javascript'></script><div class="top-sticker noedge"><div class="top-sticker-inner"><div id="right-top-nav"></div><ul id="breadcrumbs"><li class="item"><a href="/" class="model-link inside">Jenkins</a></li><li href="/" class="children"></li></ul><div id="breadcrumb-menu-target"></div></div></div></td></tr></div></div><div id="page-body" class="clear"><div id="side-panel"><div class="task"><a href="https://jenkins.io/" class="task-icon-link"><img src="/static/61cb6928/images/24x24/next.png" style="width: 24px; height: 24px; width: 24px; height: 24px; margin: 2px;" class="icon-next icon-md" /></a> <a href="https://jenkins.io/" class="task-link">Jenkins project</a></div><div class="task"><a href="https://jenkins.io/redirect/report-an-issue" class="task-icon-link"><img src="/static/61cb6928/images/24x24/gear2.png" style="width: 24px; height: 24px; width: 24px; height: 24px; margin: 2px;" class="icon-gear2 icon-md" /></a> <a href="https://jenkins.io/redirect/report-an-issue" class="task-link">Bug tracker</a></div><div class="task"><a href="https://jenkins.io/redirect/mailing-lists" class="task-icon-link"><img src="/static/61cb6928/images/24x24/search.png" style="width: 24px; height: 24px; width: 24px; height: 24px; margin: 2px;" class="icon-search icon-md" /></a> <a href="https://jenkins.io/redirect/mailing-lists" class="task-link">Mailing Lists</a></div><div class="task"><a href="https://twitter.com/jenkinsci" class="task-icon-link"><img src="/static/61cb6928/images/24x24/user.png" style="width: 24px; height: 24px; width: 24px; height: 24px; margin: 2px;" class="icon-user icon-md" /></a> <a href="https://twitter.com/jenkinsci" class="task-link">Twitter: @jenkinsci</a></div></div><div id="main-panel"><a name="skip2content"></a><h1 style="text-align: center"><img src="/static/61cb6928/images/rage.png" width="154" height="179" /><span style="font-size:50px"> Oops!</span></h1><div id="error-description"><p>A problem occurred while processing the request.
        Please check <a href="https://jenkins.io/redirect/issue-tracker">our bug tracker</a> to see if a similar problem has already been reported.
        If it is already reported, please vote and put a comment on it to let us gauge the impact of the problem.
        If you think this is a new issue, please file a new issue.
        When you file an issue, make sure to add the entire stack trace, along with the version of Jenkins and relevant plugins.
        <a href="https://jenkins.io/redirect/users-mailing-list">The users list</a> might be also useful in understanding what has happened.</p><h2>Stack trace</h2><pre style="margin:2em; clear:both">java.lang.IllegalStateException: Unexpected authentication type: org.acegisecurity.providers.UsernamePasswordAuthenticationToken@ede88e28: Username: MingHuang; Password: [PROTECTED]; Authenticated: false; Details: org.acegisecurity.ui.WebAuthenticationDetails@ffffa64e: RemoteIpAddress: 24.234.162.252; SessionId: null; Not granted any authorities
    at com.microsoft.jenkins.azuread.AzureSecurityRealm$3.authenticate(AzureSecurityRealm.java:267)
    at jenkins.security.BasicHeaderRealPasswordAuthenticator.authenticate(BasicHeaderRealPasswordAuthenticator.java:56)
    at jenkins.security.BasicHeaderProcessor.doFilter(BasicHeaderProcessor.java:79)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at org.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:249)
    at hudson.security.HttpSessionContextIntegrationFilter2.doFilter(HttpSessionContextIntegrationFilter2.java:67)
    at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    at hudson.security.ChainedServletFilter.doFilter(ChainedServletFilter.java:90)
    at hudson.security.HudsonFilter.doFilter(HudsonFilter.java:171)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
    at org.kohsuke.stapler.compression.CompressionFilter.doFilter(CompressionFilter.java:49)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
    at hudson.util.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:82)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
    at org.kohsuke.stapler.DiagnosticThreadNameFilter.doFilter(DiagnosticThreadNameFilter.java:30)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:146)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:524)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:257)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1317)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:203)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:201)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1219)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:144)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
    at org.eclipse.jetty.server.Server.handle(Server.java:531)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:352)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:260)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:281)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
    at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:333)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:310)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:168)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:126)
    at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:366)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:762)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:680)
    at java.lang.Thread.run(Thread.java:748)
</pre></div></div></div><footer><div class="container-fluid"><div class="row"><div class="col-md-6" id="footer"></div><div class="col-md-18"><span class="page_generated">Page generated: Apr 2, 2019 6:27:45 PM UTC</span><span class="rest_api"><a href="api/">REST API</a></span><span class="jenkins_ver"><a href="https://jenkins.io/">Jenkins ver. 2.141</a></span></div></div></div></footer></body></html>

实际上我几乎在这里阅读了与我的任务相关的所有帖子,其中没有一个可以帮助我解决问题。有帮助吗?

答案

Jenkins管道不支持远程激活,因此您可以执行以下操作之一,创建一个“适配器”作业,通过UI(不是管道)支持远程激活并触发它,从而触发您的管道。

或使用"build" step,如果它在同一个詹金斯。如果这是一个偏远的詹金斯的工作,你可以尝试“triggerRemoteJob”虽然我没有亲自使用它。

以上是关于如何使用curl和API令牌触发参数化的Jenkins作业的主要内容,如果未能解决你的问题,请参考以下文章

具有用户名和 api 令牌的 CURL 的 Python 等效项

markdown 使用curl从cli获取github API令牌

使用rest api生成jenkins api令牌

curl 命令

如何将 Magento 客户授权令牌从 php curl 传递到浏览器?

Azure:如何获得刷新令牌?当连接的输出仅提供访问令牌时使用 Curl