|
| 1 | +# Trigger Jenkins |
| 2 | + |
| 3 | +There are some important details to know when triggering Jenkins jobs with parameters and/or token. |
| 4 | + |
| 5 | +## Using _**token**_ |
| 6 | + Tokens need to be treated as constants so they should be surrounded with single quotes. See examples below. |
| 7 | + |
| 8 | +## _**buildWithParameters**_ endpoint |
| 9 | + * Jenkins discards the body specified in notificationMessageKeyValues |
| 10 | + * the parameters need to be send in the URL |
| 11 | + * do not specify more parameters than you have in your job. This is a Jenkins security feature to hinder that someone triggers jobs that overwrite job environment variables. Your job will not be triggered otherwise. |
| 12 | + |
| 13 | + Example below shows a subscription that triggers a parameterized Jenkins job having job token and a parameter object containing the aggregated object. |
| 14 | + Observe that we use buildWithParameters and empty notificationMessageKeyValues. |
| 15 | + |
| 16 | + { |
| 17 | + "subscriptionName" : "Subscription1", |
| 18 | + |
| 19 | + // the name of the user who created the subscription |
| 20 | + // defaults to an empty string if LDAP is disabled |
| 21 | + "ldapUserName" : "ABC", |
| 22 | + |
| 23 | + // instructs whether same subscription should be re-triggered for new additions |
| 24 | + // to the aggregated object. If false only first time the conditions are fulfilled |
| 25 | + // a notification will be triggered. No matter how many times the aggregated object |
| 26 | + // is updated. |
| 27 | + "repeat" : false, |
| 28 | + "notificationMessageKeyValuesAuth" : [], |
| 29 | + "created" : 1542117412833, |
| 30 | + |
| 31 | + // how to notify when a subscription is triggered |
| 32 | + "notificationType" : "REST_POST", |
| 33 | + "authenticationType" : "BASIC_AUTH", |
| 34 | + |
| 35 | + // the username and password to insert in headers of the POST request when sending |
| 36 | + // a notification via REST POST |
| 37 | + "userName" : "functionalUser", |
| 38 | + "password" : "functionalUserPassword", |
| 39 | + |
| 40 | + // which url to use for the HTTP POST request |
| 41 | + "notificationMeta" : "http://eiffel-jenkins1:8080/job/ei-artifact-triggered-job/buildWithParameters?token='TOKEN'&object=id", |
| 42 | + |
| 43 | + // headers for the HTTP request, can be 'application/x-www-form-urlencoded' or 'application/json' |
| 44 | + "restPostBodyMediaType" : "application/json", |
| 45 | + |
| 46 | + // the data to send with the HTTP POST request |
| 47 | + "notificationMessageKeyValues" : [ |
| 48 | + { |
| 49 | + } |
| 50 | + ], |
| 51 | + |
| 52 | + // An array of requirements. At least one requirement should be fulfilled to |
| 53 | + // trigger this subscription. |
| 54 | + "requirements" : [ |
| 55 | + { |
| 56 | + // Array of conditions. Here we use JMESPATH condition based on content in |
| 57 | + // aggregated object. All conditions needs to be fulfilled in order for |
| 58 | + // a requirement to be fulfilled. |
| 59 | + |
| 60 | + "conditions" : [ |
| 61 | + { |
| 62 | + "jmespath" : "gav.groupId=='com.othercompany.library'" |
| 63 | + } |
| 64 | + ] |
| 65 | + } |
| 66 | + |
| 67 | + ] |
| 68 | + } |
| 69 | + |
| 70 | +## _**build**_ endpoint |
| 71 | + * the parameters should be specified in notificationMessageKeyValues. Also here not more parameters than the job is configured with. Your job will not be triggered otherwise. |
| 72 | + * no job parameters in the URL |
| 73 | + |
| 74 | + The subscription below triggers the same parameterized Jenkins job but we now use build endpoint and we send the parameter in a json form using REST body. |
| 75 | + |
| 76 | + { |
| 77 | + "subscriptionName" : "Subscription1", |
| 78 | + |
| 79 | + // the name of the user who created the subscription |
| 80 | + // defaults to an empty string if LDAP is disabled |
| 81 | + "ldapUserName" : "ABC", |
| 82 | + |
| 83 | + // instructs whether same subscription should be re-triggered for new additions |
| 84 | + // to the aggregated object. If false only first time the conditions are fulfilled |
| 85 | + // a notification will be triggered. No matter how many times the aggregated object |
| 86 | + // is updated. |
| 87 | + "repeat" : false, |
| 88 | + "notificationMessageKeyValuesAuth" : [], |
| 89 | + "created" : 1542117412833, |
| 90 | + |
| 91 | + // how to notify when a subscription is triggered |
| 92 | + "notificationType" : "REST_POST", |
| 93 | + "authenticationType" : "BASIC_AUTH", |
| 94 | + |
| 95 | + // the username and password to insert in headers of the POST request when sending |
| 96 | + // a notification via REST POST |
| 97 | + "userName" : "functionalUser", |
| 98 | + "password" : "functionalUserPassword", |
| 99 | + |
| 100 | + // which url to use for the HTTP POST request |
| 101 | + "notificationMeta" : "http://eiffel-jenkins1:8080/job/ei-artifact-triggered-job/build?token='TOKEN'", |
| 102 | + |
| 103 | + // headers for the HTTP request, can be 'application/x-www-form-urlencoded' or 'application/json' |
| 104 | + "restPostBodyMediaType" : "application/json", |
| 105 | + |
| 106 | + // the data to send with the HTTP POST request |
| 107 | + "notificationMessageKeyValues" : [ |
| 108 | + { |
| 109 | + // form value will be run through JMESPATH engine to extract |
| 110 | + // content from aggregated object. |
| 111 | + |
| 112 | + "formkey" : "json", |
| 113 | + "formvalue" : "{parameter: [{ name: 'object', value : to_string(@) }]}" |
| 114 | + } |
| 115 | + ], |
| 116 | + |
| 117 | + // An array of requirements. At least one requirement should be fulfilled to |
| 118 | + // trigger this subscription. |
| 119 | + "requirements" : [ |
| 120 | + { |
| 121 | + // Array of conditions. Here we use JMESPATH condition based on content in |
| 122 | + // aggregated object. All conditions needs to be fulfilled in order for |
| 123 | + // a requirement to be fulfilled. |
| 124 | + |
| 125 | + "conditions" : [ |
| 126 | + { |
| 127 | + "jmespath" : "gav.groupId=='com.othercompany.library'" |
| 128 | + } |
| 129 | + ] |
| 130 | + } |
| 131 | + |
| 132 | + ] |
| 133 | + } |
0 commit comments