如何在 Jenkins Blue Ocean 中运行“sidecar”容器?

Neo*_*734 2 jenkins docker jenkins-pipeline jenkins-blueocean

总的来说,我对 Jenkins 和 CI/CD 相当陌生,但相信我已经搜索了足够长的时间来得出结论,事情并不像我预期的那样。

我想在我的网站上进行一些前端测试,就像在现实生活中一样,我想在一个 Docker 容器中测试网站,在另一个容器中测试数据库。Jenkins 将此记录为“sidecar”容器,它可以是管道的一部分。

他们的例子:

node {
    checkout scm
    /*
     * In order to communicate with the MySQL server, this Pipeline explicitly
     * maps the port (`3306`) to a known port on the host machine.
     */
    docker.image('mysql:5').withRun('-e "MYSQL_ROOT_PASSWORD=my-secret-pw" -p 3306:3306') { c ->
        /* Wait until mysql service is up */
        sh 'while ! mysqladmin ping -h0.0.0.0 --silent; do sleep 1; done'
        /* Run some tests which require MySQL */
        sh 'make check'
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是我没有“传统”Jenkins 管道,但我正在运行 Jenkins Blue Ocean。这为我提供了一个精美的管道编辑器,但我的管道代码(Jenkinsfile)看起来与示例非常不同:

pipeline {
  agent {
    docker {
      image 'php'
    }

  }
  stages {
    stage('Build') {
      steps {
        sh 'composer --version'
        sh 'composer install'
      }
    }

    stage('Tests') {
      steps {
        echo 'Do test'
      }
    }

  }
}
Run Code Online (Sandbox Code Playgroud)

那么我将如何在蓝海管道中生成(并拆除)这些“sidecar”容器呢?目前,如果我想添加与 Docker 相关的步骤,管道编辑器没有可用的选项。我还能用吗docker.image?我确实安装了Docker Pipeline 插件

没有可用的步骤

小智 5

Jenkins 在链接中提供的示例实际上是一个功能齐全的管道,但有一个例外。checkout scm如果直接向 Jenkins 提供管道脚本,则需要注释掉。

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'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

您可能会感到困惑的是,上面示例中的代码风格与 Blue Ocean 管道编辑器生成的代码风格非常不同。那是因为脚本是在Scripted Pipeline中编写的,而 Blue Ocean 生成了Declarative Pipeline。两者都在 Jenkins 中得到完全支持,并且底层都使用相同的引擎,但语法差异可能会导致启动时出现混乱。

您可以使用上面的脚本化管道示例,但如果您想保留声明式管道,您可以在步骤内运行脚本化部分script。在这两种情况下,您都需要根据需要更改 docker 映像并执行命令。

pipeline {
    agent any
    stages {
        stage('Build and test') {
            steps {
                script {
                    node {
                        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'
                            }
                        }
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意:

  1. 本示例中使用的 Docker 容器链接功能是一项遗留功能,它最终可能会被删除。
  2. 管道将在 处失败make check,正如图像make中未提供的那样centos:7