简体   繁体   中英

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

}

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 (eg 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.

I am using parallel stages. Here is a minimum example:

pipeline {
  agent any
  options {
    parallelsAlwaysFailFast()  // https://stackoverflow.com/q/54698697/4480139
  }
  stages {

    stage('Parallel') {
      parallel {
        stage('docker-compose up') {
          steps {
            sh 'docker-compose up'
          }
        }
        stage('test') {
          steps {
            sh 'sleep 10'
            sh 'docker-compose down --remove-orphans'
          }
        }
      }
    }
  }

  post { 
    always {
      sh 'docker-compose down --remove-orphans'
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM