【问题标题】:If else conditions for docker container in jenkins pipeline scriptjenkins 管道脚本中 docker 容器的 if else 条件
【发布时间】:2021-01-13 21:48:30
【问题描述】:

我在 Jenkins 中创建了一个管道,其步骤是:

  1. git pull(从 GitHub 获取应用)
  2. 构建(maven 打包应用)
  3. 构建图像
  4. 运行图像

运行图像的代码是 docker run -d --name **application_name** -p 8081:8081 **application_name**

我需要一个 if-else 条件来检查图像是否已创建。 例如。

if (image not created)
     create the image
else
     different step

我需要这个,因为每次我更改代码中的某些内容时,Jenkins 都会抛出一个错误,指出映像已经在运行。

【问题讨论】:

  • 您可以编辑问题并添加您当前的管道脚本吗?即使容器已经在运行,您也应该能够重建映像。

标签: docker jenkins jenkins-groovy jenkins-docker


【解决方案1】:

错误可能与this question有关。

无论如何,您都可以构建映像,因为再次构建它并不重要。

如果您想检查图像是否已经存在,您可以使用docker images 命令并检查图像是否存在,如here 所述。

它可能看起来像这样:

script {
    def imageExists = sh(script: "docker images -q <image name>", returnStdout: true) == 0

    if(!imageExists){
        // build the image
    }
}

【讨论】:

    猜你喜欢
    • 2020-07-19
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2018-03-27
    • 2021-12-05
    • 1970-01-01
    相关资源
    最近更新 更多