Skip to content

Commit 7030b72

Browse files
committed
docs: Add an example usage of withCredentials and mocking Jenkins credentials
1 parent 36d40d0 commit 7030b72

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,40 @@ Please refer to the `BasePipelineTest` class for the list of currently supported
217217
Some tricky methods such as `load` and `parallel` are implemented directly in the helper.
218218
If you want to override those, make sure that you extend the `PipelineTestHelper` class.
219219

220+
### Mocking Jenkins Credentials
221+
222+
You can create mock credentials for use in the pipeline.
223+
The provided mock methods can be used to simulate sensitive data, such as access keys or user login details.
224+
225+
```groovy
226+
// Jenkinsfile
227+
node {
228+
stage('Process with credentials') {
229+
withCredentials([usernamePassword(credentialsId: 'credentials-1', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
230+
echo 'User/Password: ${USERNAME}/${PASSWORD}'
231+
}
232+
withCredentials([string(credentialsId: 'credentials-2', variable: 'TOKEN')]) {
233+
echo 'Token: ${TOKEN}'
234+
}
235+
}
236+
}
237+
```
238+
239+
```groovy
240+
import com.lesfurets.jenkins.unit.BasePipelineTest
241+
242+
class TestExampleJob extends BasePipelineTest {
243+
@Test
244+
void exampleWithCredentialsTest() {
245+
// Simulates a username and password
246+
addUsernamePasswordCredential('credentials-1', 'admin', 'very-strong-password')
247+
248+
// Simulates a secret text
249+
addStringCredential('credentials-2', 'secret-token')
250+
}
251+
}
252+
```
253+
220254
### Mocking `readFile` and `fileExists`
221255

222256
The `readFile` and `fileExists` steps can be mocked to return a specific result for a

0 commit comments

Comments
 (0)