GithubHelp home page GithubHelp logo

doytsujin / pipeline-multibranch-defaults-plugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jenkinsci/pipeline-multibranch-defaults-plugin

0.0 1.0 0.0 85 KB

Multibranch tasks with default pipeline

Home Page: https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Multibranch+Defaults+Plugin

License: MIT License

Java 97.53% HTML 2.47%

pipeline-multibranch-defaults-plugin's Introduction

About Pipeline Multibranch Defaults Plugin

Build Status

Table of Contents

Normally, Jenkins pipelines and Multibranch pipelines requires a developer to save a Jenkinsfile in their repository root on one or more branches. The Pipeline Multibranch Defaults Plugin allows a Jenkins administrator to specify a default Jenkinsfile so that developers do not need to have a Jenkinsfile in their repository.

This is useful because:

  • Jenkins Administrators can define a default Jenkinsfile in which all repositories can use. This makes it easy to centralize build configuration for all projects.
  • Jenkins Administrators can define a set of required steps before and after a developer Jenkinsfile is run, but still run the normal repository Jenkinsfile by calling it with the load step. For example,
    • Security analysis can be run as a required step before running a user Jenkinsfile.
    • An Audit step can be run as a required step after running a user Jenkinsfile which might do things like archive the build logs and update ticketing systems with a download link to the archived logs. This is great for organizations which have compliance and regulations to meet by governing bodies.

Warnings

Pipeline multibranch defaults plugin v1.1 and older created a new Job type named Multibranch Pipeline with defaults. This is considered a design flaw.

Pipeline multibranch defaults plugin v2.0 and later allow specifying a default Jenkinsfile directly with a normal Multibranch Pipeline. If you're using v2.0 or later of this plugin do not use the "Multibranch Pipeline with defaults" project type. It will be removed from later releases of this plugin.

Security implications

This plugin supports running a default Jenkinsfile with or without a pipeline sandbox. If the pipeline load step is used, then the default Jenkinsfile must be run with sandbox enabled. Otherwise, unauthorized developer code will be able to run within and modify the Jenkins server runtime. This basically means all developers with commit access can affect any part of the Jenkins infrastructure (malicious or not). This is essentially the same security implications as granting developers access to the script console.

How it works

Configuration files will be referenced from the global Jenkins script store provided by the config-file provider plugin. If a multibranch pipeline is configured to use a default Jenkinsfile, then the following happens:

  • Every branch discovered is assumed to have a Jenkinsfile because of the default Jenkinsfile setting.
  • Every pull request discovered is assumed to have a Jenkinsfile because of the default Jenkinsfile setting.
  • When discover tags is enabled, every tag is assumed to have a Jenkinsfile because of the default Jenkinsfile setting.

How to configure

This section explains how to configure a multibranch pipeline to use the default Jenkinsfile feature provided by this plugin.

Create a default Jenkinsfile

Before pipelines can be configured to use a default Jenkinsfile, one must configure a groovy script in the global Config File Management provided by the Configfile-Provider Plugin.

  1. Visit Manage Jenkins menu.

  2. Visit Manage files menu.

    screenshot of manage files

  3. Add a new config file of type Groovy file. During this step you can give your config file the Script ID Jenkinsfile or whatever ID you would like. Leaving it to be a UUID is fine as well.

  4. Fill in the settings for the config file.

    screenshot of adding a config file

Note: versions prior to the pipeline multibranch defaults plugin v2.0 required the Script ID to be "Jenkinsfile". This is no longer required.

Create a Multibranch Pipeline job

Create a new multibranch pipeline job as one normally would (or update an existing multibranch pipeline job). In the menu on the left, choose New Item and select multibranch pipeline.

multibranch pipeline screenshot

Update the build configuration mode

Under the Build Configuration section there will be a Mode setting. Change the Mode to "by default Jenkinsfile".

screenshot of defaults setting

Configurable options include:

  • Script ID - The ID of the default Jenkinsfile to use from the global Config File Management.
  • Run default Jenkinsfile within Groovy sandbox - If enabled, the configured default Jenkinsfile will be run within a Groovy sandbox. This option is strongly recommended if the Jenkinsfile is using the load pipeline step to evaluate a groovy source file from an SCM repository.

Note: if disabling the groovy sandbox, then admin script approval may be required the first time the default Jenkinsfile is used.

Example default Jenkinsfile

Default scripted pipeline

A default scripted pipeline Jenkinsfile which does not allow user a repository Jenkinsfile to run.

node {
    stage("Hello world") {
        echo "Hello world"
    }
}

A default scripted pipeline Jenkinsfile which loads a repository Jenkinsfile. Use case is adding required security scans or additional auditing steps.

node('security-scanner') {
    stage('Pre-flight security scan') {
        checkout scm
        sh '/usr/local/bin/security-scan.sh'
    }
}
try {
    node {
        stage('Prepare Jenkinsfile environment') {
            checkout scm
        }
        // now that SCM is checked out we can load and execute the repository
        // Jenksinfile
        load 'Jenkinsfile'
    }
} catch(Exception e) {
    throw e
} finally {
    stage('Post-run auditing') {
        // do some audit stuff here
    }
}

Default declarative pipeline

Using declarative pipeline as your default Jenkinsfile is also possible. Here's an example:

pipeline {
    agent none
    stages {
        stage('Pre-flight security scan') {
            agent 'security-scanner'
            steps {
                sh '/usr/local/bin/security-scan.sh'
            }
        }
        stage('Load user Jenkinsfile') {
            agent any
            steps {
                load 'Jenkinsfile'
            }
        }
    }
    post {
        always {
            stage('Post-run auditing') {
                // do some audit stuff here
            }
        }
    }
}

Example Job DSL configuration

multibranchPipelineJob('example') {
    // SCM source or additional configuration

    factory {
        pipelineBranchDefaultsProjectFactory {
            // The ID of the default Jenkinsfile to use from the global Config
            // File Management.
            scriptId 'Jenkinsfile'

            // If enabled, the configured default Jenkinsfile will be run within
            // a Groovy sandbox.
            useSandbox true
        }
    }
}

Additional recommended plugins

The following plugins are recommended as great companion plugins to the pipeline multibranch with defaults plugin.

  • Basic Branch Build Strategies Plugin provides some basic branch build strategies for use with Branch API based projects. This plugin is especially good at limiting tags built. This is good for projects adopting a tag-based workflow.
  • SCM Filter Branch PR Plugin provides wildcard and regex filters for multibranch pipelines which include matching the destination branch of PRs with the filters. This is necessary if one does not want all branches for a project to be matched by the default Jenkinsfile. This also allows one to filter tags as well.
  • Job DSL plugin allows Jobs and Views to be defined via DSLs. Multibranch pipelines can be automatically created with a default Jenkinsfile configured. This plugin provides configuration as code for jobs.

Release Notes

For release notes see CHANGELOG.

Authors

  • Saponenko Denis - Initial work - vaimr
  • Sam Gleske - plugin maintainer since Sep 29, 2018

See also the list of contributors who participated in this project.

License

The MIT License

pipeline-multibranch-defaults-plugin's People

Contributors

samrocketman avatar vaimr avatar daniel-beck-bot avatar jimklimov avatar oleg-nenashev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.