Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Building a mdbook with Jenkins

Use the following Jenkinsfile and customize it to your needs:

// Don't forget to add http://jenkins.dandaedre.com/gitea-webhook/post as a gitea webhook if is not in your organisation

pipeline {
    agent { label 'rust_no_docker' }                // any COULD work, but prefer this label for mdbooks
    /* ===============
    BEGIN USER CONFIG
    ================== */
    environment {
        // The book name careful as spaces aren't really well supported (rsync command)
        BOOK_NAME = "test_book"
        // Change with desired plugins, comment the node "Install plugins if no plugins are needed"
        MDB_PLUGIN_LIST = "mdbook-admonish mdbook-hide"

        // Book is usually a good intermediate name
        MDBOOK_BUILD_DIR = "book"
        // Infered deploy directory
        DEPLOY_DIR = "/mnt/books/$BOOK_NAME"
    }

    /* ===============
     END USER CONFIG
    ================== */

    stages {
        stage('Checkout') {
            steps {
                // Checkout intead of full clone
                checkout scm
            }
        }

        stage('Install mdBook') {
            steps {
                // Install the latest mdBooks
                sh 'cargo install mdbook && echo "mdBook already installed"'
            }
        }

        stage('Install plugins'){
            steps {
                sh 'cargo install ${MDB_PLUGIN_LIST}'
            }
        }

        stage('Build Book') {
            steps {
                sh 'mdbook build'
            }
        }

        stage('Scan Warnings') {
            steps {
                // warnError('Scanning warnings') {
                // withChecks('Parsing: ', includeStage: true) {
                    recordIssues tools: [
                        groovyScript('mdbook_preprocessor_import')
                    ],
                    qualityGates: [
                        [threshold: 1, type: 'TOTAL', unstable: true, criticality: 'UNSTABLE'],
                        [threshold: 1, type: 'TOTAL_ERROR', unstable: true, criticality: 'UNSTABLE'] 
                    ]
                // }
            }
        }

        stage('Deploy to Books') {
            steps {
                sh 'echo "Deploying to ${DEPLOY_DIR}"'
                // This would be the nuclear approach
                sh 'mkdir -p ${DEPLOY_DIR}'
                // sh 'rm -rf ${DEPLOY_DIR}/*'
                // sh 'cp -r ${MDBOOK_BUILD_DIR}/* ${DEPLOY_DIR}/'
                sh 'rsync -a --delete "${MDBOOK_BUILD_DIR}/" "${DEPLOY_DIR}"'
            }
        }
    }

    post {
        success {
            publishChecks conclusion: 'SUCCESS', name: 'Build Successful', summary: 'The build was successful', text: 'The build was sucessful', title: 'Build Successful'
            echo '✅mdBook Built!'
        }
        failure {
            publishChecks conclusion: 'FAILURE', name: 'Build Failed', summary: 'The build failed', text: 'The build was a failure', title: 'Build failed'
            echo '❌mdbook Failed to Build'
        }
        unstable{
            publishChecks conclusion: 'ACTION_REQUIRED', name: 'Build Unstable', summary: 'Unstable due to plugins missing', text: 'Unstable due to plugins missing', title: 'Build Unstable'
        }
    }
}