diff --git a/models/vagrant_builder.rb b/models/vagrant_builder.rb index 5de65ef..46c1f77 100644 --- a/models/vagrant_builder.rb +++ b/models/vagrant_builder.rb @@ -25,6 +25,7 @@ module Vagrant end unless @vagrant.nil? + build.env[:vagrant_dirty] = true code = @vagrant.primary_vm.channel.send(vagrant_method, @command) do |type, data| # type is one of [:stdout, :stderr, :exit_status] # # data is a string for stdout/stderr and an int for exit status @@ -93,6 +94,7 @@ module Vagrant end listener.info('Provisioning the Vagrant VM.. (this may take a while)') + build.env[:vagrant_dirty] = true @vagrant.cli('provision') end end diff --git a/models/vagrant_publisher.rb b/models/vagrant_publisher.rb new file mode 100644 index 0000000..830dd45 --- /dev/null +++ b/models/vagrant_publisher.rb @@ -0,0 +1,46 @@ +require 'rubygems' +require 'vagrant' + + +module Vagrant + class PackagePublisher < Jenkins::Tasks::Publisher + display_name "Package Vagrant box" + + attr_accessor :boxname + def initialize(attrs) + @vagrant = nil + @boxname = attrs['boxname'] + end + + def prebuild(build, listener) + # To run packaging, we need to own the destroy of the box + build.env[:vagrant_disable_destroy] = true + end + + def perform(build, launcher, listener) + @vagrant = build.env[:vagrant] + if @vagrant.nil? + built.halt "OH CRAP! I don't seem to have a Vagrant instance in the publisher" + end + + unless build.env[:vagrant_dirty] + listener.info("It doesn't appear any changes were made inside the Vagrant VM") + listener.info("I'm going to save us all a lot of grief and skip packaging it up then") + return + end + + name = @boxname + if name.nil? or name.empty? + name = 'package.box' + end + unless name.end_with? '.box' + name = "#{name}.box" + end + + listener.info("Preparing to export the current Vagrant box to a file named: #{name}") + output = File.expand_path(File.join(build.workspace.to_s, name)) + @vagrant.cli('package', '--output', output) + @vagrant.cli('destroy', '-f') + end + end +end diff --git a/models/vagrant_wrapper.rb b/models/vagrant_wrapper.rb index 22d0386..ac3bd3c 100644 --- a/models/vagrant_wrapper.rb +++ b/models/vagrant_wrapper.rb @@ -68,12 +68,20 @@ module Vagrant listener.info "Vagrant box is online, continuing with the build" build.env[:vagrant] = @vagrant + # We use this variable to determine if we have changes worth packaging, + # i.e. if we have actually done anything with the box, we will mark it + # dirty and can then take further action based on that + build.env[:vagrant_dirty] = false end # Called some time when the build is finished. def teardown(build, listener) - listener.info "Build finished, destroying the Vagrant box" - unless @vagrant.nil? + if @vagrant.nil? + return + end + + unless build.env[:vagrant_disable_destroy] + listener.info "Build finished, destroying the Vagrant box" @vagrant.cli('destroy', '-f') end end diff --git a/views/vagrant/package_publisher/config.erb b/views/vagrant/package_publisher/config.erb new file mode 100644 index 0000000..2ce960e --- /dev/null +++ b/views/vagrant/package_publisher/config.erb @@ -0,0 +1,10 @@ +<% +f = taglib("/lib/form") +f.block do + f.entry(:title => 'Name of .box file (optional)', + :field => 'boxname', + :description => 'Name to give the .box file packaged by Vagrant') do + f.textbox + end +end +%>