diff options
Diffstat (limited to 'app/jobs/govuk_guix/fetch_revision_job.rb')
-rw-r--r-- | app/jobs/govuk_guix/fetch_revision_job.rb | 61 |
1 files changed, 49 insertions, 12 deletions
diff --git a/app/jobs/govuk_guix/fetch_revision_job.rb b/app/jobs/govuk_guix/fetch_revision_job.rb index 2787156..d3f11bf 100644 --- a/app/jobs/govuk_guix/fetch_revision_job.rb +++ b/app/jobs/govuk_guix/fetch_revision_job.rb @@ -24,26 +24,22 @@ class GovukGuix::FetchRevisionJob < Que::Job extend EnqueuedJobs include ::ShellUtils - REPOSITORY_DIRECTORY = 'tmp/cache/govuk-guix'.freeze - @retry_interval = 30 - def run(commit_hash) - repository.fetch - - sha = repository.object(commit_hash).sha + def run(commit_hash, options = {}) + remote_host = options[:run_remotely_on_host] - repository.checkout(sha) + fetch_and_checkout(commit_hash, remote_host) command = [ - "#{REPOSITORY_DIRECTORY}/guix-pre-inst-env", + "#{@repository_directory}/guix-pre-inst-env", 'guix', 'build', '-e', '(begin (use-modules (gds packages govuk)) (current-govuk-guix))' ] - output = run_command(*command) + output = run_command(*command, run_remotely_on_host: remote_host) store_path = output.last.strip logger.debug(self.class) { "store_path: #{store_path}" } @@ -55,12 +51,53 @@ class GovukGuix::FetchRevisionJob < Que::Job ) end + def fetch_and_checkout(commit_hash, remote_host) + if remote_host + @repository_directory = '/tmp/govuk-guix' + + run_command( + "/bin/sh", + "-c", + "if cd '#{@repository_directory}';"\ + "then git fetch;"\ + "else "\ + "git clone #{self.class.repository_remote_location} '#{@repository_directory}';"\ + "fi", + run_remotely_on_host: remote_host + ) + + sha = run_command( + "/bin/sh", + "-c", + "cd '#{@repository_directory}';"\ + "git rev-list -n 1 '#{commit_hash}'", + run_remotely_on_host: remote_host + ).last.strip + + run_command( + "/bin/sh", + "-c", + "cd '#{@repository_directory}';"\ + "git checkout #{sha}", + run_remotely_on_host: remote_host + ) + else + @repository_directory = 'tmp/cache/govuk-guix' + + repository.fetch + + sha = repository.object(commit_hash).sha + + repository.checkout(sha) + end + end + def repository @_repository ||= begin - if File.exist? REPOSITORY_DIRECTORY - Git.open(REPOSITORY_DIRECTORY, log: Rails.logger) + if File.exist? @repository_directory + Git.open(@repository_directory, log: Rails.logger) else - Git.clone(self.class.repository_remote_location, REPOSITORY_DIRECTORY) + Git.clone(self.class.repository_remote_location, @repository_directory) end end end |