詹金斯搭建
我的一个项目使用Artifactory作为存储库管理器。 不幸的是,在进行Jenkins 管道构建时,有时会忘记确保Artifactory服务器先启动,并在运行一段时间后发现作业失败。
我已经在我的Jenkinsfile文件中添加了一些脚本,该脚本将尽早检查Artifactory服务器,如果未运行,则会快速失败。
人工检查
出于我的目的,我只是尝试ping Artifactory服务器 。 这可以通过发送http请求来完成: http:// [您的Artifactory URL] / artifactory / api / system / ping ,如果成功,则应返回字符串“ OK”。
詹金斯管道示例
此特定示例要求安装HTTP请求插件 。 在实际构建Artifactory检查之前,我在管道中添加了一个声明阶段。
在此阶段,如果响应状态代码在默认范围内(100到399),并且响应内容包括字符串“ OK”,则来自插件的HTTP请求调用将成功。 如果响应不满足这些条件,那么詹金斯的工作将很快失败。
pipeline {
agent any
options {
// Stop the build early in case of compile or test failures
skipStagesAfterUnstable()
}
stages {
stage( 'Build' ) {
environment {
// artifactory server url
artifactoryUrl = ' http:// [Your Artifactory URL]/artifactory'
}
// stage to check the Artifactory server is up, else will fail the job
stage( 'Artifactory check' ) {
steps {
script {
echo 'Pinging Artifactory'
// for a successful ping, the response status code must be in default acceptable range (100:399)
// and contain 'OK' in the content
def pingResponse = httpRequest url: "${artifactoryUrl}/api/system/ping" , validResponseContent: 'OK'
echo "Ping response status code: ${pingResponse.status}"
echo "Ping response: ${pingResponse.content}"
}
}
}
// continue with other stages for the job
或者,如果您不希望Artifactory检查使作业失败,则只需将HTTP请求的参数更改为允许所有响应状态代码和任何响应内容通过。 然后,如果ping操作失败,则可以设置一个标志,发送通知,打印一些信息等,然后让作业继续进行。
stage( 'Artifactory check' ) {
steps {
script {
// Allow all response codes returned from the Artifactory ping request so it doesn't fail,
// normal allowable codes are 100:399.
def pingResponse = httpRequest url: "${artifactoryUrl}${artifactoryPingPath}" , validResponseCodes: '100:599'
echo "Ping response status code: ${pingResponse.status}"
echo "Ping response: ${pingResponse.content}"
if (pingResponse.status == 200 && pingResponse.content == 'OK' )
// flag successful check
else
// flag ping failure
}
} }
翻译自: https://www.javacodegeeks.com/2020/05/checking-for-artifactory-in-a-jenkins-pipeline.html
詹金斯搭建