Search code examples
dockerjenkinsdocker-composejenkins-pipelinedevops

In Jenkinsfile choose which Docker services to deploy or stop


I have various services and each of them have docker-compose files to stop or deploy them. Often times use has to choose which services are needed to stop or deploy. Would like to automate this process from now on.

In the below example I use choice parameter

   parameters {
        choice(name: 'ACTION', choices: ['STOP','DEPLOY'], description: 'Please select what to do'),
        choice(name: 'SERVICES', choices: ['PAYMENT','GAMES','LAUNCHER'], description: 'Please choose which services to deploy'),
    }


    stage ('stop services') {
           when {
               allOf {
                    expression { params.ACTION == "STOP" }
                    expression { return params.PAYMENT }
               }
            }
            steps {
                echo "Executing Release\n"
                //somehow refer to a docker-compose file
                sh '''
                 docker-compose.yml -p down '''

            }
    }

Having different stage for each service and action seems cumbersome. Is there a way to choose which services to deploy or stop in one expression or should I create different expression for each service and choice? How to refer to a GitHub-stored docker-compose file within this pipeline?


Solution

  • It seems you are missing a couple of things here: a way to map your service names to their docker-compose files, and a way to tell docker compose which file to work on. I'd do something like this

    def SERVICE_TO_FILE = [
        'PAYMENT': 'docker-compose-payment.yml',
        'GAMES': 'docker-compose-games.yml',
        'LAUNCHER': 'docker-compose-launcher.yml',
    ]
    pipeline {
        agent {...}
        parameters {
            choice(name: 'ACTION', choices: ['STOP','DEPLOY'], description: 'Please select what to do')
            choice(name: 'SERVICES', choices: ['PAYMENT','GAMES','LAUNCHER'], description: 'Please choose which services to deploy')
        }
        stages {
            stage('stop services') {
                when {
                    expression { params.ACTION == "STOP" }
                }
                steps {
                    sh "docker compose --file ${SERVICE_TO_FILE[params.SERVICES]} down"
                }
            }
        }
    }