forked from cloudbees/jenkins-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-controlled-folder.groovy
27 lines (23 loc) · 1.02 KB
/
create-controlled-folder.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
/*
* Basic script to set a controlled agent from an existing agent
* by setting properly the SecurityGrantsFolderProperty
*/
import hudson.model.*
import jenkins.model.*
import hudson.slaves.*
import com.cloudbees.hudson.plugins.folder.*
import com.cloudbees.jenkins.plugins.foldersplus.*
String nodeName = "${nodeName}"
String folderName = "${folderName}"
Slave node = Jenkins.instance.getNode(nodeName)
node.getNodeProperties().add(new SecurityTokensNodeProperty(false));
SecurityToken token = SecurityToken.newInstance();
node.getNodeProperties().get(SecurityTokensNodeProperty.class).addSecurityToken(token);
node.save()
SecurityGrant request = SecurityGrant.newInstance();
SecurityGrant grant = token.issue(request);
Folder folder= Jenkins.instance.createProject(Folder.class, folderName)
folder.getProperties().replace(new SecurityGrantsFolderProperty(Collections.<SecurityGrant>emptyList()));
folder.getProperties().get(SecurityGrantsFolderProperty.class).addSecurityGrant(grant);
folder.save()
return "Node has been set successfully."