User Tools

Site Tools


informatica:linux:jenkins

Pipelines

* Pipeline declarativa * Debe ser el archivo 'Jenkinsfile' en la raíz * En Jenkins debe crearse el job como “pipeline” * Ejecuta el stage solo si se modifica el archivo que toca

// If referencing variables inside functions, don' use 'def' keyword
// https://stackoverflow.com/a/47007544/2695864
app_docker_image = "web-acme"
app_docker_version = "latest"
db_docker_image = "${app_docker_image}-db"
db_docker_version = "${app_docker_version}"
db_siteurl_old = "localhost:8000"
// TODO: change below URL once in production
db_siteurl_new = "acme.example.com"

pipeline {
    agent any

    environment {
        _DOCKER_REGISTRY_CREDENTIALS = credentials('jenkins.bot_username_password')
    }

    stages {
        stage('Build app') {
            when { changeset "wordpress/*"}
            steps {
                docker_registry_login()
                dir ('docker') { 
                    build_app(app_docker_version)
                    build_app(gitlabAfter)
                }
            }
        }
        stage('Build DB') {
            when { changeset "docker/docker-entrypoint-initdb.d/wordpress.sql"}
            steps {
                docker_registry_login()
                dir ('docker') { 
                    build_db(db_docker_version)
                    build_db(gitlabAfter)
                }
            }
        }
    }
}

def docker_registry_login() {
    sh "bash -c 'echo ${_DOCKER_REGISTRY_CREDENTIALS_PSW} | docker login ${_DOCKER_REGISTRY} --username ${_DOCKER_REGISTRY_CREDENTIALS_USR} --password-stdin'"
}

def build_app(version) {
    sh "bash -c './build-app.sh --registry ${_DOCKER_REGISTRY} --image ${app_docker_image} --version version'"
}

def build_db(version) {
    sh "bash -c './build-db.sh --registry ${_DOCKER_REGISTRY} --image ${db_docker_image} --version version --old $db_siteurl_old --new $db_siteurl_new'"
}
informatica/linux/jenkins.txt · Last modified: 2020/10/13 14:41 by javi