├── .gitignore ├── README.md ├── com └── github │ └── wololock │ └── groovy │ └── Dsl.groovy └── jenkinsfile.groovy /.gitignore: -------------------------------------------------------------------------------- 1 | out/ 2 | .idea/ 3 | *.iml 4 | *.ipr 5 | *.log -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Groovy DSL Quickstart 2 | 3 | [![Groovy DSL Quickstart](https://img.youtube.com/vi/i9pNYW1Pg9A/0.jpg)](https://www.youtube.com/watch?v=i9pNYW1Pg9A) 4 | 5 | ## Used Groovy version 6 | 7 | ``` 8 | $ groovy -version 9 | Groovy Version: 3.0.2 JVM: 1.8.0_232 Vendor: Oracle Corporation OS: Linux 10 | ``` 11 | 12 | ## Running example using dynamic compilation 13 | 14 | ``` 15 | $ groovy jenkinsfile.groovy 16 | 17 | ___ ___ _ _ _ 18 | / _ \_ __ ___ _____ ___ _ / _ (_)_ __ ___| (_)_ __ ___ 19 | / /_\/ '__/ _ \ / _ \ \ / / | | | / /_)/ | '_ \ / _ \ | | '_ \ / _ \ 20 | / /_\\| | | (_) | (_) \ V /| |_| | / ___/| | |_) | __/ | | | | | __/ 21 | \____/|_| \___/ \___/ \_/ \__, | \/ |_| .__/ \___|_|_|_| |_|\___| 22 | |___/ |_| 23 | 24 | Running pipeline using any available agent... 25 | ==> Running 'Build' stage... 26 | + ls -la 27 | razem 32 28 | drwxrwxr-x 5 wololock wololock 4096 04-07 18:20 . 29 | drwxrwxr-x. 45 wololock wololock 4096 04-04 12:47 .. 30 | drwxrwxr-x 3 wololock wololock 4096 04-04 12:48 com 31 | drwxrwxr-x 7 wololock wololock 4096 04-07 18:20 .git 32 | -rw-rw-r-- 1 wololock wololock 29 04-07 18:19 .gitignore 33 | drwxrwxr-x 2 wololock wololock 4096 04-07 18:19 .idea 34 | -rw-rw-r-- 1 wololock wololock 1016 04-04 13:23 jenkinsfile.groovy 35 | -rw-rw-r-- 1 wololock wololock 23 04-07 18:20 README.md 36 | 37 | + date +%Y-%m-%d 38 | 2020-04-07 39 | 40 | [ECHO] Groovy rocks! 41 | [ECHO] env.SOME_STRING = foobar 42 | ==> Running 'Test' stage... 43 | + mvn -version 44 | Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T20:33:14+02:00) 45 | Maven home: /home/wololock/.sdkman/candidates/maven/current 46 | Java version: 1.8.0_232, vendor: Oracle Corporation, runtime: /home/wololock/.sdkman/candidates/java/8.0.232-open/jre 47 | Default locale: pl_PL, platform encoding: UTF-8 48 | OS name: "linux", version: "5.5.10-100.fc30.x86_64", arch: "amd64", family: "unix" 49 | ``` 50 | 51 | ## Running example using static compilation 52 | 53 | ``` 54 | $ groovy --compile-static jenkinsfile.groovy 55 | 56 | ___ ___ _ _ _ 57 | / _ \_ __ ___ _____ ___ _ / _ (_)_ __ ___| (_)_ __ ___ 58 | / /_\/ '__/ _ \ / _ \ \ / / | | | / /_)/ | '_ \ / _ \ | | '_ \ / _ \ 59 | / /_\\| | | (_) | (_) \ V /| |_| | / ___/| | |_) | __/ | | | | | __/ 60 | \____/|_| \___/ \___/ \_/ \__, | \/ |_| .__/ \___|_|_|_| |_|\___| 61 | |___/ |_| 62 | 63 | Running pipeline using any available agent... 64 | ==> Running 'Build' stage... 65 | + ls -la 66 | razem 32 67 | drwxrwxr-x 5 wololock wololock 4096 04-07 18:20 . 68 | drwxrwxr-x. 45 wololock wololock 4096 04-04 12:47 .. 69 | drwxrwxr-x 3 wololock wololock 4096 04-04 12:48 com 70 | drwxrwxr-x 7 wololock wololock 4096 04-07 18:20 .git 71 | -rw-rw-r-- 1 wololock wololock 29 04-07 18:19 .gitignore 72 | drwxrwxr-x 2 wololock wololock 4096 04-07 18:19 .idea 73 | -rw-rw-r-- 1 wololock wololock 1016 04-04 13:23 jenkinsfile.groovy 74 | -rw-rw-r-- 1 wololock wololock 23 04-07 18:20 README.md 75 | 76 | + date +%Y-%m-%d 77 | 2020-04-07 78 | 79 | [ECHO] Groovy rocks! 80 | [ECHO] env.SOME_STRING = foobar 81 | ==> Running 'Test' stage... 82 | + mvn -version 83 | Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T20:33:14+02:00) 84 | Maven home: /home/wololock/.sdkman/candidates/maven/current 85 | Java version: 1.8.0_232, vendor: Oracle Corporation, runtime: /home/wololock/.sdkman/candidates/java/8.0.232-open/jre 86 | Default locale: pl_PL, platform encoding: UTF-8 87 | OS name: "linux", version: "5.5.10-100.fc30.x86_64", arch: "amd64", family: "unix" 88 | ``` -------------------------------------------------------------------------------- /com/github/wololock/groovy/Dsl.groovy: -------------------------------------------------------------------------------- 1 | package com.github.wololock.groovy 2 | 3 | import groovy.transform.NamedParam 4 | import groovy.transform.NamedParams 5 | import groovy.transform.stc.ClosureParams 6 | import groovy.transform.stc.SimpleType 7 | 8 | import java.util.concurrent.ConcurrentHashMap 9 | import java.util.concurrent.ConcurrentMap 10 | 11 | import static groovy.lang.Closure.DELEGATE_FIRST 12 | import static groovy.lang.Closure.DELEGATE_ONLY 13 | 14 | class Dsl { 15 | 16 | static void pipeline(@DelegatesTo(value = PipelineDsl, strategy = DELEGATE_ONLY) final Closure closure) { 17 | final PipelineDsl dsl = new PipelineDsl() 18 | 19 | closure.delegate = dsl 20 | closure.resolveStrategy = DELEGATE_ONLY 21 | closure.call() 22 | } 23 | 24 | } 25 | 26 | class PipelineDsl { 27 | final Placeholder any = Placeholder.ANY 28 | 29 | static final ConcurrentMap env = [:] as ConcurrentHashMap 30 | 31 | void agent(final Placeholder any) { 32 | println "Running pipeline using any available agent..." 33 | } 34 | 35 | void environment(@DelegatesTo(value = Map, strategy = DELEGATE_FIRST) final Closure closure) { 36 | env.with(closure) 37 | } 38 | 39 | void stages(@DelegatesTo(value = StagesDsl, strategy = DELEGATE_ONLY) final Closure closure) { 40 | final StagesDsl dsl = new StagesDsl() 41 | 42 | closure.delegate = dsl 43 | closure.resolveStrategy = DELEGATE_ONLY 44 | closure.call() 45 | 46 | dsl.stages.each { stage -> 47 | stage.run() 48 | } 49 | } 50 | 51 | enum Placeholder { 52 | ANY 53 | } 54 | } 55 | 56 | class StagesDsl { 57 | protected final List stages = [] 58 | 59 | void stage(final String name, @DelegatesTo(value = StageDsl, strategy = DELEGATE_ONLY) final Closure closure) { 60 | stages << new Stage(name, closure) 61 | } 62 | } 63 | 64 | class Stage { 65 | final String name 66 | final Closure closure 67 | 68 | Stage(String name, Closure closure) { 69 | this.name = name 70 | this.closure = closure 71 | } 72 | 73 | void run() { 74 | println "==> Running '${name}' stage..." 75 | 76 | final StageDsl dsl = new StageDsl() 77 | 78 | closure.delegate = dsl 79 | closure.resolveStrategy = DELEGATE_ONLY 80 | closure.call() 81 | } 82 | } 83 | 84 | class StageDsl { 85 | void steps( 86 | @DelegatesTo(value = Steps, strategy = DELEGATE_ONLY) 87 | @ClosureParams(value = SimpleType, options = ["java.util.Map"]) final Closure closure) { 88 | final Steps steps = new Steps() 89 | 90 | closure.delegate = steps 91 | closure.resolveStrategy = DELEGATE_ONLY 92 | closure.call(PipelineDsl.env) 93 | } 94 | } 95 | 96 | class Steps { 97 | void sh(final String script) { 98 | sh(script: script, returnStdout: false) 99 | } 100 | 101 | Object sh(@NamedParams([ 102 | @NamedParam(value = "script", type = String, required = true), 103 | @NamedParam(value = "returnStdout", type = Boolean) 104 | ]) final Map param) { 105 | 106 | final Process p = param.script.toString().execute() 107 | p.waitFor() 108 | 109 | println "+ ${param.script}" 110 | 111 | if (p.exitValue() == 0) { 112 | if (param.returnStdout) { 113 | return p.text 114 | } 115 | 116 | println p.text 117 | } else { 118 | println p.err.text 119 | } 120 | 121 | } 122 | 123 | void echo(final String message) { 124 | println "[ECHO] ${message}" 125 | } 126 | } 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | -------------------------------------------------------------------------------- /jenkinsfile.groovy: -------------------------------------------------------------------------------- 1 | import static com.github.wololock.groovy.Dsl.pipeline 2 | 3 | println ''' 4 | ___ ___ _ _ _ 5 | / _ \\_ __ ___ _____ ___ _ / _ (_)_ __ ___| (_)_ __ ___ 6 | / /_\\/ '__/ _ \\ / _ \\ \\ / / | | | / /_)/ | '_ \\ / _ \\ | | '_ \\ / _ \\ 7 | / /_\\\\| | | (_) | (_) \\ V /| |_| | / ___/| | |_) | __/ | | | | | __/ 8 | \\____/|_| \\___/ \\___/ \\_/ \\__, | \\/ |_| .__/ \\___|_|_|_| |_|\\___| 9 | |___/ |_| 10 | ''' 11 | 12 | pipeline { 13 | agent any 14 | 15 | environment { 16 | SOME_NUMBER = 123 17 | SOME_STRING = "foobar" 18 | } 19 | 20 | stages { 21 | stage("Build") { 22 | steps { env -> 23 | sh "ls -la" 24 | sh(script: 'date +%Y-%m-%d', returnStdout: false) 25 | echo "Groovy rocks!" 26 | echo "env.SOME_STRING = ${env.SOME_STRING}" 27 | } 28 | } 29 | stage("Test") { 30 | steps { 31 | sh "mvn -version" 32 | } 33 | } 34 | } 35 | } 36 | --------------------------------------------------------------------------------