Expanding Jenkins Pipeline Functionality Throug...

Charlotte Mays
May 28, 2019
55

Expanding Jenkins Pipeline Functionality Through Shared Libraries

Charlotte Mays

May 28, 2019

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

48 slides
  1. What is Jenkins? Jenkins is (text paraphrased from wikipedia): •

    an open source automation server • written in Java • to automate the non-human part of the software development process (continuous integration and continuous delivery)
  2. Jenkins Pipelines - In Brief From Jenkins documentation (https://jenkins.io/doc/book/pipeline/): Jenkins

    Pipeline (or simply "Pipeline" with a capital "P") is a suite of plugins which supports implementing and integrating continuous delivery pipelines into Jenkins @charlottecodes
  3. Why Jenkins Pipelines Code: • Checked into source control •

    Editable and iterable • Domain-Specific Language (DSL) ◦ Groovy-based @charlottecodes
  4. Why Jenkins Pipelines Durable: • Survive restarts of the Jenkins

    master (planned or unexpected) @charlottecodes
  5. Uses For Shared Libraries • Reduce repetition • Lower barrier

    to entry • Increase compliance with best practices @charlottecodes
  6. Uses For Shared Libraries • Reduce repetition • Lower barrier

    to entry • Increase compliance with best practices • Increase robustness of CI/CD @charlottecodes
  7. Basic Pipeline Example stage("Build") { steps { sh "mvn clean

    install" } } stage("Report") { steps { report() } } @charlottecodes
  8. Directory Structure vars • Script files exposed as 'variables' in

    Pipelines ◦ 'methods' might be more accurate • Help files also located here (with .txt extensions) @charlottecodes
  9. Directory Structure src • Added to classpath ◦ Available to

    methods in vars and within Pipelines • Standard Java source directory structure @charlottecodes
  10. Directory Structure resources • Allows libraryResource step to load non-Groovy

    files • Useful for templates, default config, etc • Standard Java source directory structure @charlottecodes
  11. The Jenkins Environment def call(ArrayList recipients) { messageBody = """

    Job Name: ${env.JOB_NAME} Job Build #: ${env.BUILD_NUMBER} Job URL: ${env.BUILD_URL} """ // send message with messageBody } @charlottecodes
  12. What About Files? def call(ArrayList recipients) { // define messageBody

    // Append log to message messageBody += readFile( file: "${WORKSPACE}/log.txt" ) // send message with messageBody } @charlottecodes
  13. Jenkins Steps Available Any step valid in Jenkins Pipeline DSL

    is valid in your library code Examples: • sh • git • Notification (slackSend, hipchatSend) • Lots of Jira hooks • And more….. @charlottecodes
  14. Namespaced Calls def call(ArrayList recipients=[]) { // send message }

    def slack() { slackSend( message: "Info available at ${env.JOB_URL}" ) } @charlottecodes
  15. Usage in Pipeline stage("Report") { steps { report() // Uses

    the `call` function report.slack() // Uses the `slack` function } } @charlottecodes
  16. Closures withCredentials( [string(credentialsId: 'mytoken', variable: 'TOKEN')] ) { sh '''

    set +x curl -H "Token: $TOKEN" https://some.api/ ''' } @charlottecodes
  17. Closures def call(Map config=[:], Closure body){ // Any needed setup

    body() // Any needed teardown } @charlottecodes vars/withSomeAction.groovy
  18. Error Handling • Option 1: allow error to propagate as

    normal • Option 2: catch error and process ◦ Pass to Jenkins using Jenkins error step ◦ Handle internally @charlottecodes
  19. Error Handling def call(Map config=[:], Closure body){ // Any needed

    setup try { body() } catch(Exception e) { error e.toString() } // Any needed teardown } @charlottecodes
  20. Testable • Pipeline as code means we can unit test!

    • But no Jenkins running during tests… ◦ Lots of mocking/stubbing! @charlottecodes
  21. Testable • Pipeline as code means we can unit test!

    • But no Jenkins running during tests… ◦ Lots of mocking/stubbing! ◦ jenkins-spock by homeaway helps! @charlottecodes
  22. Adding Your Shared Library Name • Required • Arbitrary Default

    Version • Optional • Interpreted by SCM @charlottecodes
  23. Adding Your Shared Library Load Implicitly • Makes shared library

    available without using @Library syntax inside the Pipeline Retrieval Method • Source code to be pulled in • Location, creds, etc @charlottecodes
  24. Ready to Use! Jenkins jobs running on that master will

    now have access to shared library functions @charlottecodes
  25. Reference @charlottecodes on Twitter Slides: - https://bit.ly/30rPplQ Example Repo: -

    https://github.com/openshift/contra-hdsl Jenkins Documentation: - https://jenkins.io/doc/book/pipeline/ - https://jenkins.io/doc/book/pipeline/shared-libraries/ Charlotte Mays Red Hat Gr8Conf EU 2019