Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add sidecar MySQL in declarative Jenkins pipeline?

I am setting up a PHP build system and needs to run a local instance of MySQL for executing tests. Currently I am using declarative pipeline syntax and using docker. Is it possible to run MySQL as a sidecar in declarative syntax ?

If not any other method to run MySQL agent along with a custom docker image and execute migrations ?

like image 567
Happy Coder Avatar asked May 16 '18 11:05

Happy Coder


1 Answers

Currently there is no support for sidecar containers in Jenkins declarative pipelines.

You run MySQL as a sidecar container using scripted pipeline as shown in the Jenkins documentation:

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'
        }
  }
}

You can execute pieces of scripted pipeline in a declarative pipeline using the <script> tag: https://jenkins.io/doc/book/pipeline/syntax/#script

like image 195
Joaquim Oliveira Avatar answered Sep 18 '22 00:09

Joaquim Oliveira