HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How to terminate EC2 instance in Jenkins via Vagrant when job is stopped manually?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
vagrantterminatejenkinsstoppedec2jobinstancemanuallyviahow

Problem

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?

Solution

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:

Code Snippets

vagrant destroy -f

Context

StackExchange DevOps Q#87, answer score: 3

Revisions (0)

No revisions yet.