-
Notifications
You must be signed in to change notification settings - Fork 487
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #821 from basecamp/retry-clone
Handle corrupt git clones
- Loading branch information
Showing
6 changed files
with
183 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require "uri" | ||
|
||
class Kamal::Cli::Build::Clone | ||
attr_reader :sshkit | ||
delegate :info, :error, :execute, :capture_with_info, to: :sshkit | ||
|
||
def initialize(sshkit) | ||
@sshkit = sshkit | ||
end | ||
|
||
def prepare | ||
begin | ||
clone_repo | ||
rescue SSHKit::Command::Failed => e | ||
if e.message =~ /already exists and is not an empty directory/ | ||
reset | ||
else | ||
raise Kamal::Cli::Build::BuildError, "Failed to clone repo: #{e.message}" | ||
end | ||
end | ||
|
||
validate! | ||
rescue Kamal::Cli::Build::BuildError => e | ||
error "Error preparing clone: #{e.message}, deleting and retrying..." | ||
|
||
FileUtils.rm_rf KAMAL.config.builder.clone_directory | ||
clone_repo | ||
validate! | ||
end | ||
|
||
private | ||
def clone_repo | ||
info "Cloning repo into build directory `#{KAMAL.config.builder.build_directory}`..." | ||
|
||
FileUtils.mkdir_p KAMAL.config.builder.clone_directory | ||
execute *KAMAL.builder.clone | ||
end | ||
|
||
def reset | ||
info "Resetting local clone as `#{KAMAL.config.builder.build_directory}` already exists..." | ||
|
||
KAMAL.builder.clone_reset_steps.each { |step| execute *step } | ||
rescue SSHKit::Command::Failed => e | ||
raise Kamal::Cli::Build::BuildError, "Failed to clone repo: #{e.message}" | ||
end | ||
|
||
def validate! | ||
status = capture_with_info(*KAMAL.builder.clone_status).strip | ||
|
||
unless status.empty? | ||
raise Kamal::Cli::Build::BuildError, "Clone in #{KAMAL.config.builder.build_directory} is dirty, #{status}" | ||
end | ||
|
||
revision = capture_with_info(*KAMAL.builder.clone_revision).strip | ||
if revision != Kamal::Git.revision | ||
raise Kamal::Cli::Build::BuildError, "Clone in #{KAMAL.config.builder.build_directory} is not on the correct revision, expected `#{Kamal::Git.revision}` but got `#{revision}`" | ||
end | ||
rescue SSHKit::Command::Failed => e | ||
raise Kamal::Cli::Build::BuildError, "Failed to validate clone: #{e.message}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module Kamal::Commands::Builder::Clone | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
delegate :clone_directory, :build_directory, to: :"config.builder" | ||
end | ||
|
||
def clone | ||
git :clone, Kamal::Git.root, path: clone_directory | ||
end | ||
|
||
def clone_reset_steps | ||
[ | ||
git(:remote, "set-url", :origin, Kamal::Git.root, path: build_directory), | ||
git(:fetch, :origin, path: build_directory), | ||
git(:reset, "--hard", Kamal::Git.revision, path: build_directory), | ||
git(:clean, "-fdx", path: build_directory) | ||
] | ||
end | ||
|
||
def clone_status | ||
git :status, "--porcelain", path: build_directory | ||
end | ||
|
||
def clone_revision | ||
git :"rev-parse", :HEAD, path: build_directory | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters