|
| 1 | +# Pipeline setup options |
| 2 | + |
| 3 | +- [Defaults](#user-content-defaults) |
| 4 | +- [Remote Server Configuration](#user-content-server) |
| 5 | +- [Authentication](#user-content-authentication) |
| 6 | +- [The Handle Object](#user-content-handle) |
| 7 | +- [Blocking vs. Non-Blocking](#user-content-blockingnonblocking) |
| 8 | + - [Blocking usage (recommended)](#user-content-blocking) |
| 9 | + - [Non-blocking usage](#user-content-nonblocking) |
| 10 | +- [Support of Folders on Remote Jenkins](#user-content-folders) |
| 11 | + |
| 12 | +The `triggerRemoteJob` pipeline step triggers a job on a remote Jenkins. This command is also available in the Jenkins Pipeline Syntax Generator: |
| 13 | + |
| 14 | +You can select a globally configured remote server and only specify a job name here. |
| 15 | +The full URL is calculated based on the remote server, the authentication is taken from the global configuration. |
| 16 | +However it is possible to override the Jenkins base URL (or set the full Job URL) and override credentials used for authentication. |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +You can also specify the full job URL and use only the authentication from the global configuration or specify the authentication per job. |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +<div id="defaults"/> |
| 26 | + |
| 27 | +## Defaults |
| 28 | +The simplest way to trigger a job is: |
| 29 | +``` |
| 30 | +def handle = triggerRemoteJob job: 'https://myjenkins:8080/job/JobWithoutParams' |
| 31 | +echo 'Remote Status: ' + handle.getBuildStatus().toString() |
| 32 | +``` |
| 33 | + |
| 34 | +If the job has parameters: |
| 35 | +``` |
| 36 | +def handle = triggerRemoteJob job: 'https://myjenkins:8080/job/JobWithParams', parameters: 'param1=abc\nparam2=xyz' |
| 37 | +``` |
| 38 | + |
| 39 | +If authentication is required: |
| 40 | +``` |
| 41 | +def handle = triggerRemoteJob job: 'https://myjenkins:8080/job/JobWithoutParams', auth: TokenAuth(apiToken: '<theApiToken>', userName: '<userName>') |
| 42 | +``` |
| 43 | + |
| 44 | + |
| 45 | +The pipeline will wait/block until the remote build finished. |
| 46 | + |
| 47 | + |
| 48 | +<div id="server"/> |
| 49 | + |
| 50 | +## Remote Server Configuration |
| 51 | + |
| 52 | +:information_source: You can configure jobs/pipelines also without any global configuration. |
| 53 | + |
| 54 | +The remote Jenkins server containing the target job(s) can be configured in different ways. |
| 55 | +- **Jenkins System Configuration**<br> |
| 56 | + Remote servers can be configured in the [Jenkins System Configuration](README_SystemConfiguration.md) and referenced in Pipelines by their name. The server configuration can also include authentication settings.<br> |
| 57 | + `triggerRemoteJob remoteJenkinsName: 'remoteJenkins' ...` |
| 58 | +- **Override Server URL**<br> |
| 59 | + On Pipeline level the URL can be set/overridden with parameter `remoteJenkinsUrl`.<br> |
| 60 | + `triggerRemoteJob remoteJenkinsUrl: 'https://myjenkins:8080' ...`<br> |
| 61 | + If combined with `remoteJenkinsName` only the URL of the globally configured server will be overridden, the other settings like authentication will be used from the global configuration.<br> |
| 62 | + `triggerRemoteJob remoteJenkinsName: 'remoteJenkins', remoteJenkinsUrl: 'https://myjenkins:8080' ...`<br> |
| 63 | +- **Full Job URL**<br> |
| 64 | + It is also possible to configure the full job URL instead of the job name only and the remote Jenkins server root URL.<br> |
| 65 | + `triggerRemoteJob job: 'https://myjenkins:8080/job/MyJob' ...`<br> |
| 66 | + |
| 67 | +:information_source: If the remote Jenkins uses folders please [read this](#user-content-folders). |
| 68 | + |
| 69 | +<div id="authentication"/> |
| 70 | + |
| 71 | +## Authentication |
| 72 | +Authentication can be configured globally in the system configuration or set/overridden for each pipeline via the `auth` parameter. |
| 73 | + |
| 74 | +The following authentication types are available: |
| 75 | +- **Token Authentication** The specified user id and Jenkins API token is used.<br> |
| 76 | + ```auth: TokenAuth(apiToken: '<theApiToken>', userName: '<userName>')``` |
| 77 | +- **Credentials Authentication** The specified Jenkins Credentials are used. This can be either user/password or user/API Token.<br> |
| 78 | + ```auth: CredentialsAuth(credentials: '<credentialId>')``` |
| 79 | +- **No Authentication** No Authorization header will be sent, independent of the global 'remote host' settings.<br> |
| 80 | + ```auth: NoneAuth()``` |
| 81 | + |
| 82 | +**Note:** *Jenkins API Tokens* are recommended since, if stolen, they allow access only to a specific Jenkins |
| 83 | +while user and password typically provide access to many systems. |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +<div id="handle"/> |
| 88 | + |
| 89 | +## The Handle Object |
| 90 | +The `Handle` object provides the following methods: |
| 91 | + |
| 92 | +- `String getJobName()` returns the remote job name |
| 93 | +- `URL getBuildUrl()` returns the remote build URL including the build number |
| 94 | +- `int getBuildNumber()` returns the remote build number |
| 95 | +- `BuildStatus getBuildStatus()` returns the current remote build status |
| 96 | +- `BuildStatus getBuildStatusBlocking()` waits for completion and returns the build result |
| 97 | +- `boolean isFinished()` true if the remote build finished |
| 98 | +- `boolean isQueued()` true if the job is queued but not yet running |
| 99 | +- `String toString()` |
| 100 | +- `Object readJsonFileFromBuildArchive(String filename)`<br> |
| 101 | + This is a convenience method to download and parse the specified JSON file (filename or relative path) from the build archive. |
| 102 | + This mechanism might be used by remote builds to provide return parameters. |
| 103 | + |
| 104 | +``` |
| 105 | +def handle = triggerRemoteJob blockBuildUntilComplete: true, ... |
| 106 | +def results = handle.readJsonFileFromBuildArchive('build-results.json') |
| 107 | +echo results.urlToTestResults //just an example |
| 108 | +``` |
| 109 | + |
| 110 | +The `BuildStatus` enum provides the following types and methods: |
| 111 | + |
| 112 | +- Custom statuses: `UNKNOWN`, `NOT_STARTED`, `QUEUED`, `RUNNING`, if the remote job did not finish yet. |
| 113 | +- Jenkins Result statuses: `ABORTED`, `FAILURE`, `NOT_BUILT`, `SUCCESS`, `UNSTABLE`, if the remote job finished the status reflects the Jenkins build `Result`. |
| 114 | +- `boolean isJenkinsResult()`, true if the `BuildStatus` reflects a Jenkins `Result`. |
| 115 | +- `Result getJenkinsResult()`, the Jenkins `Result` if the status reflects one, null otherwise. |
| 116 | +- `String toString()` |
| 117 | + |
| 118 | + |
| 119 | +<div id="blockingnonblocking"/> |
| 120 | + |
| 121 | +## Blocking vs. Non-Blocking |
| 122 | +The `triggerRemoteJob` command always returns a [`Handle`](#user-content-the-handle-object) object. This object can be used to track the status of the remote build (instead of using the environment variables like in the Job case). |
| 123 | + |
| 124 | +There are two ways to use the command, in a blocking way (it will wait/block until the remote job finished) and in a non-blocking way (the handle is returned immediately and the remote status can be checked asynchronously). |
| 125 | + |
| 126 | +<div id="blocking"/> |
| 127 | + |
| 128 | +### Blocking usage (recommended) |
| 129 | +The recommended way to trigger jobs is in a blocking way. Set `blockBuildUntilComplete: true` to let the plugin wait |
| 130 | +until the remote build finished: |
| 131 | +``` |
| 132 | +def handle = triggerRemoteJob( |
| 133 | + remoteJenkinsName: 'remoteJenkins', |
| 134 | + job: 'TheJob', |
| 135 | + parameters: 'a=b', |
| 136 | + blockBuildUntilComplete: true, |
| 137 | + ...) |
| 138 | +echo 'Remote Status: ' + handle.getBuildStatus().toString() |
| 139 | +``` |
| 140 | + |
| 141 | +<div id="nonblocking"/> |
| 142 | + |
| 143 | +### Non-blocking usage |
| 144 | +It is also possible to use it in a non-blocking way. Set `blockBuildUntilComplete: false` and the plugin directly |
| 145 | +returns the `handle` for further tracking the status: |
| 146 | +``` |
| 147 | +def handle = triggerRemoteJob( |
| 148 | + remoteJenkinsName: 'remoteJenkins', |
| 149 | + job: 'TheJob', |
| 150 | + parameters: 'a=b', |
| 151 | + blockBuildUntilComplete: false, |
| 152 | + ...) |
| 153 | +while( !handle.isFinished() ) { |
| 154 | + echo 'Current Status: ' + handle.getBuildStatus().toString(); |
| 155 | + sleep 5 |
| 156 | +} |
| 157 | +echo handle.getBuildStatus().toString(); |
| 158 | +``` |
| 159 | + |
| 160 | +Even with `blockBuildUntilComplete: false` it is possible to wait synchronously until the remote job finished: |
| 161 | +``` |
| 162 | +def handle = triggerRemoteJob blockBuildUntilComplete: false, ... |
| 163 | +def status = handle.getBuildStatusBlocking() |
| 164 | +``` |
| 165 | + |
| 166 | +:warning: Currently the plugin cannot log to the pipeline log directly if used in non-blocking mode. As workaround you can use `handle.lastLog()` after each command to get the log entries. |
| 167 | + |
| 168 | + |
| 169 | +<div id="folders"/> |
| 170 | + |
| 171 | +# Support of Folders on Remote Jenkins |
| 172 | + |
| 173 | +The Parameterized Remote Trigger plugin also supports the use of folders on the remote Jenkins server, for example if it uses the [`CloudBees Folders Plugin`](https://wiki.jenkins.io/display/JENKINS/CloudBees+Folders+Plugin) or the [`GitHub Branch Source Plugin`](https://plugins.jenkins.io/github-branch-source) (formerly [`GitHub Organization Folder Plugin`](https://wiki.jenkins.io/display/JENKINS/GitHub+Organization+Folder+Plugin)) |
| 174 | + |
| 175 | +Remote URLs with folders look like this |
| 176 | +``` |
| 177 | +https://server:8080/job/Folder1/job/Folder2/job/TheJob |
| 178 | +``` |
| 179 | + |
| 180 | +Without folders it would only be `https://server:8080/job/TheJob` |
| 181 | + |
| 182 | +To be able to trigger such jobs you have to either |
| 183 | +1. Specify the full Job URL as `Remote Job Name or URL` |
| 184 | +2. Specify the job fullname as `Remote Job Name or URL` + a globally configured [`Remote Host`](#user-content-server).<br> |
| 185 | + The jobs fullname in the example above would be 'Folder1/Folder2/TheJob'. |
| 186 | + |
| 187 | + |
| 188 | +<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> |
0 commit comments