I built this when we needed Jenkins to stop a rolling set of servers before deploying — loop through each hostname, SSH in, run the stop command. The profile-based hostname list means you can have different server sets for dev, staging, and prod without changing the script. Source is on GitHub if you want the full working example.
- Script to disable logging
1
2
3
4
| <script language="javascript">
var logger = project.getBuildListeners( ).firstElement( );
logger.setMessageOutputLevel( 0 );
</script>
|
1
2
3
4
5
6
7
| <configuration>
<target>
<ant antfile="${basedir}/build.xml">
<target name="deploy" />
</ant>
</target>
</configuration>
|
- Setup profile and add servers for that environment
1
2
3
4
5
6
7
8
9
| <profile>
<id>dev</id>
<properties>
<classifier.type>DEV</classifier.type>
<server1.hostname>server1</server1.hostname>
<server2.hostname>server2</server2.hostname>
<server3.hostname>server3</server3.hostname>
</properties>
</profile>
|
1
2
| <for list="${sorted.hostnames}" param="hostname">
</for>
|
- Ant ssh and execute commands on remote
1
2
3
4
5
6
7
| <sshexec
host="${node}"
username="server"
password="${server.password}"
command=". .bash_profile;
server stop;"
trust="true" />
|