Skip to content

BCDevOps/jenkins-pipeline-shared-lib

Repository files navigation

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/

About

Extending Jenkins with a Shared Library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages