Skip to content

Commit

Permalink
Configuration to populate node environment variables (#24)
Browse files Browse the repository at this point in the history
* Add `slave_environments` (#23)

Add new option to be able to set environments variables on slave

fixes #8

* Add new option to be able to set environments variables on slave

* Add new option to be able to set environments variables on slave windows

* Fix template groovy script
  • Loading branch information
pavelpikta authored and tgadiev committed Oct 15, 2019
1 parent 4df9c1a commit 344b1ff
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ This role sets up a new jenkins slave node and adds it to the jenkins master.
Agent name of the slave node. Default value is `agent`.
- `slave_executors_num`
Number of executors of the slave node. Default value is `1`.
- `slave_environments`
Dictionary of env variables to be set on slave. Default value is `{}`
- `slave_mode_exclusive`
Set usage of this node. If true, node will only build jobs with matching label expressions. Default value is `false`.
- `master_url`
Expand Down
1 change: 1 addition & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ master_url: "http://{{ master_host }}:{{ master_port }}"
# general slave
slave_agent_name: agent
slave_executors_num: 1
slave_environments: {}
slave_mode_exclusive: false

# jenkins linux slave
Expand Down
3 changes: 3 additions & 0 deletions molecule/default/playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
slave_linux_user_group: slave
slave_linux_jenkins_password: password
slave_linux_jenkins_public_key: '{{ jenkins_slave_key }}'
slave_environments:
env1: example1
env2: example2
pre_tasks:
- set_fact:
master_host_ip: >-
Expand Down
12 changes: 11 additions & 1 deletion templates/add_linux_slave.groovy.j2
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import hudson.slaves.ComputerLauncher
import hudson.slaves.DumbSlave
import hudson.util.VariableResolver
import jenkins.model.Jenkins
import hudson.slaves.*
import hudson.slaves.EnvironmentVariablesNodeProperty.Entry


List<Entry> env = new ArrayList<Entry>();
{% for item in (slave_environments | dict2items) %}
env.add(new Entry("{{ item.key }}", "{{ item.value }}"))
{% endfor %}
EnvironmentVariablesNodeProperty envPro = new EnvironmentVariablesNodeProperty(env);

Jenkins jenkins = Jenkins.getInstance()

Expand All @@ -21,9 +30,10 @@ ComputerLauncher nodeLauncher = new SSHLauncher( nodeHostname, nodePort,
String nodeName = "{{ slave_agent_name }}"
String nodeRemoteFS = "{{ slave_linux_home }}"

Node node = new DumbSlave( nodeName, nodeRemoteFS, nodeLauncher )
Node node = new DumbSlave(nodeName, nodeRemoteFS, nodeLauncher)
node.setNumExecutors({{ slave_executors_num }})
node.setLabelString("{{ slave_linux_labels | join(' ') }}")
node.getNodeProperties().add(envPro)
{% if slave_mode_exclusive %}
node.setMode(Node.Mode.EXCLUSIVE)
{% else %}
Expand Down
13 changes: 10 additions & 3 deletions templates/add_windows_slave.groovy.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ import hudson.model.*
import hudson.slaves.*
import hudson.slaves.EnvironmentVariablesNodeProperty.Entry

List<Entry> env = new ArrayList<Entry>();
{% for item in (slave_environments | dict2items) %}
env.add(new Entry("{{ item.key }}", "{{ item.value }}"))
{% endfor %}
EnvironmentVariablesNodeProperty envPro = new EnvironmentVariablesNodeProperty(env);

ComputerLauncher launcher = new JNLPLauncher()
Slave slave = new DumbSlave( "{{ slave_agent_name }}",
"{{ slave_windows_workdir }}",
launcher )
Slave slave = new DumbSlave("{{ slave_agent_name }}",
"{{ slave_windows_workdir }}",
launcher)
slave.numExecutors = {{ slave_executors_num }}
slave.labelString = "{{ slave_windows_labels | join(" ") }}"
{% if slave_mode_exclusive %}
Expand All @@ -15,5 +21,6 @@ slave.mode = Node.Mode.EXCLUSIVE
slave.mode = Node.Mode.NORMAL
{% endif %}
slave.retentionStrategy = new RetentionStrategy.Always()
slave.getNodeProperties().add(envPro)
Jenkins.instance.addNode(slave)
print slave.getComputer().getJnlpMac()

0 comments on commit 344b1ff

Please sign in to comment.