<p>I need to build and run some tests using a fresh database. I though of using a sidecar container to host the DB.</p>

<p>I've installed jenkins using helm inside my kubernetes cluster using <a href="https://cloud.google.com/solutions/jenkins-on-kubernetes-engine-tutorial" rel="noreferrer">google's own tutorial</a>.
I can launch simple 'hello world' pipelines which will start on a new pod.</p>

<p>Next, I tried <a href="https://jenkins.io/doc/book/pipeline/docker/#running-sidecar-containers" rel="noreferrer">Jenkin's documentation</a>
for running an instance of mysql as a sidecar.</p>

<pre><code>node {
    checkout scm
    docker.image('mysql:5').withRun('-e "MYSQL_ROOT_PASSWORD=my-secret-pw"') { c -&gt;
        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'
        }
    }
}
</code></pre>

<p>At first, it complained that docker was not found, and the internet suggested using a custom jenkins slave image with docker installed.</p>

<p>Now, if I run the pipeline, it just hangs in the loop waiting for the db to be ready.</p>

<p>Disclaimer: New to jenkins/docker/kubernetes</p>

<p>Eventually I've found <a href="https://dariuszsadowski.com/2018/06/integration-tests-with-jenkins-pipelines-and-mysql-on-kubernetes/" rel="nofollow noreferrer">this method</a>.
It relies on the kubernetes pipeline plugin, and allows running multiple containers in the agent pod while sharing resources.</p>

<p>Note that <code>label</code> should not be an existing label, otherwise when you go to run, your podTemplate will be unable to find the container you made. With this method you are making a new set of containers in an entirely new pod.</p>

<pre><code>def databaseUsername = 'app'
def databasePassword = 'app'
def databaseName = 'app'
def databaseHost = '127.0.0.1'

def jdbcUrl = "jdbc:mariadb://$databaseHost/$databaseName".toString()

podTemplate(
        label: label,
        containers: [
                containerTemplate(
                        name: 'jdk',
                        image: 'openjdk:8-jdk-alpine',
                        ttyEnabled: true,
                        command: 'cat',
                        envVars: [
                                envVar(key: 'JDBC_URL', value: jdbcUrl),
                                envVar(key: 'JDBC_USERNAME', value: databaseUsername),
                                envVar(key: 'JDBC_PASSWORD', value: databasePassword),
                        ]
                ),
                containerTemplate(
                        name: "mariadb",
                        image: "mariadb",
                        envVars: [
                                envVar(key: 'MYSQL_DATABASE', value: databaseName),
                                envVar(key: 'MYSQL_USER', value: databaseUsername),
                                envVar(key: 'MYSQL_PASSWORD', value: databasePassword),
                                envVar(key: 'MYSQL_ROOT_PASSWORD', value: databasePassword)
                        ],
                )
        ]
) {
  node(label) {

      stage('Checkout'){
          checkout scm
      }

      stage('Waiting for environment to start') {
          container('mariadb') {
              sh """
while ! mysqladmin ping --user=$databaseUsername --password=$databasePassword -h$databaseHost --port=3306 --silent; do
    sleep 1
done
"""
          }

      }

      stage('Migrate database') {
          container('jdk') {
              sh './gradlew flywayMigrate -i'
          }
      }

      stage('Run Tests') {
          container('jdk') {
              sh './gradlew test'
          }
      }
  }
}

</code></pre>
