Jenkins REST API /输入请求无效。无法恢复暂停的工作

时间:2018-04-11 07:13:36

标签: jenkins

版本:jenkins最新

问题摘要

我有一些代码调用Jenkins REST API来恢复暂停的作业。该作业被input()语句暂停。

我的REST请求有时但不总是恢复工作。具体来说,如果作业最近暂停(即一分钟左右),请求将成功,但如果我在请求恢复作业之前等待几分钟,Jenkins将返回404未找到错误。

详细

Jenkins config

  • 禁用安全性。
  • CSRF令牌已停用
  • Jenkins和REST客户端都在同一台机器上。这是一个带代理的公司网络。

Jenkins提取

#Split list into separate intervals
#i.e. [1,3,4,5,7] -> [[1], [3-5], [7]]
def split_list(lst):

  def is_linear(l):
    if len(l)<1: return False
    return sorted(l) == range(min(l), max(l)+1)

  assert isinstance(lst, list)

  lst.sort()

  n = 0
  sub = lst
  out = []
  while len(sub):
    # Search for linear chunk
    m = 0
    while is_linear(sub[:m+1]) and m+n<len(lst):
      m += 1

    out.append(sub[:m])

    # Advance forward - skip found chunk
    n += len(sub[:m])
    sub = lst[n:]

  return out

REST调用以恢复暂停的jenkins工作

请求

http://localhost:9000/job/sirgis-poc/job/qa-dev-35cdedcf-7f88-46e6-bc2e-c805441e5269/1/input/35cdedcf-7f88-46e6-bc2e-c805441e5269/submit?nonce=745493 JSON = { “参数”:[{ “名称”: “IS_APPROVED”, “值”: “Y”}]}&安培;继续= Y

响应

如果在Jenkins暂停作业后立即发出REST请求,则响应为:

stage('Approve') {
    when {
        expression { BRANCH_NAME ==~ BRANCH_QA }
    }
    steps {
        script {
            env.IS_APPROVED = input(
                id: env.JOB_ID,
                message: "Approve release?",
                ok: "y",
                parameters: [
                    string(name: 'IS_APPROVED', defaultValue: 'y', description: 'Deploy to master?')
                ]
            )
            if (env.IS_APPROVED != 'y') {
                currentBuild.result = "ABORTED"
                error "User cancelled"
            }
        }
    } 
}

如果我等待几分钟,然后提出请求,Jenkins会返回以下内容。

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: 

System.Net.Http.NoWriteNoSeekStreamContent, Headers:
{
  Cache-Control: no-store, must-revalidate, no-cache
  Date: Wed, 11 Apr 2018 02:02:50 GMT
  Server: Jetty(9.4.z-SNAPSHOT)
  X-Content-Type-Options: nosniff
  X-Hudson-Theme: default
  X-Hudson: 1.395
  X-Jenkins: 2.107.1
  X-Jenkins-Session: 20efd199
  X-Frame-Options: sameorigin
  Content-Length: 17885
  Content-Type: text/html; charset=utf-8
  Expires: 0
}

1 个答案:

答案 0 :(得分:2)

TLDR

输入步骤ID必须以大写字母开头。

<强>详情

有一个已知的Jenkins问题,如果输入步骤ID不是以大写字母开头,那么恢复暂停作业的REST调用可能会失败。

https://issues.jenkins-ci.org/browse/JENKINS-34509