通过API保护Jenkins构建免受清理

b0zn9rqh  于 2023-05-06  发布在  Jenkins
关注(0)|答案(3)|浏览(143)

我想保护一些Jenkins构建免受自动清理的影响。我已经找到了http://ci.jenkins.com/job/[job_name]/[build_v]/toggleLogKeep,但这需要我检查状态。有没有其他我可以使用的端点。理想情况下,它应该是/keepBuildForever /dontKeepBuildForever

6mzjoqzu

6mzjoqzu1#

看来没有什么好的解决办法。最好的方法是列出所有永久保留的构建。然后检查它是否已经在列表中,如果没有,则点击/toggleLogKeep端点

buildsXml = http://ci.jenkins.com/api/xml?depth=2&xpath=/hudson/job/build[keepLog=%22true%22]/url&wrapper=forever
#check if your build is in buildsXml
wz1wpwve

wz1wpwve2#

在不小心删除了一个重要的构建后,我发现了这个替代解决方案:

# if running inside a job, the following vars are already populated:
#JOB_NAME=yourjobname
#BUILD_NUMBER=123  #your build number
#JENKINS_HOST=192.168.1.11
#JENKINS_PORT=8080
#JENKINS_URL=http://${JENKINS_HOST}:${JENKINS_PORT}

wget --no-check-certificate "${JENKINS_URL}/jnlpJars/jenkins-cli.jar"

java -jar jenkins-cli.jar -s "$JENKINS_URL" keep-build "$JOB_NAME" "$BUILD_NUMBER"
lkaoscv7

lkaoscv73#

我已经为这个做了一个要点
https://gist.github.com/antonio-quarta/357e4391573a503b9c494ab5109e238b

import requests
import os

BUILD_URL = "https://<jenkinsurl>/job/<jobname>/<buildid>/"

JENKINS_USER = "user"

JENKINS_API_TOKEN = os.environ["JENKINS_API_TOKEN"]
toggle_keep_url = BUILD_URL+'toggleLogKeep'

r = requests.post(toggle_keep_url, auth=(JENKINS_USER,JENKINS_API_TOKEN),)
print(r.status_code)

相关问题