Skip to content

Commit

Permalink
Adding multibranch example with credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Sep 7, 2018
1 parent 194ee0c commit a3b0024
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# Generic Webhook Plugin Changelog
Changelog of Generic Webhook Plugin.
## Unreleased
### No issue

**Adding multibranch example with credentials**


[3048f14d3a1674a](https://github.com/jenkinsci/generic-webhook-trigger-plugin/commit/3048f14d3a1674a) Tomas Bjerre *2018-09-07 09:30:44*


## 1.44 (2018-09-06 16:48:37)
### GitHub [#77](https://github.com/jenkinsci/generic-webhook-trigger-plugin/issues/77) Auth/security: can the GWT end point be reached without authenticating? *question*
### GitHub [#77](https://github.com/jenkinsci/generic-webhook-trigger-plugin/issues/77) Auth/security: can the GWT end point be reached without authenticating? *enhancement* *question*

**Only impersonating if token supplied #77**

Expand Down
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ If you need the resolved values in pre build steps, like git clone, you need to

This plugin can be used with the Job DSL Plugin. There is also an example int he [Violation Comments to GitLab Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Violation+Comments+to+GitLab+Plugin) wiki page.

Job DSL supports injecting credenials when processing the DSL. You can use that if you want the `token` to be set from credentials.

```groovy
job('Generic Job Example') {
parameters {
Expand Down Expand Up @@ -159,6 +161,50 @@ echo $requestHeaderName

This plugin can be used with the [Pipeline Multibranch Plugin](https://jenkins.io/doc/pipeline/steps/workflow-multibranch/#properties-set-job-properties). Here is an example:

You can use the credentials plugin to provide the `token` from credentials.
```groovy
withCredentials([string(credentialsId: 'mycredentialsid', variable: 'credentialsVariable')]) {
properties([
pipelineTriggers([
[$class: 'GenericTrigger',
...
token: credentialsVariable,
...
]
])
])
}
```

Perhaps you want a different `token` for each job.

```groovy
properties([
pipelineTriggers([
[$class: 'GenericTrigger',
...
token: env.JOB_NAME,
...
]
])
])
```

Or have a credentials string prefixed with the job name.
```groovy
withCredentials([string(credentialsId: 'mycredentialsid', variable: 'credentialsVariable')]) {
properties([
pipelineTriggers([
[$class: 'GenericTrigger',
...
token: env.JOB_NAME + credentialsVariable,
...
]
])
])
}
```

With a scripted Jenkinsfile like this:

```groovy
Expand Down
52 changes: 52 additions & 0 deletions sandbox/multibranch-pipeline-credentials.jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* curl -X POST -H "Content-Type: application/json" -H "headerWithNumber: nbr123" -H "headerWithString: a b c" -d '{ "before": "1848f12", "after": "5cab1", "ref": "refs/heads/develop" }' -vs http://admin:admin@localhost:8080/jenkins/generic-webhook-trigger/invoke?requestWithNumber=nbr%20123\&requestWithString=a%20string
*/
node {

withCredentials([string(credentialsId: 'mycredentialsid', variable: 'credentialsVariable')]) {
properties([
pipelineTriggers([
[$class: 'GenericTrigger',
genericVariables: [
[key: 'reference', value: '$.ref'],
[
key: 'before',
value: '$.before',
expressionType: 'JSONPath', //Optional, defaults to JSONPath
regexpFilter: '', //Optional, defaults to empty string
defaultValue: '' //Optional, defaults to empty string
]
],
genericRequestVariables: [
[key: 'requestWithNumber', regexpFilter: '[^0-9]'],
[key: 'requestWithString', regexpFilter: '']
],
genericHeaderVariables: [
[key: 'headerWithNumber', regexpFilter: '[^0-9]'],
[key: 'headerWithString', regexpFilter: ''],
[key: 'X-GitHub-Event', regexpFilter: '']
],
token: credentialsVariable,
printContributedVariables: true,
printPostContent: true,
regexpFilterText: '',
regexpFilterExpression: ''
]
])
])
}


stage("build") {
sh '''
echo Variables from shell:
echo reference $reference
echo before $before
echo requestWithNumber $requestWithNumber
echo requestWithString $requestWithString
echo headerWithNumber $headerWithNumber
echo headerWithString $headerWithString
echo X_GitHub_Event $X_GitHub_Event
'''
}
}

0 comments on commit a3b0024

Please sign in to comment.