[
  {
    "path": "README.md",
    "content": "# jenkinsfiles\nExamples collected for Jenkins files from www\n\n\n"
  },
  {
    "path": "externalCall.groovy",
    "content": "// If there's a call method, you can just load the file, say, as \"foo\", and then invoke that call method with foo(...) \ndef call(String whoAreYou) {\n    echo \"Now we're being called more magically, ${whoAreYou}, thanks to the call(...) method.\"\n}\n"
  },
  {
    "path": "externalMethod.groovy",
    "content": "// Methods in this file will end up as object methods on the object that load returns.\ndef lookAtThis(String whoAreYou) {\n    echo \"Look at this, ${whoAreYou}! You loaded this from another file!\"\n}\n"
  },
  {
    "path": "flow.groovy",
    "content": "\n#!groovy \n// Orig Ref From :: https://documentation.cloudbees.com/docs/cookbook/_setting_up_a_basic_build_pipeline_with_jenkins.html\n\ndef devQAStaging() {\n    env.PATH=\"${tool 'Maven 3.x'}/bin:${env.PATH}\"\n    stage 'Dev'\n    sh 'mvn clean install package'\n    archive 'target/x.war'\n  try {\n        checkpoint('Archived war')\n    } catch (NoSuchMethodError _) {\n        echo 'Checkpoint feature available in Jenkins Enterprise by CloudBees.'\n    }\n    stage 'QA'\n    parallel(longerTests: {\n        runWithServer {url ->\n            sh \"mvn -f sometests/pom.xml test -Durl=${url} -Dduration=30\"\n        }\n    }, quickerTests: {\n        runWithServer {url ->\n            sh \"mvn -f sometests/pom.xml test -Durl=${url} -Dduration=20\"\n        }\n    })\n    stage name: 'Staging', concurrency: 1\n    deploy 'target/x.war', 'staging'\n}\ndef production() {\n    input message: \"Does http://localhost:8888/staging/ look good?\"\n    try {\n        checkpoint('Before production')\n    } catch (NoSuchMethodError _) {\n        echo 'Checkpoint feature available in Jenkins Enterprise by CloudBees.'\n    }\n    stage name: 'Production', concurrency: 1\n    node {\n        unarchive mapping: ['target/x.war' : 'x.war']\n        deploy 'target/x.war', 'production'\n        echo 'Deployed to http://localhost:8888/production/'\n    }\n}\ndef deploy(war, id) {\n    sh \"cp ${war} /tmp/webapps/${id}.war\"\n}\ndef undeploy(id) {\n    sh \"rm /tmp/webapps/${id}.war\"\n}\nreturn this;\n"
  },
  {
    "path": "gitcommit.groovy",
    "content": "// Orig Ref From :: https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/gitcommit/gitcommit.groovy\n\n// These should all be performed at the point where you've\n// checked out your sources on the slave. A 'git' executable\n// must be available.\n// Most typical, if you're not cloning into a sub directory\nsh('git rev-parse HEAD > GIT_COMMIT')\ngit_commit=readFile('GIT_COMMIT')\n// short SHA, possibly better for chat notifications, etc.\nshort_commit=git_commit.take(6)\n\n//create a GIT_COMMIT file in workspace and read back into a string in Pipeline\n// If you have your sources checked out in a 'src' subdir\nsh('cd src && git rev-parse HEAD > GIT_COMMIT')\ngit_commit=readFile('src/GIT_COMMIT')\n// short SHA, possibly better for chat notifications, etc.\nshort_commit=git_commit.take(6)\n"
  },
  {
    "path": "jenkinsfile-complex-promotion-model",
    "content": "// Orig Ref From :: https://documentation.cloudbees.com/docs/cookbook/_setting_up_a_basic_build_pipeline_with_jenkins.html\n// Look for flow.groovy in same path\ndef flow\nnode('master') {\n    git branch: 'master', changelog: false, poll: true, url: 'https://github.com/lavaliere/workflow-plugin-pipeline-demo.git'\n    flow = load 'flow.groovy'\n    flow.devQAStaging()\n}\nflow.production()\n"
  },
  {
    "path": "jenkinsfile-conditional-os",
    "content": "\n#!groovy\n\n// Orig Ref From :: https://github.com/loverde/jenkinsfile-test/blob/master/Jenkinsfile\n\n\n\nstage name: 'setup'\nnode {\n    if (isUnix()) {\n        echo \"unix mode\"\n\t    sh 'pwd'\n        sh \"ls -la\"\n    } else {\n        echo \"windows mode\"\n        bat \"pwd\"\n        bat \"dir\"\n    }\n/*\n    wrap([$class: 'Groovy']) {\n        def script = '''\n        println \"\\n\\nSystem Properties\"\n        System.properties.each { k,v -> println \"$k = $v\" }\n        println \"\\n\\nEnvironment\"\n        System.getenv().each { k,v -> println \"$k = $v\" }\n'''\n    }\n*/\n\n    // Testing\n    git url: 'https://github.com/loverde/jenkinsfile-test'\n}\n\nstage name: 'build'\nnode {\n    if (isUnix()) {\n         sh './gradlew clean build'\n    } else {\n        bat './gradlew.bat clean build'\n    }\n}\n\nstage name: 'postbuild'\nnode {\n    if (isUnix()) {\n        sh 'ls -la'\n    } else { \n        bat 'dir'\n    }\n}\n"
  },
  {
    "path": "jenkinsfile-credentials-binding",
    "content": "// Orig Ref From :: https://cloudbees.zendesk.com/hc/en-us/articles/204897020-Fetch-a-userid-and-password-from-a-Credential-object-in-a-Pipeline-job-\n\nnode('<MY_UNIX_SLAVE>') {\nwithCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: '<CREDENTIAL_ID>',\nusernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {\n\nsh 'echo uname=$USERNAME pwd=$PASSWORD'\n }\n}\n\nnode('<MY_WINDOWS_SLAVE>') {\n  withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: '<CREDENTIAL_ID>',\n                    usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {\nbat '''\n      echo %USERNAME%\n    '''\n  }\n}\n\nnode('<MY_SLAVE>') {\n  withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: '<CREDENTIAL_ID>',\n                    usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {\n\nprintln(env.USERNAME)\n  }\n}\n"
  },
  {
    "path": "jenkinsfile-import-groovy-scripts",
    "content": "// Orig Ref from :: https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/load-from-file/pipeline.groovy\nnode {\n    // Load the file 'externalMethod.groovy' from the current directory, into a variable called \"externalMethod\".\n    def externalMethod = load(\"externalMethod.groovy\")\n\n    // Call the method we defined in externalMethod.\n    externalMethod.lookAtThis(\"Steve\")\n\n    // Now load 'externalCall.groovy'.\n    def externalCall = load(\"externalCall.groovy\")\n\n    // We can just run it with \"externalCall(...)\" since it has a call method.\n    externalCall(\"Steve\")\n}\n"
  },
  {
    "path": "jenkinsfile-include-groovy-script",
    "content": "#!groovy\n\n// Orig Ref From :: https://github.com/jenkinsci/pipeline-examples/blob/master/global-library-examples/global-function/Jenkinsfile\n\n// Loads the standardBuild function/step from workflowLibs.git/vars/standardBuild.groovy\n// and invokes it.\nstandardBuild {\n    environment = 'golang:1.5.0'\n    mainScript = '''\ngo version\ngo build -v hello-world.go\n'''\n    postScript = '''\nls -l\n./hello-world\n'''\n}\n"
  },
  {
    "path": "jenkinsfile-multiple-os-parallel",
    "content": "stage \"unit test\"\n\nnode {\n   git \"git@github.com:michaelneale/oaks-trail-ride.git\"\n   sh \"echo unit test app\"\n}\n\nstage \"test on supported OSes\"\n\nparallel (\n  windows: { node {\n    sh \"echo building on windows now\"\n\n }},\n  mac: { node {\n    sh \"echo building on mac now\"\n }}\n"
  },
  {
    "path": "jenkinsfile-parallel-jobs",
    "content": "// Orig Ref From :: https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/jobs-in-parallel/jobs_in_parallel.groovy\n\n// in this array we'll place the jobs that we wish to run\ndef branches = [:]\n\n//running the job 4 times concurrently\n//the dummy parameter is for preventing mutation of the parameter before the execution of the closure.\n//we have to assign it outside the closure or it will run the job multiple times with the same parameter \"4\"\n//and jenkins will unite them into a single run of the job\n\nfor (int i = 0; i < 4; i++) {\n  branches[\"branch${i}\"] = {\n//Parameters:\n//param1 : an example string parameter for the triggered job.\n//dummy: a parameter used to prevent triggering the job with the same parameters value. this parameter has to accept a different value\n//each time the job is triggered.\n    build job: 'test_jobs', parameters: [[$class: 'StringParameterValue', name: 'param1', value:\n      'test_param'], [$class: 'StringParameterValue', name:'dummy', value: \"${i}\"]]\n    }\n}\nparallel branches\n"
  },
  {
    "path": "jenkinsfile-push-to-git-repo",
    "content": "// Orig Ref From :: https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/push-git-repo/pushGitRepo.Groovy\n\n\n// This is currently the best way to push a tag (or a branch, etc) from a\n// Pipeline job. It's not ideal - https://issues.jenkins-ci.org/browse/JENKINS-28335\n// is an open JIRA for getting the GitPublisher Jenkins functionality working\n// with Pipeline.\n\n// credentialsId here is the credentials you have set up in Jenkins for pushing\n// to that repository using username and password.\nwithCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'MyID', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {\n\n    sh(\"git tag -a some_tag -m 'Jenkins'\")\n    sh('git push https://${GIT_USERNAME}:${GIT_PASSWORD}@<REPO> --tags')\n}\n\n// There isn't as trivial a way to override the ssh key if you want to push that\n// way, but I hope to add that here once I find a reasonable approach.\n"
  },
  {
    "path": "jenkinsfile-set-env-vars",
    "content": "env.JAVA_HOME = tool 'jdk-1.8.0'\n// Advice: don't define M2_HOME in general. Maven will autodetect its root fine.\ndef mvnHome   = tool 'maven-3.2.1'\nenv.PATH=\"${env.JAVA_HOME}/bin:${mvnHome}/bin:${env.PATH}\"\n"
  },
  {
    "path": "jenkinsfile-stash-unstash",
    "content": "// First we'll generate a text file in a subdirectory on one node and stash it.\nstage \"first step on first node\"\n\n// Run on a node with the \"first-node\" label.\nnode('first-node') {\n    // Make the output directory.\n    sh \"mkdir -p output\"\n\n    // Write a text file there.\n    writeFile file: \"output/somefile\", text: \"Hey look, some text.\"\n\n    // Stash that directory and file.\n    // Note that the includes could be \"output/\", \"output/*\" as below, or even\n    // \"output/**/*\" - it all works out basically the same.\n    stash name: \"first-stash\", includes: \"output/*\"\n}\n\n// Next, we'll make a new directory on a second node, and unstash the original\n// into that new directory, rather than into the root of the biuld.\nstage \"second step on second node\"\n\n// Run on a node with the \"second-node\" label.\nnode('second-node') {\n    // Run the unstash from within that directory!\n    dir(\"first-stash\") {\n        unstash \"first-stash\"\n    }\n\n    // Look, no output directory under the root!\n    // pwd() outputs the current directory Pipeline is running in.\n    sh \"ls -la ${pwd()}\"\n\n    // And look, output directory is there under first-stash!\n    sh \"ls -la ${pwd()}/first-stash\"\n}\n"
  },
  {
    "path": "jenkinsfile-timestamperWrapper",
    "content": "// Orig Ref from :: https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/timestamper-wrapper/timestamperWrapper.groovy\n// This shows a simple build wrapper example, using the Timestamper plugin.\nnode {\n    // This is the current syntax for invoking a build wrapper, naming the class.\n    wrap([$class: 'TimestamperBuildWrapper']) {\n        // Just some echoes to show the timestamps.\n        stage \"First echo\"\n        echo \"Hey, look, I'm echoing with a timestamp!\"\n\n        // A sleep to make sure we actually get a real difference!\n        stage \"Sleeping\"\n        sleep 30\n\n        // And a final echo to show the time when we wrap up.\n        stage \"Second echo\"\n        echo \"Wonder what time it is now?\"\n    }\n}\n"
  },
  {
    "path": "jenkinsfile-types-of-build-cause",
    "content": "// Orig Ref From :: https://github.com/benwtr/jenkins_experiment/blob/master/Jenkinsfile\n\nproperties [\n  [\n    $class: 'ParametersDefinitionProperty', parameterDefinitions: [\n      [\n        $class: 'ChoiceParameterDefinition', choices: ['plan', 'apply'], description: 'When this job is invoked directly, action that TF will perform', name: 'tf_action'\n      ],\n      [\n        $class: 'GitParameterDefinition', branch: '', branchFilter: '.*', defaultValue: 'master', description: 'Branch to use for plan when this job is invoked directly. This is ignored and always \"master\" when tf_action is apply', name: 'git_branch', quickFilterEnabled: true, sortMode: 'ASCENDING_SMART', tagFilter: '*', type: 'PT_BRANCH'\n      ]\n    ]\n  ]\n]\n\n\n// https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/get-build-cause/getBuildCause.groovy\n\ndef causes = currentBuild.rawBuild.getCauses()\n\nprintln causes.dump()\nprintln causes\ncauses = ''\n\nPRCause   = currentBuild.rawBuild.getCause(org.jenkinsci.plugins.github.pullrequest.GitHubPRCause)\nSCMCause  = currentBuild.rawBuild.getCause(hudson.triggers.SCMTrigger$SCMTriggerCause)\nUserCause = currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause)\n\n\n\nif (PRCause) {\n\n  println PRCause.properties\n\n} else if (SCMCause) {\n\n  println SCMCause.properties\n\n} else if (UserCause) {\n\n  println UserCause.properties\n\n} else {\n  error 'This job cant be triggered however it was just triggered, sorry.'\n}\n\nPRCause   = null\nSCMCause  = null\nUserCause = null\n"
  },
  {
    "path": "slackNotify.groovy",
    "content": "// Orig Ref From :: https://github.com/jenkinsci/pipeline-examples/blob/master/pipeline-examples/slacknotify/slackNotify.groovy\nimport groovy.json.JsonOutput\n// Add whichever params you think you'd most want to have\n// replace the slackURL below with the hook url provided by\n// slack when you configure the webhook\ndef notifySlack(text, channel) {\n    def slackURL = 'https://hooks.slack.com/services/xxxxxxx/yyyyyyyy/zzzzzzzzzz'\n    def payload = JsonOutput.toJson([text      : text,\n                                     channel   : channel,\n                                     username  : \"jenkins\",\n                                     icon_emoji: \":jenkins:\"])\n    sh \"curl -X POST --data-urlencode \\'payload=${payload}\\' ${slackURL}\"\n}\n"
  },
  {
    "path": "standardBuild.groovy",
    "content": "// See https://github.com/jenkinsci/workflow-plugin/tree/master/cps-global-lib#defining-global-functions\n\n// The call(body) method in any file in workflowLibs.git/vars is exposed as a\n// method with the same name as the file.\ndef call(body) {\n    def config = [:]\n    body.resolveStrategy = Closure.DELEGATE_FIRST\n    body.delegate = config\n    body()\n    stage 'checkout'\n    node {\n        checkout scm\n        stage 'main'\n        docker.image(config.environment).inside {\n            sh config.mainScript\n        }\n        stage 'post'\n        sh config.postScript\n    }\n}\n"
  }
]