【问题标题】:How to add sidecar MySQL in declarative Jenkins pipeline?如何在声明式 Jenkins 管道中添加 Sidecar MySQL?
【发布时间】:2018-10-26 09:41:45
【问题描述】:

我正在设置一个 PHP 构建系统,并且需要运行 MySQL 的本地实例来执行测试。目前我正在使用声明性管道语法并使用 docker。是否可以以声明式语法将 MySQL 作为 sidecar 运行?

如果没有其他方法可以运行 MySQL 代理以及自定义 docker 映像并执行迁移?

【问题讨论】:

    标签: jenkins continuous-integration jenkins-pipeline


    【解决方案1】:

    目前有no support for sidecar containers in Jenkins declarative pipelines

    您使用脚本化管道as shown in the Jenkins documentation 将 MySQL 作为 sidecar 容器运行:

    node {
        checkout scm
        docker.image('mysql:5').withRun('-e "MYSQL_ROOT_PASSWORD=my-secret-pw"') { c ->
            docker.image('mysql:5').inside("--link ${c.id}:db") {
              /* Wait until mysql service is up */
              sh 'while ! mysqladmin ping -hdb --silent; do sleep 1; done'
            }
            docker.image('centos:7').inside("--link ${c.id}:db") {
              /*
               * Run some tests which require MySQL, and assume that it is
               * available on the host name `db`
               */
              sh 'make check'
            }
      }
    }
    

    您可以使用<script> 标签在声明性管道中执行脚本化管道片段:https://jenkins.io/doc/book/pipeline/syntax/#script

    【讨论】:

    • 我试过在干净的工作上运行这个管道。该作业只是在等待 sql server 准备好的 while 循环中永远运行。我在 kubernetes 中运行 jenkins。
    • 拜托,你能解释一下为什么你调用 docker.image('mysql:5') 两次吗?
    • 代码的工作方式与答案中的完全相同。首先,启动镜像,然后在里面运行等待脚本,因为 mysqladmin 只能在镜像内部使用,然后链接到启动的数据库镜像。
    猜你喜欢
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 2018-05-17
    • 2017-08-12
    • 2021-01-15
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多