Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a docker-compose instance in jenkins pipeline

I've set up a home based CI server for working with a personal project. Below you can see what happens for the branch "staging". It works fine, however the problems with such a pipeline config are:

1) The only way to stop the instance seem to be to abort the build in jenkins whiсh leads to the exit code 143 and build marked as red instead of green

2) If the machine reboots I have to trigger build manually

3) I suppose there should be a better way of handling this?

Thanks

stage('Staging') {

  when {
    branch 'staging'
  }

  environment {
    NODE_ENV = 'production'
  }

  steps {
    sh 'docker-compose -f docker-compose/staging.yml build'
    sh 'docker-compose -f docker-compose/staging.yml up --abort-on-container-exit'
  }

  post {
    always {
      sh 'docker-compose -f docker-compose/staging.yml rm -f -s'
      sh 'docker-compose -f docker-compose/staging.yml down --rmi local --remove-orphans'
    }
  }

}
like image 892
Daniel Khoroshko Avatar asked Jan 13 '18 11:01

Daniel Khoroshko


People also ask

Can we run Docker image from Jenkins?

Note that while recent versions of Windows gained native support for running Docker images, Jenkins only provides Linux based Docker images. Windows and macOS can run Linux Docker images through virtualization, so most of the commands shown here apply equally to all operating systems, but this post will focus on Linux.


1 Answers

So, what's the goal here? Are you trying to deploy to staging? If so, what do you mean by that? If jenkins is to launch a long running process (say a docker container running a webserver) then the shell command line must be able to start and then have its exit status tell jenkins pipeline if the start was successful.

One option is to wrap the docker compose in a script that executes, checks and exits with the appropriate exit code. Another is to use yet another automation tool to help (e.g. ansible)

The first question remains, what are you trying to get jenkins to do and how on the commandline will that work. If you can model the command line then you can encapsulate in a script file and have jenkins start it.

Jenkins pipeline code looks like groovy and is much like groovy. This can make us believe that adding complex logic to the pipeline is a good idea, but this turns jenkins into our IDE and that's hard to debug and a trap into which I've fallen several times.

A somewhat easier approach is to have some other tool allow you to easily test on the commandline and then have jenkins build the environment in which to run that command line process. Jenkins handles what it is good at:

  • scheduling jobs
  • determining on which nodes jobs run
  • running steps in parallel
  • making the output pretty or easily understood by we carbon based life forms.
like image 102
Eva Brigid Avatar answered Sep 20 '22 14:09

Eva Brigid