Skip to content

Commit

Permalink
Merge pull request #2988 from zendesk/grosser/full
Browse files Browse the repository at this point in the history
allow falling back to full checkout for stages that do weird stuff
  • Loading branch information
grosser authored Oct 10, 2018
2 parents 2b60578 + 02004ba commit 21c0065
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 17 deletions.
1 change: 1 addition & 0 deletions app/controllers/stages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def stage_permitted_params
:production,
:run_in_parallel,
:static_emails_on_automated_deploy_failure,
:full_checkout,
{
deploy_group_ids: [],
command_ids: []
Expand Down
17 changes: 13 additions & 4 deletions app/models/git_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class GitRepository
extend ::Samson::PerformanceTracer::Tracers

attr_accessor :executor # others set this to listen in on commands being executed
attr_accessor :full_checkout

# The directory in which repositories should be cached.
# TODO: find out and comment why this needs to be settable or make read-only self. method
Expand Down Expand Up @@ -143,10 +144,18 @@ def sha_exist?(sha)
end

def checkout(git_reference, work_dir)
executor.execute(
"cd #{repo_cache_dir}",
"git worktree add #{work_dir.shellescape} #{git_reference.shellescape} --force"
)
if full_checkout
executor.execute(
"git clone #{repo_cache_dir} #{work_dir.shellescape}",
"cd #{work_dir.shellescape}",
"git checkout --quiet #{git_reference.shellescape}"
)
else
executor.execute(
"cd #{repo_cache_dir}",
"git worktree add #{work_dir.shellescape} #{git_reference.shellescape} --force"
)
end
end

def checkout_submodules(pwd)
Expand Down
1 change: 1 addition & 0 deletions app/models/job_execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def initialize(reference, job, env: {}, output: OutputBuffer.new, &block)

@repository = @job.project.repository
@repository.executor = @executor
@repository.full_checkout = true if stage&.full_checkout

on_finish do
Rails.logger.info("Calling finish callback for Job Execution #{id}")
Expand Down
2 changes: 2 additions & 0 deletions app/views/stages/_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

<%= form.input :no_reference_selection, as: :check_box, label: "Disable reference selection", help: no_ref_label %>

<%= form.input :full_checkout, as: :check_box, label: "Use a full checkout", help: "Samson usually uses a worktree which is a lot faster, pick this option if you want to switch branches during deploy." %>

<% if interval = Samson::Periodical.interval(:periodical_deploy) %>
<%= form.input :periodical_deploy, as: :check_box, help: "Deploy every #{distance_of_time_in_words(interval)} if last deploy succeeded, enable automated deploy failure email to be alerted. " %>
<% end %>
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20181009022203_add_full_checkout_to_stages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true
class AddFullCheckoutToStages < ActiveRecord::Migration[5.2]
def change
add_column :stages, :full_checkout, :boolean, default: false, null: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_09_21_170100) do
ActiveRecord::Schema.define(version: 2018_10_09_022203) do

create_table "audits" do |t|
t.integer "auditable_id", null: false
Expand Down Expand Up @@ -531,6 +531,7 @@
t.float "average_deploy_time"
t.string "prerequisite_stage_ids"
t.string "default_reference"
t.boolean "full_checkout", default: false, null: false
t.index ["project_id", "permalink"], name: "index_stages_on_project_id_and_permalink", unique: true, length: { permalink: 191 }
t.index ["template_stage_id"], name: "index_stages_on_template_stage_id"
end
Expand Down
30 changes: 18 additions & 12 deletions test/models/git_repository_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,25 @@ def call
describe "#checkout_workspace" do
before { create_repo_with_an_additional_branch }

it 'creates a repository' do
Dir.mktmpdir do |temp_dir|
assert repository.checkout_workspace(temp_dir, 'test_user/test_branch')
Dir.chdir(temp_dir) { current_branch.must_equal('test_user/test_branch') }
end
end
[true, false].each do |full_checkout|
describe "with full_checkout #{full_checkout}" do
before { repository.full_checkout = full_checkout }

it 'creates a repository' do
Dir.mktmpdir do |temp_dir|
assert repository.checkout_workspace(temp_dir, 'test_user/test_branch')
Dir.chdir(temp_dir) { current_branch.must_equal('test_user/test_branch') }
end
end

it 'checks out submodules' do
add_submodule_to_repo
Dir.mktmpdir do |temp_dir|
assert repository.checkout_workspace(temp_dir, 'master')
Dir.exist?("#{temp_dir}/submodule").must_equal true
File.read("#{temp_dir}/submodule/bar").must_equal "banana\n"
it 'checks out submodules' do
add_submodule_to_repo
Dir.mktmpdir do |temp_dir|
assert repository.checkout_workspace(temp_dir, 'master')
Dir.exist?("#{temp_dir}/submodule").must_equal true
File.read("#{temp_dir}/submodule/bar").must_equal "banana\n"
end
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/models/job_execution_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ def last_line_of_output
assert_equal '[04:05:06] monkey', last_line_of_output
end

it 'can do a full checkout when requested' do
stage.update_column(:full_checkout, true)
execute_job 'master'
job.output.to_s.wont_include 'worktree'
end

it 'does not fail with nil ENV vars' do
User.any_instance.expects(:name).at_least_once.returns(nil)
execution.send(:run)
Expand Down

0 comments on commit 21c0065

Please sign in to comment.