GithubHelp home page GithubHelp logo

satishkamuju / jsl Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bcdevops/jenkins-pipeline-shared-lib

0.0 0.0 0.0 185 KB

Extending Jenkins with a Shared Library

License: Apache License 2.0

Groovy 100.00%

jsl's Introduction

License

jenkins-pipeline-shared-lib

Extending Jenkins with a Shared Library

References:

Project Setup

Jenkins

Accessing shared libaries from within your Jenkinsfile.

Two Optios:

1.)

You can add the shared library into the configuration for Jenkins.
This can be found in Manage Jenkins->Configure System->Global Pipeline Libraries:

From there you will want to setup up the library to use git and point it at this repository:

Global Pipeline Setup

Then use the library in your Jenkinsfile.

Example:

@Library('devops-library') _

stage('testing lib') {
    def TIMESTAMP = getTimeStamp();
    echo "${TIMESTAMP}"
}

2.)

Define the library location at the top of the Jenkisfile. This does not require any global Jenkins configurartion changes.

Example:

library identifier: 'devops-library@master', retriever: modernSCM(
  [$class: 'GitSCMSource',
   remote: 'https://github.com/BCDevOps/jenkins-pipeline-shared-lib.git'])

stage('testing lib') {
    def TIMESTAMP = getTimeStamp();
    echo "${TIMESTAMP}"
}

Jenkinsfile

Once you have your shared library setup, you can now use the utilities within your Jenkinsfile like so:

// @Library('devops-library') _

library identifier: 'devops-library@master', retriever: modernSCM(
  [$class: 'GitSCMSource',
   remote: 'https://github.com/BCDevOps/jenkins-pipeline-shared-lib.git'])

def hasRepoChanged = false;
node{
  def lastCommit = getLastCommit()  // <------------------- getLastCommit utility from vars/
  
  if(lastCommit != null){
    if(env.CHANGE_AUTHOR_DISPLAY_NAME == null){
      env.CHANGE_AUTHOR_DISPLAY_NAME = lastCommit.author.fullName
    }

    if(env.CHANGE_TITLE == null){
      env.CHANGE_TITLE = lastCommit.msg
    }
    hasRepoChanged = true;
  }else{
    hasRepoChanged = false;
  }
}

if(hasRepoChanged){
  stage('Build ' + APP_NAME) {
    node{
      try{
        echo "Building: " + ARTIFACT_BUILD
        openshiftBuild bldCfg: ARTIFACT_BUILD, showBuildLogs: 'true'
        
        echo "Assembling Runtime: " + RUNTIME_BUILD
        openshiftBuild bldCfg: RUNTIME_BUILD, showBuildLogs: 'true'
      }catch(error){
        slackNotify(            // <------------------- slackNotify utility from shared lib vars/ folder
          'Build Broken 🤕',
          "The latest ${APP_NAME} build seems to have broken\n'${error.message}'",
          'danger',
          env.SLACK_HOOK,
          SLACK_DEV_CHANNEL,
          [
            [
              type: "button",
              text: "View Build Logs",
              style:"danger",           
              url: "${currentBuild.absoluteUrl}/console"
            ]
          ])
        throw error
      }
    }
  }
}

Utilities Available

The utility functions that are available to be called like global helpers can be found in the vars/ folder. We currently have:

  • getChangeString
    • Generates a string representation of a changeset
  • getLastCommit
    • Get's the last commit from the changeset, returns null if the build is a repeat
  • hasDirectoryChanged
    • Will return true or false whether or not a particular directory has been modified in the current changeset. Useful if multiple builds are triggered from a single repo
  • slackNotify
    • Sends a notification to slack via Incoming Webhook
    • Note: The webhook URL should be placed into an environment variable and not in your repositories code base as it should be kept secret

Directory Structure:

(root)
+- src                     # Groovy source files
|   +- org
|       +- foo
|           +- Bar.groovy  # for org.foo.Bar class
+- vars
|   +- foo.groovy          # for global 'foo' variable / custom step
|   +- foo.txt             # help for 'foo' variable

The src directory looks like a standard Java source directory structure. This directory is added to the classpath when executing Pipelines.

The vars directory hosts scripts that define global variables accessible from Pipeline. The basename of each *.groovy file should be a Groovy (~ Java) identifier, conventionally camelCased. The matching *.txt, if present, can contain documentation, processed through the system’s configured markup formatter (so may really be HTML, Markdown, etc., though the txt extension is required).

The Groovy source files in these directories get the same “CPS transformation” as in Scripted Pipeline.

Pipeline Steps https://jenkins.io/doc/pipeline/steps/

jsl's People

Contributors

agehlers avatar cjam avatar cvarjao avatar esune 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.