<p>This can be achieved by using <a href="https://wiki.jenkins-ci.org/display/JENKINS/Post+build+task" rel="nofollow noreferrer">Post build task</a> plugin and by adding the following <em>Script</em> command in <em>Post-build Action</em>, <em>Post build task</em> solved the problem:</p>

<pre><code>vagrant destroy -f
</code></pre>

<p>Plus adding rule with <em>Log text</em> as "Build was aborted" make sure that above command is only invoked on aborted builds.</p>

<p>For example:</p>

<p><a href="https://i.sstatic.net/40brUl.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/40brUl.png" alt="Jenkins, screenshot Script, Post-build Action, Log text, Build was aborted"></a></p>

CC BY-SA 3.0
2017-02-28T18:37:25.087Z
2017-03-01T12:09:16.020Z
2017-03-01T12:09:16.020Z
kenorb
<p>I've freestyle jobs which aims to run EC2 instances via Vagrant (<code>vagrant up</code>), however when I cancel the job manually (by clicking X button), the instances on EC2 are still running. How can I make sure that the instances are terminated when the job is cancelled?</p>

CC BY-SA 3.0
2017-03-01T12:06:28.303Z
Jenkins, Vagrant ... so much assumptions that the reader will know your architecture. This needs more explanation of what are you trying to achieve, what is the problem you are experiencing, etc...
Evgeny Zislis
<jenkins><amazon-ec2><jenkins-plugins><vagrant>
How to terminate EC2 instance in Jenkins via Vagrant when job is stopped manually?
kenorb
# How to terminate EC2 instance in Jenkins via Vagrant when job is stopped manually?

(Asked by: username_0 on 2017-02-28)

I've freestyle jobs which aims to run EC2 instances via Vagrant (`vagrant up`), however when I cancel the job manually (by clicking X button), the instances on EC2 are still running. How can I make sure that the instances are terminated when the job is cancelled?

**Tags:** jenkins, amazon-ec2, jenkins-plugins, vagrant

> **Comments:**
> **username_1**: Jenkins, Vagrant ... so much assumptions that the reader will know your architecture. This needs more explanation of what are you trying to achieve, what is the problem you are experiencing, etc...  

---

## 1 answers

### Answer by username_0 on 2017-02-28. Score: 3 | Accepted answer

This can be achieved by using Post build task plugin and by adding the following Script command in Post-build Action, Post build task solved the problem:
vagrant destroy -f

Plus adding rule with Log text as "Build was aborted" make sure that above command is only invoked on aborted builds.
For example:


---

