forked from electric-cloud/DSL-Samples
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUseCredentialsFromDifferentProject.groovy
47 lines (39 loc) · 1.61 KB
/
UseCredentialsFromDifferentProject.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
CloudBees CD (Flow) DSL: Use of credentials from a different project
In this example, credentials are stored in one project and used in a pipeline task in another project
*/
def CredentialName = "MyCreds"
def CredentialProject = "Credential Project"
project CredentialProject,{
credential CredentialName, userName: "MyUser", password: "My password"
}
project "Use Credential Project",{
procedure 'Command line credentials', {
step 'Use credentials in a shell', {
description = 'ec-groovy wrapped command line that uses credential values'
shell = 'ec-groovy'
command = """\
import com.electriccloud.client.groovy.ElectricFlow
ElectricFlow ef = new ElectricFlow()
def userName=ef.getFullCredential(credentialName: "/projects/${CredentialProject}/credentials/$CredentialName").credential.userName
def password=ef.getFullCredential(credentialName: "/projects/${CredentialProject}/credentials/$CredentialName").credential.password
/*
This example uses 'echo' to purposefully show the credentials as proof that this mechanism works. When using this approach, make sure not to echo the secret part of the credential.
*/
println "echo Username: \$userName, Password: \$password".execute().text
""".stripIndent()
attachCredential {
credentialName = "/projects/${CredentialProject}/credentials/$CredentialName"
}
}
}
pipeline "Use Credentials from different project",{
stage "Stage 1",{
task 'Use credential', {
subprocedure = 'Command line credentials'
subproject = projectName
taskType = 'PROCEDURE'
}
}
}
}