Git Product home page Git Product logo

jenkinsfile-cheatsheet's Introduction

jenkinsfile-cheatsheet

Just another cheat sheet of Jenkins pipeline script snippets

Environment Variables

Environment variables are exposed via the global variable env

    println env.WORKSPACE

see also Jenkins Handbook: Working with the Environment

A list of environment variables automatically set by Jenkins on each build can be found at Jenkins Set Environment Variables

Setting environment variables

Setting an environment variable within a Jenkins pipeline can be done with the withEnv step from the Basic Steps Plugin

    withEnv(["RANCHER_URL=${conf.rancherURL}"]) {
        println(env.RANCHER_URL)
    }

Reading a property file

A property file can be read using the readProperties step from the Pipeline Utility Steps Plugin

    def conf = readProperties file: "${env.WORKSPACE}@script/build.properties"

However if you need to access a properties file from within your scripts directory, where your Jenkinsfile is located, you need to use the absolute path to load it. But be aware, that it only works as in the example as long as you don't run build job in multiple times in parallel. See also Stackoverflow: How can I reference the Jenkinsfile directory, with Pipeline?

Checking out from a SCM system

Their are various ways to checkout source code from a SCM system. You can use the generic checkout step from the Pipeline SCM Step Plugin, specifying the specific SCM driver class, e.g.

    checkout scm: [$class: 'SubversionSCM',
                   locations: [[credentialsId: '6a4ef5df-461b-4a10-8bc3-f1e68b06bb5c',
                                depthOption: 'infinity',
                                ignoreExternalsOption: true,
                                clean: true,
                                remote: 'https://subversion.whatever.com/my-repo/']],
                   poll: true]

The Git plugin and the Subversion Plugin are providing specific steps as shorthand for the genereic checkout step, e.g.

    if (!fileExists('jenkinsfile-cheatsheet')) {
        sh 'mkdir jenkinsfile-cheatsheet'
    }
    dir('jenkinsfile-cheatsheet') {
        git url: '[email protected]:cwkrebs/jenkinsfile-cheatsheet.git', credentialsId: conf.gitCredentialsId
        ... 
    }

see also git step reference

    svn url: 'https://subversion.whatever.com/my-repo/'

see also svn step reference

Capture Shell Output & Status in a variable

Standard output from a shell step can be captured in variable, by setting the parameter returnStdout to true. If checked, standard output from the task is returned as the step value as a String, rather than being printed to the build log. (Standard error, if any, will still be printed to the log.) You will often want to call .trim() on the result to strip off a trailing newline.

	def out = sh script: command, returnStdout: true

Likewise the return status of the command executed can be captured by checking the returnStatus flag. Capturing the status will prevent the shell step to fail the build, when exiting with a non-zero status code.

	def status = sh script: command, returnStatus: true

Failing a build

A build can be explicitly forced to fail using the error step.

	error("Build failed: reason ....")

In order to mark the build as unstable rather than terminating it with an error, the currentBuild.result can be set to 'UNSTABLE'.

	currentBuild.result = 'UNSTABLE'

Using external Groovy class

References

  • Jenkins Handbook, Chapter Pipeline General description of a Jenkins pipeline, some basic but useful steps and common patterns, and Jenkinsfile examples.
  • Pipeline Steps Reference List of Pipeline-compatible steps provided by the various Jenkins plugins, offering information about the parameters for each step.

jenkinsfile-cheatsheet's People

Contributors

cwkrebs 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.