Skip to content

Commit

Permalink
Update image build to use the APIs dockerignore function
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeQuilty committed Nov 17, 2020
1 parent 1f30888 commit f0f66fd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
- Debify now packages and publishes an RPM file, alongside a debian file.
[conjurinc/debify#49](https://github.com/conjurinc/debify/pull/49)

### Fixed
- Bug causing `all` files in the git repo to be added to the debian file.
[conjurinc/debify#50](https://github.com/conjurinc/debify/pull/50)

# 1.11.5

### Changed
Expand Down
33 changes: 22 additions & 11 deletions lib/conjur/debify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'gli'
require 'json'
require 'base64'
require 'tmpdir'

require 'conjur/debify/utils'

Expand Down Expand Up @@ -255,22 +256,32 @@ def copy_packages_from_container(container, package_name, dev_package_name)
fpm_image = Docker::Image.build_from_dir File.expand_path('fpm', File.dirname(__FILE__)), tag: "debify-fpm", &DebugMixin::DOCKER
DebugMixin.debug_write "Built base fpm image '#{fpm_image.id}'\n"
dir = File.expand_path(dir)

Dir.chdir dir do
version = cmd_options[:version] || detect_version
dockerfile_path = cmd_options[:dockerfile] || File.expand_path("debify/Dockerfile.fpm", pwd)
dockerfile = File.read(dockerfile_path)

output = StringIO.new
Gem::Package::TarWriter.new(output) do |tar|
git_files.each do |fname|
stat = File.stat(fname)
tar.add_file(fname, stat.mode) { |tar_file| tar_file.write(File.read(fname)) }
end
tar.add_file('Dockerfile', 0640) { |tar_file| tar_file.write dockerfile.gsub("@@image@@", fpm_image.id) }
# move git files and Dockerfile to temp dir to make deb from
# we do this to avoid adding "non-git" files
# that aren't mentioned in the dockerignore to the deb
temp_dir = Dir.mktmpdir
DebugMixin.debug_write "Copying git files to tmp dir '#{temp_dir}'\n"
git_files.each do |fname|
original_file = File.join(dir, fname)
destination_path = File.join(temp_dir, fname)
FileUtils.mkdir_p(File.dirname(destination_path))
FileUtils.cp(original_file, destination_path)
end
output.rewind
# rename specified dockerfile to 'Dockerfile' during copy, incase name is different
dockerfile_path = cmd_options[:dockerfile] || File.expand_path("debify/Dockerfile.fpm", pwd)
temp_dockerfile = File.join(temp_dir, "Dockerfile")

# change image variable in specified Dockerfile
dockerfile = File.read(dockerfile_path)
replace_image = dockerfile.gsub("@@image@@", fpm_image.id)
File.open(temp_dockerfile, "w") {|file| file.puts replace_image}

image = Docker::Image.build_from_tar output, &DebugMixin::DOCKER
# build image from project being debified dir
image = Docker::Image.build_from_dir temp_dir, &DebugMixin::DOCKER

DebugMixin.debug_write "Built fpm image '#{image.id}' for project #{project_name}\n"

Expand Down

0 comments on commit f0f66fd

Please # to comment.