Sergey Otpuschennikov | aad1ae0 | 2020-09-11 19:33:51 +0400 | [diff] [blame] | 1 | #!groovy |
| 2 | import io.jenkins.plugins.casc.ConfigurationAsCode |
| 3 | |
| 4 | @NonCPS |
| 5 | Boolean testConfig(String cascPath) { |
| 6 | def casc = ConfigurationAsCode.get() |
| 7 | def form |
| 8 | Boolean result = true |
| 9 | try { |
| 10 | form = casc.doCheckNewSource(cascPath) |
| 11 | } catch (Exception e) { |
| 12 | result = false |
| 13 | message = e.toString() |
| 14 | } finally { |
| 15 | if (form) { |
| 16 | println form.kind |
| 17 | println form.message.split('<')[0] |
| 18 | if (form.kind.toString() != 'OK') { |
| 19 | result = false |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | return result |
| 24 | } |
| 25 | |
| 26 | @NonCPS |
| 27 | void applyConfig(String cascPath) { |
| 28 | ConfigurationAsCode.get().configure(cascPath) |
| 29 | } |
| 30 | |
| 31 | node('master') { |
| 32 | Boolean doApply = false |
| 33 | String cascPath = env.WORKSPACE |
| 34 | |
| 35 | if (env.GERRIT_EVENT_TYPE == 'ref-updated') { |
| 36 | doApply = true |
| 37 | cascPath = ConfigurationAsCode.get().getStandardConfig()[0] ?: "${env.HOME}/casc" |
| 38 | } |
| 39 | |
| 40 | stage('SCM checkout') { |
| 41 | String refSpec = env.GERRIT_REFSPEC ?: env.GERRIT_REFNAME |
| 42 | String gitUrl = "ssh://${env.GERRIT_HOST}:${env.GERRIT_PORT}/${env.GERRIT_PROJECT}" |
| 43 | echo "Checking out git repository from ${env.GERRIT_PROJECT} @ ${refSpec}" |
| 44 | |
| 45 | dir(cascPath) { |
| 46 | checkout([ |
| 47 | $class: 'GitSCM', |
| 48 | branches: [[ |
| 49 | name: 'FETCH_HEAD', |
| 50 | ]], |
| 51 | userRemoteConfigs: [[ |
| 52 | url: gitUrl, |
| 53 | refspec: refSpec, |
| 54 | credentialsId: env.GIT_CREDENTIALS_ID, |
| 55 | ]], |
| 56 | extensions: [[ |
| 57 | $class: 'WipeWorkspace', |
| 58 | ]], |
| 59 | ]) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | stage('Test configuration') { |
| 64 | if (! testConfig(cascPath) ) { |
| 65 | currentBuild.result = 'FAILURE' |
| 66 | throw new RuntimeException ('Fail') |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (doApply) { |
| 71 | stage('Apply configuration') { |
| 72 | applyConfig(cascPath) |
| 73 | } |
| 74 | } |
| 75 | } |